Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: splicing two arrays together

by TJPride (Pilgrim)
on Jan 06, 2012 at 05:08 UTC ( #946524=note: print w/ replies, xml ) Need Help??


in reply to splicing two arrays together

use strict; use warnings; my @first = qw(Can unlock secret); my @second = qw(you the code?); splice(@first, $_+1, 0, $second[$_]) for reverse 0..$#second; print "@first\n";

I notice this is fairly close to one of GrandFather's solutions, but that's really because there is only one best way to do this if the requirement is to use splice.


Comment on Re: splicing two arrays together
Download Code
Re^2: splicing two arrays together
by GrandFather (Cardinal) on Jan 06, 2012 at 06:22 UTC

    This is a trivial variant of one of the three different splice based options I provided. So which of the trivial variations is "the one best way" to do it? How is this "the best way" if preserving the content of @first is desired? What non-artificial context would require the use of split?

    Remember an oft quoted virtue of Perl is TIMTOWTDI with the strong implication that there is seldom a "single best solution". Actually even with languages that tout themselves as offering the "one true way" there is seldom a single "best" solution to any problem.

    True laziness is hard work
      Thanks Grandfather for those solutions, the best solution I guest is the one that requires the least amount of typing in any language?
        Well, it depends. The best solution might be the one that runs fastest, uses minimal memory, or the one that can be easily understood for later maintenance.

        I'd say the best solution is the one that works correctly, is maintainable and fast enough. Number of lines or characters of code is generally one of the least important criteria. The most important criteria depends a great deal on the purpose of the code.

        True laziness is hard work
      Actually, it's an independent solution that just happens to be mostly identical to yours. My determination of "best" is that the OP asked for a solution with splice, and this was the best solution possible with splice. Obviously, splice is not the most efficient way to do this overall, but that's the OP's problem, since he framed the question in the context of splice rather than the context of finding the most efficient way to do merge two arrays. I'm guessing it's a homework problem and they said he had to use splice.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-07-28 06:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (191 votes), past polls