From: Uri Guttman on
>>>>> "BM" == Ben Morrow <ben(a)morrow.me.uk> writes:

BM> Quoth Willem <willem(a)turtle.stack.nl>:
>> Ben Morrow wrote:
>> ) 'They' in this case being me? I wasn't trying to be clever, I was trying
>> ) to write the code in a simple and obvious way. Counting indices by hand
>> ) is much easier to get wrong than letting perl count them for you.
>>
>> You mean like: for (0..2) { $lines[$_] = <IN> }

BM> Mmm, I suppose. I think my problem with that is that I just dislike
BM> array indices other than [0] and [-1]: I think of arrays as 'frozen
BM> lists', so either you iterate over the whole thing or you attack it from
BM> the ends.

amusing viewpoint but what about random accesses, direct accesses (it
happens), splicing (both inserts and deletes), etc. true, i do what you
say most of the time but i still keep those other things in my
toolbox. :)

uri

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: Mart van de Wege on
Ralph Malph <ralph(a)happydays.com> writes:

>>> my @lines;
>>> my $limit = 3;
>>> for my $line(1..$limit) {
>>> $lines[$line] =<IN>
>>> }
>>>
>>> But then again I have a personal dislike of counters and flags, so I
>>> tend to look for ways to avoid using them.
>>
>> And that was an off-by-one error. Saw it as I hit 'post'.
>>
>> So the correct code would of course be:
>>
>> my @lines;
>> my $limit = 3;
>> for my $line(0..$limit-1) {
>> $lines[$line] =<IN>
>> }
> In some senses you original code was correct although
> it didn't fill $lines[0].
> As required, it read a certain number of lines into an array.
> Nonetheless, I suggest you start disliking silly errors
> more than you dislike counters and flags.
> HTH

You know, if you want your advice to be taken seriously, it would be
better not to behave like a complete twit.

Mart


--
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.
From: bankair on
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>;

Hi,

May be the special var "$." (line NBR of the last read file handle)
could help :
my @lines;
while (<>) { push @lines,$_ if($.<4) }

Regards,
Alexandre
From: Uri Guttman on
>>>>> "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 ;

uri

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: bankair on
On Jun 9, 10:17 am, "Uri Guttman" <u...(a)StemSystems.com> wrote:
> >>>>> "b" == bankair  <bank...(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 ;

Ah ! Your "scalar <>" (thank you) gave me another idea :

my @lines = grep {defined $_} map {scalar <>} (1..3);