[EMAIL PROTECTED] wrote:
>
On Dec 4, 7:25 am, [EMAIL PROTECTED] (Ankur) wrote:
I am running the following code and receiving the warning :
Use of uninitialized value in pattern match (m//) at a.pl line 15.
That is to be expected.
a.pl :
/
**************************************************************************************/
sub CommaFormatted{
my $delimiter = ','; # replace comma if desired
my($n,$d) = split /\./,shift,2;
my @a = ();
while($n =~ /\d\d\d\d/)
{
$n =~ s/(\d\d\d)$//;
unshift @a,$1;
}
unshift @a,$n;
$n = join $delimiter,@a;
$n = "$n\.$d" if $d =~ /\d/;
return $n;
}
print CommaFormatted(10000);
You are setting $d to be the second part of '10000' as delimited by
'.'.
There are no periods in '10000' so there's no such thing.
Therefore $d is set to the special undefined value. The pattern
matching operation operates on strings. The special undefined value is
not a string. It can be implicitly converted into a null string but
often needing to so this is a sign that something is wrong. That's
what the warning is saying. Actually the warning says 'uninitialized'
when it means 'undefined'. I have always considered this to be a bug
in Perl.
As it happens, even if there was a '.' as the last character in your
input string then you'd still get the same message. This is counter-
intuitive but is documented in the documentation of the split()
function.
No.
my @list = split /\./, '1000.';
will indeed generate only one value, but split() will omit trailing null
fields only if the number of fields is defaulted. In this case the
call is
my ($n,$d) = split /\./, shift, 2;
which specifies the field count in two ways: the third parameter says
that the string should be split into (a maximum of) two parts. and even
if that wasn't there the two scalars in the list on the left hand side
would force an implicit split into three parts.
Rob
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/