Re: Shortening a script, but aiming for efficiency

[email protected] (Jose Alves de Castro)
Newsgroups perl.fwp
Message-ID <[email protected]>
On Fri, 2004-10-01 at 05:12, John W. Krahn wrote:
> 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?
> 
> Change that to three steps:
> 
> 1) ignore the first line (it's a header line)
> 2) replace all tabs with pipes
> 3) if a line ends in a pipe, change it to a space.

Well... all lines have fields separated with tabs and they all end in a
 tab, too...

Here are some examples:

a\tb\t
a\t\t
\tb\t
\t\t\t

The output for all these should be:

a|b
a|\s
 |b
 |\s

(I know \s isn't a space, but that was for stating it visually)

This means that I could start by replacing all tabs with pipes, but then
 I would always have to remove the last pipe...

I could replace two ending pipes with "| " (second and fourth cases),
 but I would also have to remove a single ending pipe (first and third
 cases)

Sorry if I wasn't clear in the beginning.


> Even easier.
> 
> 
> > So, here's an attempt:
> > 
> > #!/usr/bin/perl -lwn
> > use strict;
> > 
> > $. - 1 || next;
> > s/\t$//;
> > y/\t/|/;
> > s/\|$/ /;
> > print;
> 
> #!/usr/bin/perl -lwp
> use strict;
> 
> $. - 1 || next;
> y/\t/|/;
> s/\|$/ /;
> 
> 
> 
> John
-- 
José Alves de Castro <[email protected]>
  http://natura.di.uminho.pt/~jac
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.