Prev: FAQ 8.3 How do I do fancy stuff with the keyboard/screen/mouse?
Next: convert integer to string
From: Martijn Lievaart on 9 Jun 2010 05:06 On Wed, 09 Jun 2010 04:17:33 -0400, Uri Guttman wrote: >>>>>> "b" == bankair <bankair(a)gmail.com> writes: > > b> On Jun 8, 6:14 pm, Peng Yu <pengyu...(a)gmail.com> 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>; > > b> Hi, > > b> May be the special var "$." (line NBR of the last read file handle) > b> could help : > b> my @lines; > b> while (<>) { push @lines,$_ if($.<4) } > > and what happens to the lines after the third one? that will read to eof > which is likely not wanted. > > better to invert the loop: > > while ( $. < 4 ) { > push @lines, scalar <> ; > } > > or for those lovers of one liners: > > push @lines, scalar <> while $. < 4 ; Those have the potential of an infinite loop when the file has less than 3 lines. Besides, even when fixed, the 'push @lines, scalar <>' pushes undef at eof, so I very much doubt this is what you want. So I guess the best option (assuming you want to exit early at eof, not push undefs): while (<>) { last if ($.>=4) push @lines,$_; } Eventually followed by push @lines, undef for ($. .. 3); If you do want undefs for missing lines. M4
From: Justin C on 9 Jun 2010 07:23 On 2010-06-08, Peng Yu <pengyu.ut(a)gmail.com> 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>; > > Regards, > Peng The variable $. holds the current line number for the last filehandle accessed. It may be useful to you, see perldoc perlvar for details. Justin. -- Justin C, by the sea.
From: Peng Yu on 9 Jun 2010 11:48 On Jun 8, 3:58 pm, Marc Girod <marc.gi...(a)gmail.com> wrote: > On Jun 8, 9:14 pm, Willem <wil...(a)turtle.stack.nl> wrote: > > > You mean like: for (0..2) { $lines[$_] = <IN> } > > I'am not smart enough to not try being clever. > Besides (and for wrong reasons: I used to like Lisp), I like maps. > So, I was thinking of the following minimal(?) fix(?): > > my @lines = map scalar <IN>, 1..3; I don't understand why 'scalar' has to be used. Would you please let me know where I should read in man page to understand file handle in scalar and list context.
From: Marc Girod on 9 Jun 2010 13:17 On Jun 9, 4:48 pm, Peng Yu <pengyu...(a)gmail.com> wrote: > I don't understand why 'scalar' has to be used. Would you please let > me know where I should read in man page to understand file handle in > scalar and list context. I/O Operators in perlop? In scalar context, evaluating a filehandle in angle brackets yields the next line from that file (the newline, if any, included), or "undef" at end-of-file or on error. .... If a <FILEHANDLE> is used in a context that is looking for a list, a list comprising all input lines is returned, one line per list element. and then, of course, scalar in perlfunc... scalar EXPR Forces EXPR to be interpreted in scalar context and returns the value of EXPR. Marc
From: Mart van de Wege on 10 Jun 2010 16:53 Peng Yu <pengyu.ut(a)gmail.com> writes: > On Jun 8, 3:58 pm, Marc Girod <marc.gi...(a)gmail.com> wrote: >> On Jun 8, 9:14 pm, Willem <wil...(a)turtle.stack.nl> wrote: >> >> > You mean like: for (0..2) { $lines[$_] = <IN> } >> >> I'am not smart enough to not try being clever. >> Besides (and for wrong reasons: I used to like Lisp), I like maps. >> So, I was thinking of the following minimal(?) fix(?): >> >> my @lines = map scalar <IN>, 1..3; > > I don't understand why 'scalar' has to be used. Would you please let > me know where I should read in man page to understand file handle in > scalar and list context. As Marc pointed out, using the readline operator <> in scalar context reads only one line. 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. Mart -- "We will need a longer wall when the revolution comes." --- AJS, quoting an uncertain source.
First
|
Prev
|
Next
|
Last
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 |