Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: unshift single scalar

by Not_a_Number (Prior)
on Aug 09, 2014 at 18:37 UTC ( [id://1096856]=note: print w/replies, xml ) Need Help??


in reply to unshift single scalar

If I understand correctly, you want:

unshift (@output, "Top");

The array that you want to alter should be the first argument to unshift.

Update: Added explanation.

Replies are listed 'Best First'.
Re^2: unshift single scalar
by Bodger (Acolyte) on Aug 09, 2014 at 18:47 UTC

    I saw that in the docs but it did not explain what it was, but it says this:

    Starting with Perl 5.14, unshift can take a scalar EXPR, which must hold a reference to an unblessed array. The argument will be dereferenced automatically. This aspect of unshift is considered highly experimental. The exact behaviour may change in a future version of Perl.

    But that is a winner, it works.

      No. When the docs say 'unshift can take a scalar EXPR', they mean as the first argument, eg:

      my $ref = [ 2, 3 ,4 ]; unshift $ref, 1;

      But that's probably too much information. What you need to understand is that the standard (non-experimental) syntax for unshift is:

      unshift ARRAY,LIST

      It so happens (as is often the case) that the LIST that you want to add at the beginning of your ARRAY consists of just one element (namely 'Top').

      Maybe the doc for unshift could benefit from a less obscure example of usage?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1096856]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-29 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found