Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Using Splice with Two Arrays within a loop

by hdb (Prior)
on Jun 10, 2013 at 17:42 UTC ( #1038128=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Using Splice with Two Arrays within a loop
in thread Using Splice with Two Arrays within a loop

Be very careful when you ask for "short" code on this website, you might get what you ask for ... ;) like this (meeting your requirement to use splice):

sub interleave_words { my @results = splice @_, shift; splice @results, 2*$_, 0, shift for 0..@results-1; return @results; }


Comment on Re^3: Using Splice with Two Arrays within a loop
Download Code
Replies are listed 'Best First'.
Re^4: Using Splice with Two Arrays within a loop
by Anonymous Monk on Jun 10, 2013 at 20:16 UTC
    Hi thanks for the process however I noticed that the die statement is no longer in the code. I need to include the statement. How do I include that without it printing the die statement instead of the answer I wanted which was Result: Can you unlock the secret code?
Re^4: Using Splice with Two Arrays within a loop
by Anonymous Monk on Jun 10, 2013 at 20:42 UTC
    Thanks for the help. Question I still require to keep the die statement in the code however not sure how I put it in without the die statement printing out instead of the correct answer. Thanks for any input

      Now with death build in.

      sub interleave_words { my @results = splice @_, shift; die "Arrays of different size\n" unless @results == @_; splice @results, 2*$_, 0, shift for 0..@results-1; return @results; }
        Thanks for the response however as I mentioned I am new to this so where would I add the code you gave in the following script:
        #!/usr/bin/perl use strict; use warnings; my @first = qw(Can unlock secret); my @second = qw(you the code?); my @mixed = interleave_words( scalar(@first), @first, @second ); print "Result: @mixed\n"; sub interleave_words { my $count = shift; my @results = splice @_, $count; foreach my $index ( 0 .. $count-1 ) { splice @results, 2*$index, 0, shift; } return @results; }
        Again many thanks

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2015-07-30 02:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls