Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
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; }

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?
[Your Mother]: Don't think so.
[Your Mother]: Windows doesn't even recognize them as far as that goes.
[Lady_Aleena]: Your Mother, thanks. I had a feeling it would be no.
[Your Mother]: :P
[Lady_Aleena]: Your Mother, it is a good thing I am no on Windows.
[Lady_Aleena]: s/no/not/;
[Lady_Aleena]: My blasted web host will not put perl 5.22 in /usr/bin/perl, it is in /usr/local/cpanel/ 3rdparty/bin/perl
Lady_Aleena has been banging her head against this wall for years now with no success on breaking the wall.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2017-04-28 06:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (519 votes). Check out past polls.