Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Dear monks,

I'm looking for a perl idiom for switching adjacent array elements with each other, e.g., to go from

    1  2  3  4  5  6  7  8

to

    2  1  4  3  6  5  8  7

Every way that I've done this before has been rather grotty. My main interest is in having something that avoids using indexing; this is not only for personal preference, but also because my fellow users will also have to maintain what I give them and these users don't "do" indexing well. (Heck, *I* don't do indexing well.)

The main constraint is that order is important; these are Y,X pairs being transformed into X,Y pairs, and these pairs have to maintain relative order. Thus the naked reverse() call would not be appropriate. However, it is checked beforehand that there is an even number of elements in the array, so a clever use of reverse() might work.... Also there is no uniqueness for the Y values, so transforming to a hash and back has even more potential pitfalls than reverse().

And there is a second constraint: No CPAN. The corporate IT support for perl is "perl only" and is always a few years out-of-date. Thus natatime() is out of consideration :-(

A simple example of code that I currently write would be something like

@foo = (1 .. 8); @bar = map { $_%2 ? $foo[$_] : $foo[$_-2] } 1 .. @foo;

But I can't help feeling that there is some perlism using reverse() and/or splice() and/or shift() and/or ??? that would be more in line with the concept of a whole array function. My Google-fu let me down and I'm in enough of a pique to ask this question even though I know that most people would tell me I have an acceptable method at hand.

Apologies: The above code snippet is untested since I am writing this on a machine with no perl installed.

Your grateful novice


In reply to Switch the odd/even elements of an array by jaredor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-25 20:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found