in reply to unshift single scalar

I do not want "push" that puts it at the bottom of the list. I want to put the single scalar value at the top of the list see example.

# What list looks like before I unshift it 0 "Item 1" 1 "Item 2" After I do the unshift (or whatever command). 0 "Top" 1 "Item 1" 2 "Item 2"

I just feel there has to be a more elegant way of doing this.

Replies are listed 'Best First'.
Re^2: unshift single scalar
by Laurent_R (Canon) on Aug 09, 2014 at 20:33 UTC
    The post by Not_a_Number (and other monks) already gave you the solution:
    unshift @output, "Top";
    Quite simple, isn't it?
Re^2: unshift single scalar
by LanX (Saint) on Aug 10, 2014 at 02:18 UTC
    > I do not want "push" that puts it at the bottom of the list.

    That's the code you posted as demonstration of what you want

    DB<108> @array = ( "Top" ); => "Top" DB<109> @output=1..3 => (1, 2, 3) DB<110> unshift (@array, @output); => 4 DB<111> @array => (1, 2, 3, "Top")

    and that's what push does

    DB<112> push @output, "Top" => 4 DB<113> @output => (1, 2, 3, "Top")

    maybe you should check your examples before posting?

    Besides: I've never heard of "bottom" or "top" of lists or even arrays. "Start" and "End" are much better description for chains w/o any vertical orientation.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

      Besides: I've never heard of "bottom" or "top" of lists or even arrays. "Start" and "End" are much better description for chains w/o any vertical orientation.

      It's not so uncommon when one is working with stacks: you push items onto the top of the stack, and pop them off the top again. Since this is where Perl's push and pop got their names from, the terminology is not malapropos, even if it is perhaps uncommon for general arrays and lists.

        But you are aware that now the end of the list is the top of stack (push/pop) , while the OP considers the start as such? :)

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re^2: unshift single scalar
by AnomalousMonk (Archbishop) on Aug 10, 2014 at 00:43 UTC

    Here are some examples that may help drive the point home:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @output = qw(OldTop Second Third); dd \@output; ;; unshift @output, 'NewTop'; dd \@output; ;; my @otherarray = qw(uno dos tres); unshift @output, @otherarray; dd \@output; " ["OldTop", "Second", "Third"] ["NewTop", "OldTop", "Second", "Third"] ["uno", "dos", "tres", "NewTop", "OldTop", "Second", "Third"]

    ... a more elegant way ...

    I don't know if you will consider this more elegant or not:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @output = qw(OldTop Second Third); dd \@output; ;; @output = ('NewTop', @output); dd \@output; " ["OldTop", "Second", "Third"] ["NewTop", "OldTop", "Second", "Third"]
    The disadvantage of this approach is that it requires the construction of a temporary list, which in the case of a large  @output array will be correspondingly large.