arun10427 has asked for the wisdom of the Perl Monks concerning the following question:

I am a perl newbie. I am using a perl code to read a column full of data from 1 field into an array
for(my $i=2;$i<table->last_record;$i++) my @data = $table->get_record($i,"Date");
Now for example if my @data[1] consists of list of numbers, I need to find the diff between current record and prev record till it reaches the end of array.. Perl has such complex way of array accessing, I donno how to do $data[$i]-$data[$i-1].

Replies are listed 'Best First'.
Re: Perl array access in a for loop
by GrandFather (Saint) on Oct 24, 2009 at 20:11 UTC

    Actually it's just:

    $data[$i] - $data[$i - 1]

    What though have you actually tried? What error message or behaviour is confusing you? Can you put together a sample something like the following code that demonstrates the problem you have?

    use strict; use warnings; my @table = (1, 2, 4, 6, 10); my @diffs; for my $index (0 .. $#table - 1) { $diffs[$index] = $table[$index + 1] - $table[$index]; } print "@diffs\n";

    True laziness is hard work