From: Peng Yu on
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
From: Ralph Malph on
On 6/8/2010 12:14 PM, 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>;
This will store the lines in the
cleverly named array @lines.
I've set $limit to 3 as in your short example
but obviously this works for any limit.
Always be sure to double check any user-inputed values!
--------------------------------
my @lines;
my $limit=3;
my $counter=0;
while($counter < $limit){
$lines[$counter] = <IN> ;
$counter++;
}
From: Mart van de Wege on
Ralph Malph <ralph(a)happydays.com> writes:

> On 6/8/2010 12:14 PM, 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>;
> This will store the lines in the
> cleverly named array @lines.
> I've set $limit to 3 as in your short example
> but obviously this works for any limit.
> Always be sure to double check any user-inputed values!
> --------------------------------
> my @lines;
> my $limit=3;
> my $counter=0;
> while($counter < $limit){
> $lines[$counter] = <IN> ;
> $counter++;
> }

or:

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.

--
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.
From: Mart van de Wege on
Mart van de Wege <mvdwege(a)mail.com> writes:

> Ralph Malph <ralph(a)happydays.com> writes:
>
>> On 6/8/2010 12:14 PM, 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>;
>> This will store the lines in the
>> cleverly named array @lines.
>> I've set $limit to 3 as in your short example
>> but obviously this works for any limit.
>> Always be sure to double check any user-inputed values!
>> --------------------------------
>> my @lines;
>> my $limit=3;
>> my $counter=0;
>> while($counter < $limit){
>> $lines[$counter] = <IN> ;
>> $counter++;
>> }
>
> or:
>
> 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>
}
--
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.
From: Ralph Malph on
On 6/8/2010 1:14 PM, Mart van de Wege wrote:
> Mart van de Wege<mvdwege(a)mail.com> writes:
>
>> Ralph Malph<ralph(a)happydays.com> writes:
>>
>>> On 6/8/2010 12:14 PM, 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>;
>>> This will store the lines in the
>>> cleverly named array @lines.
>>> I've set $limit to 3 as in your short example
>>> but obviously this works for any limit.
>>> Always be sure to double check any user-inputed values!
>>> --------------------------------
>>> my @lines;
>>> my $limit=3;
>>> my $counter=0;
>>> while($counter< $limit){
>>> $lines[$counter] =<IN> ;
>>> $counter++;
>>> }
>>
>> or:
>>
>> 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