Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
P is for Practical
 
PerlMonks  

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

by hdb (Parson)
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
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 browsing the Monastery: (9)
As of 2014-04-19 03:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (477 votes), past polls