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:
$names{$ref}=$str;
Update: stop inventing new verbs
| [reply] [d/l] [select] |
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.
s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&
| [reply] [d/l] [select] |
|
|
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 | [reply] [d/l] |
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.
| [reply] [d/l] |
|
|
sorry yes ur right posted wrong code $refnum should be $ref
| [reply] |
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.
davidj | [reply] [d/l] [select] |
|
|
it would appear that the number of lines in the hash is double of that in the gly file.
$var1 = undef
$war2 = 'h20'
etc
| [reply] |
|
|
| [reply] [d/l] [select] |
|
|
Re: hash problem
by Fletch (Bishop) 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
| [reply] [d/l] [select] |
Re: hash problem
by murugu (Curate) on Jun 08, 2005 at 09:54 UTC
|
Hi,
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.
Regards, Murugesan Kandasamy.
| [reply] |