develooper Front page | perl.fwp | Postings from December 2001

Re: Santa Hole 1 (head.pl) Post Mortem

Thread Previous | Thread Next
From:
Jeff 'japhy' Pinyan
Date:
December 6, 2001 17:23
Subject:
Re: Santa Hole 1 (head.pl) Post Mortem
Message ID:
Pine.GSO.4.21.0112062021360.14590-100000@crusoe.crusoe.net
On Dec 6, aaron@marasco.com said:

>> #!/usr/bin/perl -p
>> $_ x=$.<11
>
>Can this one be explained please? (And can we keep explanations in the
>per-hole threads?)

I was using this too, until I reached the age of reason. ;)

I learned the trick from Larry Wall (indirectly).  He had the $_ x= N
trick in some code I saw, and I thought it brilliant.

  # for brevity, but this is technically incorrect
  while (<>) {
    $_ = $_ x ($. < 11);
    print;
  }

$. < 11 is true until $. >= 11, so $. < 11 returns 1 until then, so we get

  $_ = $_ x 1;  # or just $_

Once $. >= 11, we get:

  $_ = $_ x '';  # or '';

-- 
Jeff "japhy" Pinyan      japhy@pobox.com      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About