Chuck,

On Monday 23 July 2001 12:18, you wrote:
> I'm trying to take a string, which may or may not contain a $ and/or
> a comma, and return just the number with 2 decimal places. I am
> having some trouble. Here is my test code:

Try this:

#!/usr/bin/perl

my @a = qw( $1234.05 1234.78 $768 777.99 );
#$a[2] = "$768";

for ($j=0; $j<=3; $j++){
        print "$j. " . Stripprice($a[$j]) . "\n";
}

sub Stripprice{
        my ( $inpr ) = @_;
        $inpr =~ s/[\$|,]//g;
        return $inpr;
}

The commented out line is a problem because perl thinks that $768 is a 
scalar variable and, since it's not been assigned to previously, the 
interpreter automatically creates it and initializes it with an undefined 
value.  This is autovivification in action.

Why doesn't it do that with the 0 index of the array?  Well, a scalar that 
starts with a digit must be only digits (according to the only reference I 
have at hand, "Effective Perl Programming").  The comma seems to be a 
signal to perl that this is a string, but I don't know the details - I'm 
still plowing through Simon Cozens' tutorial on Perl internals.

To see the autovivification come back, uncomment the assignment to index 2.

I hope I haven't confused the issue.

Regards,

Troy Denkinger


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to