elusion has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks, I'm not sure this is possible based on nodes I looked at using search where people tried similar things. I have code (push @rows, [ @temp[$one..$two] ];) and it's not giving me the output I want. It's easier to explain in graphical form. I want this:

[ "Whatever", [ "Blah", "Blah" ], "Yeah", "Get the picture" ]

When I'm getting:

[ "Whatever" ], [ [ "Blah", "Blah" ] ], [ "yeah" ], [ "Get the picture" ]

Is there a way I can do what I want to do? It'd save me a lot of effort and code. Thanx

- p u n k k i d
"Reality is merely an illusion, albeit a very persistent one." -Albert Einstein

Replies are listed 'Best First'.
Re: passing elements of an array as a ref to another array
by jeroenes (Priest) on Mar 28, 2001 at 17:28 UTC
    You don't provide code, so we can tell you only little.

    But your description indicates that you have one level of referencing too much. Try

    push @array, $element; #AND push @array, \@another_array;
    Oh yeah, give perlref a good ponder.

    Jeroen
    "We are not alone"(FZ)

Re: passing elements of an array as a ref to another array
by davorg (Chancellor) on Mar 28, 2001 at 17:31 UTC

    I think you want to change it to just:

    push @rows, @temp[$one..$two];

    As this avoids the unwanted extra level of references that you are introducing.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: passing elements of an array as a ref to another array
by arturo (Vicar) on Mar 28, 2001 at 19:40 UTC

    I'm confused a bit; the thing that would help me to understand your question is knowing what's in @temp.

    I take it from your description you want your push to add *one thing* to your array, and that one thing is an anonymous array, the members of which are an array slice of @temp. What you're getting is a list of anonymous arrays.

    So let's make an assumption, shall we? Here's some code that might help illustrate something (what, exactly, it illustrates is left as an exercise for the reader):

    my @temp = ('bob', ['carol', 'ted'], 'alice', ['felicity', 'austin'], +'arturo'); my @rows; push @rows, @temp[1..3]; # @rows is now (['carol', 'ted'], 'alice', ['felicity', 'austin']) # do something with that @rows= (); push @rows, [ @temp[0..2] ]; # @rows is now (['bob', ['carol', 'ted'], 'alice'])

    But a lot could depend on how you made @temp; let me remind you that taking a reference to a literal list gives you a list consisting of the references to the elements of that list.

    my @temp = \('foo', 'bar'); # @temp now (['foo'], ['bar']), not (['foo', 'bar'])

    I don't know that anything like that is going on here, but remember :garbage in, garbage out. Check the input when the output's not what you expect.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor