On 2/21/07, Sumit Shah <[EMAIL PROTECTED]> wrote:
Hello All,
I have a string like: 'a = 1; b = 2; c = 3; d = 4'
Whats the best way to parse it so that I can get a value for c, which is
3? I have used the hash approach. But, I was wondering if there is a
faster way to do it in Perl.
Well, from a somewhat-beginners standpoint, the following worked for me:
##### Begin Code #####
#!/usr/bin/perl -w
use strict;
my $i;
my $values;
my @values2;
my %valuesHash;
my $hashKeys;
my $keys;
$values = "a=1;b=2;c=3;d=4";
@values2 = split(/;/, $values);
for ($i=0; $i <= $#values2; $i++)
{
my $index;
foreach $index ($values2[$i])
{
$index =~ m/^(\w)\=(\d)$/;
$valuesHash{$1} = "$2";
}
}
print ("$valuesHash{c} \n");
##### End Code #####
I know, its a little much, but TIMTOWTDI, and this is how I ended up doing
it. It uses a hash as you said you had tried. Like others, I don't see why
you say its slow though.
Regards,
jlk