in reply to Tidier and more efficient parsing code
Incantation two: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 three:my $section = 0; my @sectone; my @secttwo; while(<TEXTFILE>) { if (/;section([12])/) { push( $1 == 1? @sectionone : @sectiontwo, $_); } else { print "No section defined for: $_\n"; } }
update: Incantation four (kinda hesitated on this one):my $section = 0; my %sections = (1 =>[],2=>[]); while(<TEXTFILE>) { if (/;section([12])/) { push @{$sections{$1}},$_; } else { print "No section defined for: $_\n"; } }
incantation huh?: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 two:
Incantation three: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 four: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()}} ,$_; }
Interesting aint isn't it ... no matter how relatively simple a task, it's easily fudgedmy $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}},$_ }
update:
I lobster, but I never flounder
** note, I tested none of these, but they should work ;)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() )}} ,$_; }
| ______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 |