Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

hash problem

by Anonymous Monk
on Jun 08, 2005 at 08:45 UTC ( #464591=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,
%names; { open (name, "<data/gly.txt"); while (<name>) { ($ref, $str) = split /\|/; $names{$refnum} = $str; } } $number =1; $structure = $names{ $number} or die "unable to open";
the $number is generated by another loop, just testing with a single value. it is unable to perform the last line of the code and prints the die message. Where $strutcure should be $str when $ref =1 Thanks

Replies are listed 'Best First'.
Re: hash problem
by robartes (Priest) on Jun 08, 2005 at 08:56 UTC

    I don't know whether this is part of a larger script, but $refnum never gets set in this snippet, so you are storing your data in a hash entry with an empty key. That's why the entry with key '1' is undefinded undefined, triggering your die.

    Perhaps in the while loop you meant:


    Update: stop inventing new verbs


Re: hash problem
by muntfish (Chaplain) on Jun 08, 2005 at 08:53 UTC

    It's not necessarily true that it is "unable to perform the last line of code".

    The code after the or will be run if the results of the previous code was 0, empty string, or undefined. So if $names{1} is any of those things, your die will be called.

    Is that die in the right place anyway? Looks like it should be after your open:

    open (name, "<data/gly.txt") or die "unable to open";

    It's more common to use CAPS for file handles, by the way.

    Hope this helps - if not please post a specific example of your input to illustrate what's going on.

      if i print $structure i get nothing at all. the file split looks like:
      1| h20 2| acyl etc
      so the $structure when $number =1 should be h20
Re: hash problem
by holli (Abbot) on Jun 08, 2005 at 08:56 UTC
    err, you split into $ref and then use $refnum? Apparently those 2 are not the same variables. How do you make sure $number equals $ref/$refnum?

    I bet this is one of the cases where use strict; would help.

    holli, /regexed monk/
      sorry yes ur right posted wrong code $refnum should be $ref
Re: hash problem
by davidj (Priest) on Jun 08, 2005 at 09:02 UTC
    First, I think you might have an error in the code you submitted. I assume that

    $names{$refnum} = $str;
    should be
    $names{$ref} = $str;
    Second, the reason the code is dying is because on the last line,  $names{$number} does not exist.

    It might be a good idea to take a look at what $names is actually holding after the loop. A good way to see that is to use Data::Dumper

    #!/usr/bin/perl use Data::Dumper; %names; { open(name, "<data/gly.txt"); while(<name>) { ($ref, $str) = split /\|/; $names{$ref} = $str; } } print Dumper(%names);
    This will help you track down the problem.


      it would appear that the number of lines in the hash is double of that in the gly file. $var1 = undef $war2 = 'h20' etc
        The hash has not doubled. Rather, $var1 = undef is the key to the first hash element, $var2 = 'h20' is the value of the first hash element, etc.

        The good news is, this verifies the problem. That is, your keys are empty. Please show us the content of the text file you are using. That will certainly help us help you resolve the problem.


Re: hash problem
by Fletch (Chancellor) on Jun 08, 2005 at 12:26 UTC

    And no one else has explicitly mentioned it, but just having the name of a hash in void context (%names; in your code) doesn't do anything (or at least anything useful). You probably meant my %names;, but then you're not using strict or warnings so you wouldn't have gotten any gripes.

    We're looking for people in ATL

Re: hash problem
by murugu (Curate) on Jun 08, 2005 at 09:54 UTC


    The code you written have some typo errors. You have assigned splitted values into $ref and $str, but you are storing the $str to an hash %names with key as $refnum which in turn is undefined. That is why die part is getting executed.

    Murugesan Kandasamy.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://464591]
Approved by ghenry
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2020-10-20 20:08 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (210 votes). Check out past polls.