Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Using Splice with Two Arrays within a loop

by moritz (Cardinal)
on Jun 10, 2013 at 15:45 UTC ( #1038102=note: print w/replies, xml ) Need Help??

in reply to Using Splice with Two Arrays within a loop

I don't understand your question. First you say I need to remove a specified number of elements from the @_ array at once rather than the one-at-a-time that shift does, which your code does with splice.

But then you give it the same input as before, and expect the same outcome, even though you changed the code. Why did you even change the code if you don't want it to change? And how do you expect you will ever get more than two elements into @merge_list, when all you do is to create a new variable and assign to elements to it?

Replies are listed 'Best First'.
Re^2: Using Splice with Two Arrays within a loop
by Anonymous Monk on Jun 10, 2013 at 17:03 UTC
    Sorry for not being clear the issue is that the first part of the code is the original code that I have to modify to work using Splice. Since I am a noobie with Perl the bottom code is my attempt which is not working. I need to use splice to make this work as part of my assignment.

      Here is the splice version. Make sure you understand what's going on before you submit to whomever... (there is a twist...)

      #!/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; }
Re^2: Using Splice with Two Arrays within a loop
by Anonymous Monk on Jun 10, 2013 at 17:29 UTC
    Applogies: I have a assignment as follows: "This program can be made both shorter and faster by using a built-in function named splice that will remove a specified number of elements from the @_ array at once, rather than the one-at-a-time that shift does." Using the existing code I need to change it to use splice and have the same exact output. As you can see it is not working. I need some help in this. Thanks

      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; }
        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?
        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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1038102]
[atcroft]: stevieb: Also, one other odd question... is it possible to set it up so that all the versions installed can use a single directory that they pull module sources into, rather than duplicating files? (Am I making sense, or do I have too high a CBC
[atcroft]: (caffeine-blood content) atm?)
[atcroft]: .oO(Interesting... I just went to http://blogs.perl. org/, did a search for "Mars", and tried to go to the second page of results, and received instead a "Request-URI Too Large" message.... odd.)

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2017-04-29 05:16 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (531 votes). Check out past polls.