Oops... (check below) On Thu, 2004-09-30 at 16:19, Jose Alves de Castro wrote: > To make it short: I have a small filter that I'd like to keep as compact > as possible, while not losing efficiency. > > How it works: > > 1) ignore the first line (it's a header line) > 2) if a line ends in a tab, remove that character > 3) replace all tabs with pipes > 4) if the line now ends in a pipe, add a space at the end > > This all looks pretty easy, right? > > So, here's an attempt: > > > #!/usr/bin/perl -lwn > use strict; > > $. - 1 || next; > s/\t$//; > y/\t/|/; > s/\|$/ /;
That would be: s/\|$/| /; > print; > > > All rather easy, right? But this was the point where my mind started > playing tricks on me... I didn't want to have much code, but I did want > the script to be efficient. > > I thought that those two substitutions were going to consume more > resources than they should, as they would probably go on through the > whole line, trying to match the end of line after each tab/pipe (am I > not correct?) > > I decided to try and replace them with substr, like this: > > > $. - 1 || next; > substr($_,-1) = '' if substr($_,-1) eq "\t"; > y/\t/|/; > $_ .= ' ' if substr($_,-1) eq '|'; > print; > > > This is, apparently, not a good solution... it takes longer :-| (not > that much, but it does) > > So... has my mind been playing tricks on me when I thought those > substitutions weren't as optimal as they could be? > > > Any other pointers/suggestions/whatever would be welcome :-) > > > (The part of skipping the first line could be replaced with easier > things, but a simple BEGIN{<>} would issue a warning, so I decided to > keep that out for now) > > > Regards, > > jac -- José Alves de Castro <[EMAIL PROTECTED]> http://natura.di.uminho.pt/~jac