Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Constructing a hash - why isn't my regex matching anything

by PeterPeiGuo (Hermit)
on Dec 19, 2010 at 08:27 UTC ( [id://877829] : note . print w/replies, xml ) Need Help??


in reply to Constructing a hash - why isn't my regex matching anything

One way to understand the result of the matching operation is to view it as a true/false value that indicates whether there is a match, and that's not what you wanted. To get what you want, do the following (you may have to further tweak your regexp, but that's a different story):

$line =~/;(.*)\s-\s/; my $key = $1; $line =~/\.\\(.*)-\d+\;/; my $value = $1;

Peter (Guo) Pei

Replies are listed 'Best First'.
Re^2: Constructing a hash - why isn't my regex matching anything
by perl_mystery (Beadle) on Dec 19, 2010 at 08:42 UTC

    I keep getting the below warning

    Use of uninitialized value in hash element at hash_construct.pl line 15, <$fh> line 909. Use of uninitialized value in concatenation (.) or string at hash_construct.pl line 11, <$fh> line 910.

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash; open my $fh, '<', $ARGV[0] or die "could not open $ARGV[0]'' $!"; while (my $line = <$fh>) { $line =~/;(.*)\s-\s/; my $key = $1; print "KEY:$key\n"; $line =~/\.\\(.*)-/; my $value = $1; print "VALUE:$value\n"; $hash{$key}=$value; } print Dumper(\%hash);
      Yes, I know, do you have a question?

        why is $key and $match not matching anything?what change should I do in regex to match them?