Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: Perl hex substraction

by inkubus (Initiate)
on Feb 15, 2011 at 13:37 UTC ( [id://888234]=note: print w/replies, xml ) Need Help??


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

I know, but this is not my problem.
Let's assume I have the following array:
@names = ("Ann","John","Michael","George","Smith");
What I want to accomplish is to print them in pairs like:
Ann John John Michael Michael George George Smith
Every object with the next one to it in the list...simple, but I don't know how to do that.

Replies are listed 'Best First'.
Re^4: Perl hex substraction
by rovf (Priest) on Feb 15, 2011 at 13:46 UTC
    Is this a Perl question, or is it that you have never programmed before and start learning to program using Perl, so it's more like a general programming question? From the way you ask, I suppose the latter, but you should make this clear, because you can get better help if we know about your current level of knowledge.

    For instance, Perl offers several ways to iterate over an array, so for a Perl-newbie, I would refer him or her to perlsyn, in particular the sections about looping (because people new to Perl usually don't know which part of the huge documentation contains what they are looking for]. However, if I know that you have never programmed before, this information won't help you, because you don't even know what "looping" means, and we would have to teach you basic skills first.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re^4: Perl hex substraction
by raybies (Chaplain) on Feb 15, 2011 at 13:54 UTC

    sounds to me like you're hung up on how to keep pairs of values together. When you have a list.

    There're a dozen ways to think of this issue.

    One might ask why you have to pair them up at all. If you know the index in the array of the value you're interested in... then the next index is the pair. It's in the list already, why take it appart into pairs.

    or if you needed to pull it apart into pairs (or groups of any arbitrary size (let's say instead of pairs, you had to group them into groups of five numbers at a time), which in some languages is done with multidimensional arrays, or you could create tuples--your own custom data type, using lists of lists, or be clever and concatentate the pairs together using an identifier (like you did with the space), or painstakingly keep track of two lists where you once had one...

    The sky's the limit, each choice will have consequences, but you have to make a choice and try something out.

Re^4: Perl hex substraction
by Anonyrnous Monk (Hermit) on Feb 15, 2011 at 13:59 UTC

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

    Output:

    Ann John John Michael Michael George George Smith
      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
        Hrm.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-03-19 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found