in reply to Need help from the esteemed monks on scoping/hashes
You should enable both strict and warnings pragmas.
You should verify that the file opened correctly before trying to use the filehandle.
You have all the variables declared inside the while loop so they are created anew each time a line is read from the file.
This may do what you want:
#!/usr/bin/perl use strict; use warnings; open IHF, '<', 'top.spef' or die "Cannot open 'top.spef' $!"; my %mapping; while ( <IHF> ) { tr/*//d; my @fields = split; if ( /NAME_MAP/ .. /PORTS/ ) { redo if /PORTS/; next unless @fields == 2; $mapping{ $fields[ 0 ] } = $fields[ 1 ]; } if ( /PORTS/ .. eof ) { next unless /D_NET/ and @fields == 3; print "$fields[0] $mapping{$fields[1]} $fields[2]\n"; } } __END__
|
|---|