Hi Perlers,

On 30 Sep 2004 10:11:29 +0100, Jose Alves de Castro
<[EMAIL PROTECTED]> wrote:
> On Wed, 2004-09-29 at 21:25, JupiterHost.Net wrote:
> > >>> I would like the output in the following format
> > >>> object1<...tab....>Description1
> > >>> object2<...tab....>Description2
> > >>> object3<...tab....>Description3
> > >>
> > >>
> > >> perl -lne 'BEGIN{$/="\n\n";}s/\n/\t/;print' FILENAME
> > >
> > >
> > >
> > > perl -l -00pe's/\n/\t/' FILENAME
> >
> > That's pretty slick you guys, he's sure to get an A+ ;)
> >
> > If your teacher requires the quotes to be removed:
> 
> What if the teacher requires an explanation? O:-)
> 
> It is my opinion that code should be explained, at least in this list.
>  You're trying to teach people how to fish (and maybe swim). Giving them
>  fish is good, of course, but tell them how you got it :-)
> 
> That said, nice code :-)
> 
> >   perl -l -00pe's/\n/\t/;s/\"//g;' FILENAME
> >
> > :)
> -- 
> José Alves de Castro <[EMAIL PROTECTED]>
>   http://natura.di.uminho.pt/~jac
> 

I'll give it a try.

First, it's good to know that the two Perl special variables '$/' and
'$\' are the input separator and output separator.  By Default, they
will be $/ = \n (newline character) and $\ = undef (nothing.  No
output separator).

Now, on the command line, the '-0' option will set the input separator
($/).  In the above example, it's setting $/ = 0.  Also, in the
example, the '-l' will do two things.  First, it will automatically
chomp() whatever's in '$/', and then it will set the output separator
to be whatever the input separator will be.  So, specific to our
example, first '-l' sets '$\' (output separator) to whatever '$/' is
(at this point, it's \n, or a newline).  Then, the '-0' switch is
setting the $/ = 0 ( or null, or nothing!).

OK, next we have '-p' and '-e'.  The '-e' tells Perl to read one line
(the one after the '-e') and use that as the code to process.  The
'-p' causes Perl to assume the following code around your code:

LINE:
  while( <> ) {
    # your code goes here
  } continue {
    print or die "-p destination: $!\n";
  }

So, this is going to process whatever files it finds on your command
line and then print '$_'!

Now, the code that's going into that block is, in our example:
   s/\n/\t/;
   s/\"//g;

So, we get this as the code Perl is running:

LINE:
  while( <> ) {
    s/\n/\t/;      # Change newlines into tabs
    s/\"//g;       # Remove all double-quotes
  } continue {
    print or die "-p destination: $!\n";
  }

but with the special $/ = 0 as the input separator and $\ = \n as the
output separator!

There!  Am I right?  This is fun ... we should do this more often! 
This taught me a lot.

BTW, I found most of these explanations in the 'perldoc perlrun' and
'perldoc perlvar' pages.  You can check out continue blocks with
'perldoc -f continue'.

--Errin

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to