Incantation one:
my $section = 0; my @sectone; my @secttwo; while(<TEXTFILE>) { if (/;section(1)/) { push(@sectionone, $_); } elsif (/;section(2)/) { push(@sectiontwo, $_); } else { print "No section defined for: $_\n"; } }
Incantation two:
my $section = 0; my @sectone; my @secttwo; while(<TEXTFILE>) { if (/;section([12])/) { push( $1 == 1? @sectionone : @sectiontwo, $_); } else { print "No section defined for: $_\n"; } }
Incantation three:
my $section = 0; my %sections = (1 =>[],2=>[]); while(<TEXTFILE>) { if (/;section([12])/) { push @{$sections{$1}},$_; } else { print "No section defined for: $_\n"; } }
update: Incantation four (kinda hesitated on this one):
my $section = 0; my %sections = (1 =>[],2=>[]); while(<TEXTFILE>) { push @{$sections{$1}},$_ and next if /;section([12])/; warn "No section defined for: $_\n"; # cause warn gives you $. in <TEXTFILE> }
incantation huh?:
I dunno... I guess none of them work ... until now maybe

Incantation two:

my $section = 0; my @sectone; my @secttwo; while(<TEXTFILE>) { if (/;section([12])/) { $section = ( $1 == 1 ? @sectionone : @sectiontwo); } else { push @{$section},$_ and next if $section; warn "No section defined for: $_\n"; } }
Incantation three:
my $section = 0; my %sections = (1 =>[],2=>[]); while(<TEXTFILE>) { if (/;section([12])/) { $section = $1 and next } push @{$sections{$section|| warn "No section defined for: $_\n" and next()}} ,$_; }
Incantation four:
my $section = 0; my %sections = (1 =>[],2=>[]); while(<TEXTFILE>) { $sections=$1 and next if /;section([12])/; warn "No section defined for: $_\n" unless $sections; push @{$sections{$sections}},$_ }
Interesting aint isn't it ... no matter how relatively simple a task, it's easily fudged

update:
I lobster, but I never flounder

my $section = 0; my %sections = (1 =>[],2=>[]); while(<TEXTFILE>) { push @{$sections{( /;section([12])/ and $section = $1 and next() ) or ( $section || warn "No section defined for: $_\n" and next() )}} ,$_; }
** note, I tested none of these, but they should work ;)

 
______crazyinsomniac_____________________________
Of all the things I've lost, I miss my mind the most.
perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"


In reply to (crazyinsomniac) Re: Tidier and more efficient parsing code by crazyinsomniac
in thread Tidier and more efficient parsing code by JPaul

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.