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


in reply to File Parsing

chorobas solution and commentary is excellent; I don't disagree with any of it. I offer this as an alternate possible solution. (I was doing this before I saw that was posted.) Here I slurp the file in by paragraphs then pick out the lines I want to save.

#!usr/bin/perl use strict; use warnings; use Tk; use Cwd; use Data::Dumper; # ###################################################################### +##################### # GUI Building ###################################################################### +##################### # # Create Main Window my $mw = MainWindow->new; my $n = 0; my %temp; my $ms_button = $mw->Button( -text=>"MS", -command=> \&BJSFM_MS )->pack(); MainLoop; sub BJSFM_MS { my $filename = 'BJSFM_out.prn'; #open my $infile, '<', $filename or die "Can't open $filename! $!" +; my $infile = *DATA; local $/="FAILURE CRITERIA PER PLY\n"; while (my $paragraph = <$infile>) { chomp $paragraph; $paragraph =~ s/^\s+\n|\s+$//g; my @lines = split "\n", $paragraph; next if @lines < 4; map {$temp{$n++} = $_ } @lines[3..$#lines]; } close $infile; print Dumper \%temp; } __DATA__ FAILURE CRITERIA PER PLY DIST ANGLE PLY FAILURE NUMBERS 1 2 SHEAR 0.000 0.00 -45.00 0.238 0.282 -1.459 0.000 0.00 0.00 0.971 1.369 0.004 0.000 5.00 -45.00 0.475 0.142 -1.585 0.000 5.00 0.00 1.003 1.531 -0.274 0.000 10.00 -45.00 0.721 0.037 -1.623 FAILURE CRITERIA PER PLY DIST ANGLE PLY FAILURE NUMBERS 1 2 SHEAR 0.000 0.00 -45.00 0.247 0.293 -1.514 0.000 0.00 0.00 1.008 1.422 0.004 0.000 5.00 -45.00 0.493 0.147 -1.645 0.000 5.00 0.00 1.042 1.589 -0.284

Replies are listed 'Best First'.
Re^2: File Parsing
by shortyfw06 (Beadle) on May 31, 2012 at 20:45 UTC

    Thank you for the replies. To complicate this a bit more, if the file looks like this and I need to parse out the last two occurances of the search string, skip three lines in each and then only print the lines of data that have 0.150 in the first field and -45.00 in the third field, what would I add? Thanks Again!

    9 FAILURE CRITERIA PER PLY FAILURE CRITERIA PER PLY DIST ANGLE PLY FAILURE NUMBERS 1 2 SHEAR 0.000 0.00 -45.00 0.238 0.282 -1.459 0.000 0.00 0.00 0.971 1.369 0.004 0.150 5.00 -45.00 0.475 0.142 -1.585 0.150 5.00 0.00 1.003 1.531 -0.274 FAILURE CRITERIA PER PLY DIST ANGLE PLY FAILURE NUMBERS 1 2 SHEAR 0.000 0.00 -45.00 0.247 0.293 -1.514 0.000 0.00 0.00 1.008 1.422 0.004 0.150 5.00 -45.00 0.493 0.147 -1.645 0.150 5.00 0.00 1.042 1.589 -0.284
      if the file looks like this and I need to parse out the last two occurances of the search string, skip three lines in each and then only print the lines of data that have 0.150 in the first field and -45.00 in the third field, what would I add?

      <smartass>"Code to do that" seems like an obvious answer.</smartass>

      Honestly though, there is code in my example above that demonstrates splitting into fields and checking for equality isn't a very obscure operation. Try it yourself and if you can't figure it out, show us your code that doesn't work. You'll get less sarcastic answers if you demonstrate that you've made a little effort.