in reply to Tidier and more efficient parsing code

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;"

Replies are listed 'Best First'.
Re: (crazyinsomniac) Re: Tidier and more efficient parsing code
by demerphq (Chancellor) on Jan 24, 2002 at 15:19 UTC
    Im confused dude. Does that fourth one actually work?

    I just ran it and it seems not to... As far as I can tell the push will only happen when there is a ;section on the line.

    Sorry.

    Yves / DeMerphq
    --
    When to use Prototypes?