in reply to Parsing simple text, made difficult

Hi sdyates,

I've made an attempt to refactor your code somewhat, and have come up with the following:

# Use strict and warnings -- ALWAYS! use strict; use warnings; # Declare things that might change at the top of the program my $input = "dump.txt"; my $output = "/Users/simondyates/Desktop/OutFile.txt"; # Open the output OUTSIDE of the subroutine my @Finish = ProcessRedFile(); # Check the return value to see if the file was open (OUT, ">", $output) or die "Can't write '$output' ($!)\n"; print OUT "\t"; my @Test2 = ( ); my $x; for ($x = 0; $x < @Finish; $x++) { my @Test = split(/\t/, $Finish[$x]); push @Test2, @Test; } @Finish = @Test2; my $f = 0; while ($x < 200) { # Consolidate -- all branches for $f in {1, 2, 4, 5} did the same +thing! if ($f == 1 or $f == 2 or $f == 4 or $f == 5) { print OUT "$Finish[$x]\t"; } elsif ($f == 8) { print OUT "\n"; $f=0; } $x = $x + 1; $f++; } close OUT; # # Consider choosing a better subroutine name -- I have no # idea what a "Red File" is ... ?? # sub ProcessRedFile { open (READ, $input) or die "Can't read '$input' ($!)\n"; my @File = <READ>; my @lines = ( ); foreach my $File (@File) { # This regex doesn't make much sense -- you're trying # to split on 2 whitespace character with optional leading # or trailing spaces?? Why not just split(/\s\s+/, $file)?? my @Format = split(/ *\s\s */, $File); push @lines, @Format; } close READ; return @lines; }

Note that it is now strict and warning safe.  However, I'm still not following all the logic, particulary with regards to the hardcoded while ($x < 200) {.

Part of my confusion may be due to not having a copy of the data file you're using, but you should strive to make your program not rely on hardcoded numbers; rather, have it process just as much data as is available.

You should try this code and see if it gets you any farther.  It opens the output file outside of the subroutine so that the filehandle stays in scope (and also because a subroutine for processing an input file shouldn't be doing anything with an output file).  I've also put comments in the code at places where I've changed things, or where there's some question as to the functionality.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/