Striping the zeros may not be necessary depending on what you are using the
$index and $value for. If you are actually going to use $index and $value
as numbers, the leading zeros won't matter. For example:
my $index = '0003';
print "Original: ";
print $index;
print "\n";
print "In a string context: ";
print $index . '15';
print "\n";
print "In a numerical context: ";
print $index + 15;
print "\n";
will print the following:
Original: 0003
In a string context: 000315
In a numerical context: 18
That's the good thing about loosly typed languages... (I guess it's also the
bad thing.)
Bruce W. Lowther
-----Original Message-----
From: David M. Lloyd [mailto:[EMAIL PROTECTED]]
Sent: Thursday, April 19, 2001 8:48 AM
To: Mark Martin
Cc: [EMAIL PROTECTED]
Subject: Re: Removing Leading Zeros
On Thu, 19 Apr 2001, Mark Martin wrote:
> Hi all,
> I have sucked two substrings into two variables :
>
> $index = substr $_, 35, 11;
> $value = substr $_, 64, 6;
>
> These variables may or may not have leading zero/s :
>
> 000009/000099/000999........ and so on.
>
> If they do I need to strip away the leading zeros.
>
> Any ideas?
# Begin perl code
$index =~ s/^0+//;
$value =~ s/^0+//;
# End perl code
It reads like this:
"Apply a substitution to $index, in which any group of 1 or more zeros (0)
at the beginning of the string, are replaced with nothing".
The s/// operator is very powerful. I'd recommend reading up on it... you
can do all kinds of cool things with it.
- D
<[EMAIL PROTECTED]>