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$..$/

In reply to Re: Parsing simple text, made difficult by liverpole
in thread Parsing simple text, made difficult by sdyates

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.