[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Loop question



> I need to change it so
> it only prints out the first 8 lines of the array. How would I change this to
> do that?
> 

Easy. Just change this:

> foreach $line (@whole) {
> $line =~ s/\n//g;
> print "$line\n";
> }

to this:

foreach $line(@whole) {
    last if ++$counter == 8;
    $line =~ s/\n//g;
    print "$line\n";
}

Or for simplicity:

for(@whole) {
    # $_ is locally set to the current line ($line in above sample)
    last if ++$counter == 8;
    s/\n//g;
    print "$_\n";
}

Hope that helps.

Regards,
David


# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org