http://www.perlmonks.org?node_id=976228


in reply to Re: File Parsing
in thread File Parsing

The example in the original post is one step in my overall goal. In moving forward, I think I do want a hash, however, the hash value for key 1 should be @array. Then the @array is undefined and the next match creates a new @array with is then the hash value for key 2. This process is repeated until all matches have been found in the file for each element in @headers. I am really struggling with this one.... I hope my explanation is clear. Here is my attempt at this.

#!usr/bin/perl use Tk; use Cwd; use strict; use warnings; # ###################################################################### +##################### # GUI Building ###################################################################### +##################### # # Create Main Window my $mw=new MainWindow; my $filename; my $line; my $n; my @headers = ("LAMINATE PROPERTIES", "LAMINATE STRESSES", "LAMINATE STRAINS", "CIRCUMFERENTIAL AND RADIAL STRESSES & STRAINS", "DISPLACEMENTS", "STRAINS PER PLY", "STRESSES PER PLY", "FAILURE CRITERIA PER PLY"); my $inner; my @array; my %hash; my $ms_button = $mw->Button(-text=>"MS", -command=> \&BJSFM_MS)->pack(); MainLoop; sub BJSFM_MS { $filename="BJSFM_out.prn"; open(OUTPUT_FILE, "< $filename") or die "Can't find $filename!"; $n=1; while ($line = <OUTPUT_FILE>) { if ($line =~ /$headers[$n-1]/) { undef @array; #This clears the previous two instances + of $string1 <OUTPUT_FILE> for 1..3; while ($inner = <OUTPUT_FILE>) { last if $inner =~ /^$/; push @array,$inner; } $hash{$n}=@array; $n++; } } close OUTPUT_FILE; print @array; print %hash; }