Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: subtractionn in an array

by Rudolf (Monk)
on Mar 21, 2012 at 17:10 UTC ( #960821=note: print w/ replies, xml ) Need Help??


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"; }


Comment on Re: subtractionn in an array
Download Code
Re^2: subtractionn in an array
by Lotus1 (Chaplain) 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.
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://960821]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-12-27 00:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls