Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

subtractionn in an array

by brachysclereid (Initiate)
on Mar 21, 2012 at 16:38 UTC ( #960810=perlquestion: print w/replies, xml ) Need Help??
brachysclereid has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am new to perl. I am trying to write a simple script that will pass through an array subtracting the value of the an element from that of the next and so on. Reporting each answer on a new line. One of my many problems is that I am not sure how to refer to the n+1 element in an array: Here what I have so far:

#!/usr/bin/perl use warnings; open(FILE, "filename.txt") or die("Unable to open file"); @List = <FILE>; close(FILE); foreach $line (@List) { print ($line+1) - $line); print "\n"; }
Any help would be much appreciated! Thanks, Art

Replies are listed 'Best First'.
Re: subtractionn in an array
by roboticus (Chancellor) on Mar 21, 2012 at 16:51 UTC

    brackysclereid:

    my $first_item = $List[0]; my $fifth_item = $List[4]; print $fifth_item-$first_item, "\n";

    Update:

    for my $idx = (1 .. $#List) { my $val = $List[$idx] - $List[$idx-1]; print "List[n] - List[n-1] = ", $val, " for n=", $idx, "\n"; }

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: subtractionn in an array
by RichardK (Parson) on Mar 21, 2012 at 16:50 UTC

    Have a look at the section on array variables in perlintro and it will explain how to get the value of an array element, and lots of other things too ;)

Re: subtractionn in an array
by Rudolf (Pilgrim) on Mar 21, 2012 at 17:10 UTC
    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"; }

      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

      # 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: subtractionn in an array
by GrandFather (Sage) on Mar 21, 2012 at 21:04 UTC

    I strongly recommend you add use strict; to every script you write. Also you should always use the three parameter version of open and lexical file handles (a variable declared with my): open my $fileIn, '<', 'filename.txt' or die "Unable to open filename.txt: $!\n".

    There are lots of ways to solve your problem and some of them don't need an array at all. Consider:

    use strict; use warnings; my $lastValue = <DATA>; chomp $lastValue; while (<DATA>) { chomp; print $_ - $lastValue, "\n"; $lastValue = $_; } __DATA__ 1 76 34 6 12 5

    Prints:

    75 -42 -28 6 -7

    Use perldoc to look up chomp and defined if you aren't familiar with them.

    True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://960810]
Approved by marto
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2018-07-19 21:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (420 votes). Check out past polls.

    Notices?