Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^4: Perl hex substraction

by Anonyrnous Monk (Hermit)
on Feb 15, 2011 at 13:59 UTC ( #888243=note: print w/ replies, xml ) Need Help??

in reply to Re^3: Perl hex substraction
in thread Perl hex substraction

One way is to iterate over all values and always store the current value, so it becomes the previous value in the next iteration. This way you have both values available at the same time.

The 'tricky' thing is to handle the border case correctly, i.e. when you don't have a previous value, because it's the first iteration... (in the following snippet, this is done by testing if the previous value is defined)

#!/usr/bin/perl -w use strict; my @names = ("Ann","John","Michael","George","Smith"); my $prev_name; for my $name (@names) { print "$prev_name $name\n" if defined $prev_name; $prev_name = $name; }


Ann John John Michael Michael George George Smith

Comment on Re^4: Perl hex substraction
Select or Download Code
Replies are listed 'Best First'.
Re^5: Perl hex substraction
by Fox (Pilgrim) on Feb 15, 2011 at 14:15 UTC
    FWIW, without storing the previous value:
    $ perl -E 'my @a=1..5; for my $i(1..$#a) { say "$a[$i-1], $a[$i]" } ' 1, 2 2, 3 3, 4 4, 5

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2015-10-10 20:58 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (258 votes), past polls