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(){ #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"; }