in reply to Look ahead and join if the line begins with a +
=>my @list; #Read first, ask questions later is probably not optimal for this #Scope our variable, merge, read, and assign all at once while(chomp(my $line = <NETLIST>)){ #Save a match, substitution returns true iff we susbtituted if ($line =~ s/^\+//){ #Append to previous line (last line in the array) $list[-1] .= $line; } else{ #Save the line push(@list, $line); } } #Map is good, nice way to get all the newlines back #join would work if you the file need not end in a newline print map {"$_\n"} @list;
=>my @list; while(chomp(my $line = <NETLIST>)){ #Trinary is good ;-) $line =~ s/^\+// ? $list[-1] .= $line : push(@list, $line); } print map {"$_\n"} @list;
my @list; #for is very perlish, so is using $_ chomp && $_ =~ s/^\+// ? $list[-1] .= $_ : push(@list, $_) for <NETLIS +T>; print map {"$_\n"} @list;
--
perl -pe "s/\b;([mnst])/'\1/mg"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
What if last line has no LF?
by RMGir (Prior) on Apr 11, 2002 at 10:46 UTC | |
by belg4mit (Prior) on Apr 11, 2002 at 14:07 UTC | |
|
Re: Re: Look ahead and join if the line begins with a +
by petral (Curate) on Apr 11, 2002 at 18:57 UTC | |
by belg4mit (Prior) on Apr 11, 2002 at 19:11 UTC | |
by petral (Curate) on Apr 11, 2002 at 19:18 UTC |