#Files - Answer to exercise 3

$start = [0];
$end = [1];

$filename = [2];

open FILE,"<$filename" or die "Cannot read $filename: \n";

#read all of the lines into an array
$i = 0;

while ($line = <FILE>)
{
    $i = $i + 1;
    $lines[$i] = $line;
}

if ($start <= $end)
{
    for ($i = $start; $i <= $end; $i = $i + 1)
    {
        print $lines[$i];
    }
}
else
{
    for ($i = $start; $i >= $end; $i = $i - 1)
    {
        print $lines[$i];
    }
}

Compare with Python


Previous Up Next