Prev: FAQ 8.3 How do I do fancy stuff with the keyboard/screen/mouse?
Next: convert integer to string
From: Ben Morrow on 10 Jun 2010 17:11 Quoth Mart van de Wege <mvdwege(a)mail.com>: > Why it is necessary here is because the above statement would normally > evaluate <> in list context. I'm a bit unclear if that is because of the > array assignment or the map function. I believe it is the map function > forcing a list context here, but I could be wrong. Even if I am, > assigning to an array definitely forces list context. map always evaluates its first argument (EXPR or BLOCK) in list context. This makes it possible to change the length of the returned list. Ben
From: Dr.Ruud on 11 Jun 2010 19:15 Peng Yu wrote: > I want to give a given number of lines. Current, I have to write the > following code to read, for example, 3 lines. Is there a subroutine to > read a given number of lines in an array? > > $line1=<IN>; > $line2=<IN>; > $line3=<IN>; $ echo -e 'a\nb\nc\nd\ne\n' | perl -wle' sub get_lines { my ( $fh, $max, $aref )= @_; my $count; while ( ++$count <= $max and defined( my $line= <$fh> ) ) { push @$aref, $line; } return $count; } my @lines; get_lines +STDIN, 2, \@lines; print for @lines; ' a b -- Ruud
First
|
Prev
|
Pages: 1 2 3 4 5 Prev: FAQ 8.3 How do I do fancy stuff with the keyboard/screen/mouse? Next: convert integer to string |