[Omaha.pm] Some lines of a file

James Harr jharr at ist.unomaha.edu
Fri Oct 31 08:45:36 PDT 2008


Sticking with the tradition of timtowtdi:

@in = <>;
print splice(@in, 2, @in - 6);

... or ...

<> foreach 0 .. 1;
push @buf, scalar(<>) foreach 0 .. 5;
while(<>)
{
  push @buf, $_; 
  print shift @buf;
}

... continuing on the path to insanity through inverted loops ...

<> foreach 0 .. 1;
push @buf, scalar(<>) foreach 0 .. 5;
sub {push @buf, $_; print shift @buf}->() while <>;

I shouldn't be allowed to code perl.

http://forums.somethingawful.com/showthread.php?threadid=2952520 - Programming languages personified, see perl (I'd have ad-block if I were you). 


-------------------
From: omaha-pm-bounces+jharr=ist.unomaha.edu at pm.org [mailto:omaha-pm-bounces+jharr=ist.unomaha.edu at pm.org] On Behalf Of Robert Fulkerson
Sent: Saturday, October 25, 2008 19:51
To: Perl Mongers of Omaha, Nebraska USA
Subject: Re: [Omaha.pm] Some lines of a file

Y'know, I should be grabbing some of these ideas for quiz and exam questions.  :)  That's certainly the oddball kind of thing one of us goofball instructors would ask on an exam.  ;)

-- b
On Sat, Oct 25, 2008 at 7:37 AM, Jay Hannah <jay at jays.net> wrote:
A friend of mine asked me for this one yesterday.


Problem:

 Given STDIN print everything except the first 2 lines and last 4 lines.


Solution:

 while (<>) {
    push @in, $_;
 }
 print splice(@in, 2, @in - 6);


Note this is a terrible solution with large files (memory hog), but works fine on small STDIN.

Cheers,

j



$ cat j
one
two
three
four
five
six
seven
eight
nine
ten
$ cat j | perl j2.pl
three
four
five
six


_______________________________________________
Omaha-pm mailing list
Omaha-pm at pm.org
http://mail.pm.org/mailman/listinfo/omaha-pm



More information about the Omaha-pm mailing list