Jakob Kofoed wrote:
Hi,
Hello,
I would like to load a file to a hash so I will be able to call a
specific number in that hash:
use strict;
use warnings;
my %hash = ();
open IN, "< $ARGV[0]" || die "could not open $ARGV[0]\n";
That will never die() because of the high precedence of the || operator.
You need to either use open() with parentheses or use the lower
precedence 'or' operator. You should also include the $! variable in
the error message so you know why it died.
open IN, "< $ARGV[0]" or die "could not open $ARGV[0]: $!";
my $cnt = "0";
If you are going to use $cnt numerically why are you assigning a string
to it? Why not just use the built-in $. variable?
while (<IN>) {
chomp;
%hash = ( $cnt => $_ );
You are assigning a list to %hash which overwrites the hash each time
through the loop. What you want to do is assign the value to a specific
key.
$hash{ $cnt } = $_;
But if all of your keys are sequential numbers then you should just use
an array instead:
push @array, $_;
$cnt++;
}
print $hash{"837"};
This works fine with the last line only! how do I append to my hash?
and secondly: here I use a counter to get a specific "line" in the
hash - does a hash have a "build in" counter to refer to?
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>