Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: simplish script (I think)

by NetWallah (Canon)
on Sep 25, 2020 at 00:33 UTC ( [id://11122182]=note: print w/replies, xml ) Need Help??


in reply to simplish script (I think)

An array element is referenced using SQUARE brackets and a Dollar sigil.

You should be using

print "difference is " . $array[$counter] - $array[$counter - 1];
Your second "foreach" is not necessary.
It can be replaced by:
print "difference is " . $array[$#array] - $array[-2] , "\n";

See correction below

                "Imaginary friends are a sign of a mental disorder if they cause distress, including antisocial behavior. Religion frequently meets that description"

Replies are listed 'Best First'.
Re^2: simplish script (I think)
by jeffenstein (Hermit) on Sep 25, 2020 at 08:10 UTC
    Your second "foreach" is not necessary.

    I think the OP is trying to do this with the second foreach:

    for(my $i=1; $i < @array; $i++){ print "The difference is ", $array[$i] - $array[$i-1], "\n"; }
      You are correct(++) - I misread the curly brackets.

      Another way he can achieve this - arguably more readable is:

      my @a=@array; # make a copy, since we will destroy @a my $prev=pop @a; for (reverse @a){ print $prev - $_, "\n"; $prev=$_; }

                      "Imaginary friends are a sign of a mental disorder if they cause distress, including antisocial behavior. Religion frequently meets that description"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-03-19 02:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found