Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Why are my hash keys undefined?

by biologistatsea (Acolyte)
on Feb 08, 2016 at 17:03 UTC ( [id://1154647]=perlquestion: print w/replies, xml ) Need Help??

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

Below is part of a script that I think should read a file into a hash, so I can look up the record using the key from other parts of the program. The print statements include are for trying to find out what's wrong! The program finds the name successfully (based on the print in the if loop), and creates the hash with the right values, but the keys are always empty (based on the output of the while loop that prints the hash)

open (NOV, '<', $novo); my %denovo; while (my $line = <NOV>){ chomp $line; if ($line =~ /([^,]*)$/){ my $name = "$1"; print "name is $name\n"; $denovo{$name} = "$line"; } } print "hash denovo is:\n"; while( my( $key, $value ) = each %denovo ){ print "$key: $value\n"; } close NOV;

I've tried changing the presence of / types of commas around $name when defining each hash entry. The keys should look like following: R2b.9138.9138.2(intensity=1341005.0692)_2 so I've tried using simpler names in case there were some illegal characters or something - but nothing works! I think I must be misunderstanding something fairly fundamental. Any help would be enormously appreciated.

Replies are listed 'Best First'.
Re: Why are my hash keys undefined?
by choroba (Cardinal) on Feb 08, 2016 at 17:13 UTC
    The * quantifier means "zero or more times". It seems your data contain commas at line ends, so the non-comma is matched zero times. $1 is then the empty string:
    #!/usr/bin/perl use warnings; use strict; my %denovo; while (my $line = <DATA>){ chomp $line; if ($line =~ /([^,]*)$/){ my $name = "$1"; print "name is $name\n"; $denovo{$name} = $line; # Quotes not needed. } } print "hash denovo is:\n"; while( my( $key, $value ) = each %denovo ){ print "$key: $value\n"; } __DATA__ no comma comma, in the middle comma at the end, previous line was empty
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Thanks for the response. The thing is my data doesn't have commas at the ends of the line, and I know that the regex matchs the right thing because the print in the if statement always gives me the names I expect. Despite this, the keys are empty!
        Windows line endings, then? The keys aren't empty, but contain "\r" at the end. When printed, the character moves the cursor to the beginning of the line, and the value then overwrites the key. Convert your files to the *nix format using dos2unix or fromdos, or apply
        s/\r//
        to your input lines.
        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Why are my hash keys undefined?
by Laurent_R (Canon) on Feb 08, 2016 at 17:57 UTC
    Try replacing:
    chomp $line;
    with:
    $line =~ s/[\r\n]+//g;
    It might solve your problem if it is related, as I suspect, with line endings.
Re: Why are my hash keys undefined?
by dorko (Prior) on Feb 08, 2016 at 19:55 UTC
    I'm a big fan of debugging by print statements. When I'm using print statements to see what's in a variable, I'll wrap the variable in some odd characters to see special characters that otherwise might not show up on the screen.

    If you change this line:

    print "name is $name\n";

    to something like this:

    print "name is |||$name|||\n";

    you'll see tabs, spaces, newlines, etc. at the beginning and end of your variable you might not otherwise notice.

    Cheers,

    Brent

    -- Yeah, I'm a Delt.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1154647]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (None)
    As of 2024-04-19 00:10 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found