in reply to reading from a file and initializing a variable
#!/usr/bin/perl
no strict; # Yuk! no warnings; # Yuk! # s/no/use/gsm;
my $step1; my $step2; my $step3; my @array1; my @array; my $i = 0;
open(FILE, "assign");
open my $fh, '<', 'assign' or die "hard, but say why (\$!): $!\n";
Huh?!? Do you mean to iterate over the lines of your file? If so... the you're not doing it. Just do it the perl idiomatic way:$line = <FILE>; while ($line ne ""){
while (<$fh>) { ... # or while (my $line=<$fh>) { ...
So you are using a hash after all! Then stop here and stick with it!! Don't try useless symref hacking...chomp($line); if($line =~ /^(\S+)\s+(\d+)$/) { print "inside\n"; $hash{$1} = $2;
Am I missing something or are you doing... ehm! nothing, with that self mainteined counter?$i++;
Worth repeating: don't! (even if you knew how to do it correctly!)while( ($key, $value) = each %hash) { print "$key +> $value\n"; $key = $value; }
Proof of concept:
#!/usr/bin/perl -ln use strict; use warnings; BEGIN { @ARGV='assign' } our %hash; $hash{$1}=$2 if /^(\S+)\s+(\d+)/; END { my @msg=qw/FOO 1st 2nd 3rd/; $hash{"\$step$_"} and print $msg[$_] for 1..3; } __END__
|
---|