I have a perl variable which contains the below value.
$var1 = "2% / 51%"
Now I would like to split the data into 2 different variables.
For example
$part1 = 2
$part2 = 51
Could anyone please help me in this regard ?
Regards,
GS
I have a perl variable which contains the below value.
$var1 = "2% / 51%"
Now I would like to split the data into 2 different variables.
For example
$part1 = 2
$part2 = 51
Could anyone please help me in this regard ?
Regards,
GS
Hi,
Try this one,
if($var =~ /^(\d+)%.*(\d +)%$/) {
my ($part1, $part2)=($1,$2);
}
Cheers,
Ranga:-)
Many thanks for replying over my post.
Tried the below code but no luck... black space was printed instead of expected 2 and 51.
$var1 = "2% / 51%";
if($var1 =~ /^(\d+)%.*(\d +)%$/) {
my ($part1, $part2)=($1,$2);
}
print $part1;
print "\n";
print $part2;
Could you please help me on this?
---------- Post updated at 04:00 AM ---------- Previous update was at 03:30 AM ----------
Finally with minor changes... I had done with it ..
$var1 = "9% / 41%"; $z=0;
while($var1 =~ m/(\d+)/g) {
$item[$z]=$1;
$z++;
}
print $item[0];
print $item[1];
thanks alot rangarasan ...!!
By declaring $part1
and $part2
as lexical variables within the if
block, you've scoped them to that block. Hence, no values ( undef
) or values before that block, are printed outside the block. Remove my
before the variables. This will make them global and the values will be accessible outside the if
block.
Also, that regexp is not correct. The $2
value will always be a single digit.
Try:
my ($var1,$var2,$var3);
$var1 = "2% / 51%";
if($var1 =~ /^(\d+)%.*?(\d+)%$/) {
($part1, $part2)=($1,$2);
}
print $part1;
print "\n";
print $part2;