in reply to multidimensional hash value seems to become unitialized
I would not use $Tid as a global. Pass it around when you need it. Check the value of the open of the file named "file" to make sure that it worked. Instead of using "a" and "b", why not use something more descriptive like the text that was in the file, ie FIRST_NUMBER, etc?
I could make some other comments, but the most important part here is to understand the multi-dimensional hash.
#!/usr/bin/perl -w use strict; use Data::Dumper; # hashes my %Tids=(); #open(IN,"<file") or die "unable to open file"; while(<DATA>) { if(/ORIGINATION_ATTEMPT/) { my $Tid = findTID(); print "$Tid,$Tids{$Tid}{'FIRST_NUMBER'},", "$Tids{$Tid}{'SECOND_NUMBER'}\n"; } } #close(IN); print "============================================\n"; foreach my $Tid (keys(%Tids)) { print "$Tid,$Tids{$Tid}{'FIRST_NUMBER'},", "$Tids{$Tid}{'SECOND_NUMBER'}\n"; } print Dumper \%Tids; ## subroutines sub findTID { my $line=(<DATA>); if($line =~ /Tid: ([0-9a-f]{8})/) { my $Tid = substr($1,3,5); $Tids{$Tid}{FIRST_NUMBER} ="----------"; $Tids{$Tid}{SECOND_NUMBER}="----------"; findNumbers ($Tid); return ($Tid); } } sub findNumbers { my $Tid = shift; my $line=""; while($line=<DATA>, $line !~ /\-{30,}/) { if($line =~ /FIRST_NUMBER\s+(\d+)\s/) { $Tids{$Tid}{'FIRST_NUMBER'}=$1; } if($line =~ /SECOND_NUMBER\s+(\d+)\s/) { $Tids{$Tid}{'SECOND_NUMBER'}=$1; } } } =prints ############################### d1234,9876,9999 a4321,1234,---------- a9999,----------,3333 ============================================ a9999,----------,3333 a4321,1234,---------- d1234,9876,9999 $VAR1 = { 'a9999' => { 'SECOND_NUMBER' => '3333', 'FIRST_NUMBER' => '----------' }, 'a4321' => { 'SECOND_NUMBER' => '----------', 'FIRST_NUMBER' => '1234' }, 'd1234' => { 'SECOND_NUMBER' => '9999', 'FIRST_NUMBER' => '9876' } }; =cut ###################################### __DATA__ --------ORIGINATION_ATTEMPT-------- Tid: abcd1234 FIRST_NUMBER 9876 SECOND_NUMBER 9999 --------------------------------------- --------ORIGINATION_ATTEMPT-------- Tid: defa4321 FIRST_NUMBER 1234 --------------------------------------- --------ORIGINATION_ATTEMPT-------- Tid: defa9999 SECOND_NUMBER 3333 ---------------------------------------
|
|---|