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 | |
|
Re^2: subtractionn in an array
by Lotus1 (Vicar) on Mar 21, 2012 at 17:48 UTC | |
by Rudolf (Pilgrim) on Mar 21, 2012 at 23:30 UTC |