pacman has asked for the wisdom of the Perl Monks concerning the following question:

I have a file with some data which i want to crunch into an AOH.Thed data corresp. to a record needs to be put into a hash.The data is of the form

Record#1:
name:'George Simpson'
age:25
occupation:'PLumber' ... so on

I use the following code to seperate the 'name'(to be the key of hash) & 'George Simpson'(to be the corresp value)

while(defined($index = <$inp_file>)) { if (($index =~ m"Record#") && ((keys %$hash_ref) != 0)) { return ($hash_ref); } next if ($index =~ m"Record#"); $index =~ m"(.*?): (.*)"; my $key = $1; my $val = $2; #1 $key =~ s"^\s+""g; #2 $key =~ s"\s+$""g; #3 $val =~ s"^\s+""g; #4 $val =~ s"\s+$""g; $hash_ref->{$key} = $val; }

Problem is that the four #ed lines above that i use to clean empty spaces is generating a warning which says :

Use of uninitialized value in substitution (s///)at f2ds4.pl at line 30 , <GENO> line 13

I am a bit lost here .. though the script runs and gives results, Id like to know which uninitialized value it is talking about ! thank you -pacman

edit by thelenm: added code tags

Replies are listed 'Best First'.
Re: cleaning up spaces on either side of an extracted string
by cLive ;-) (Prior) on Mar 19, 2004 at 07:48 UTC
    Try changing this:
    $index =~ m"(.*?): (.*)"; my $key = $1; my $val = $2;
    to this:
    my $key=''; my $value=''; if ($index =~ m"(.*?): (.*)") { $key = $1; $val = $2; }

    Never assume a match will exist. Consider this piece of careless code : )

    'boo!' =~ /(.*)/; $test_string = "this is a test sentence."; $test_string =~ /(\w+)$/; my $last_word=$1; print "$last_word\n";

    If you assume a match will take place, you'd get extremely confused :)

    cLive ;-)

      hey clive thanks ..i'll keep that in mind .
Re: cleaning up spaces on either side of an extracted string
by matija (Priest) on Mar 19, 2004 at 07:43 UTC
    Well, it appears that in some circumstances, either $key, $val or possibly both can be undefined. Since that probably indicates a problem with the syntax of your input file, you should test for it:
    unless (defined($key) && defined($val)) { warn "Problem in input: <$index>\n"; } else { $key =~ s"^\s+""g; $key =~ s"\s+$""g; $val =~ s"^\s+""g; $val =~ s"\s+$""g; $hash_ref->{$key} = $val; }
      hey mat thanks for the tip , it worked :D