in reply to subtractionn in an array

use strict; my @List; open(FILE, "filename.txt") or die "Unable to open file\n"; # assuming numbers, get them from file and add them to your array while(<FILE>){ #each time through the loop, line is held in $_ chomp $_; #remove any newline from the line taken from file push(@List,$_); #push line into the array, in your case the number +you got } close(FILE); # to loop through the array, I found it usefull to increment my count +var ($i) by two # here, scalar(@List), returns the number of elements in the array. for(my $i=0;$i < scalar(@List);$i+=2){ my $answer = ($List[$i+1]-$List[$i]); #use array indexing for your +calculation print "$answer\n"; }

Replies are listed 'Best First'.
Re^2: subtractionn in an array
by brachysclereid (Initiate) on Mar 21, 2012 at 17:57 UTC

    Thanks for all the excellent information! I have learned more about array manipulation in the last hour than in the last day! I really appreciate the well annotated code!

    #!/usr/bin/perl print "newbie thanks you all! \n"
    Cheers!, Art

Re^2: subtractionn in an array
by Lotus1 (Vicar) on Mar 21, 2012 at 17:48 UTC
    # to loop through the array, I found it usefull to increment my count +var ($i) by two # here, scalar(@List), returns the number of elements in the array. for(my $i=0;$i < scalar(@List);$i+=2){

    Why increment by two?

      Lotus1: can't remember why I did, hah, but maybe I misunderstood what he was trying to do.