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.
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);
/
**************************************************************************************/
Please help.
Hi Ankur.
As has been explained, the line
my($n,$d) = split /\./,shift,2;
splits the string into a maximum of two pieces at the first full-stop
character. Because there is no such character in the target string
there is no second piece, and the value of $d is undefined. The warning
is generated when you try to use it in a pattern match at line 15.
To get around this you could change the statement to
$n = "$n.$d" if $d and $d =~ /\d/;
(there is also no need to escape the full-stop in a simple quoted
string: it is only a special character in regular expressions.)
You may prefer the the subroutine below which does this job more
concisely.
HTH,
Rob
sub CommaFormatted {
my $delimiter = ',';
my $n = shift;
while (1) {
last unless $n =~ s/^(\d+)(\d\d\d)/$1$delimiter$2/;
}
return $n;
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/