Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: Add new data to array (updated)

by AnomalousMonk (Bishop)
on Mar 17, 2020 at 20:40 UTC ( #11114398=note: print w/replies, xml ) Need Help??


in reply to Re^2: Add new data to array
in thread Add new data to array

WRT this code, because  $add_ins[0] is a hash reference (@add_ins is an array of hash references) and so  $add_in would become (by autovivification) a reference to an array holding a single element: a hash reference. The expression  $add_in->{name} would then have to become  $add_in->[0]{name} which would be possible, but a useless complication.

Update 1: Normally, a statement like
    my @$add_in = $add_ins[0];
would be written
    my @$add_in = ($add_ins[0]);
using parentheses to make the RHS a proper list, but in the special case of single-element array assignment, Perl allows the parentheses to be dispensed with. In
    my $add_in = ();
the parentheses have no effect; the statement is equivalent to
    my $add_in;

Update 2: BillKSmith covers a point which I neglected to mention: the statements
    my $add_in = ();
    my @$add_in = $add_ins[0];
are syntactically incorrect due to the use of the my declarator in the second statement. However, if this operator is removed, what's left is code that can be made to compile and to run without warnings and to work, albeit IMHO in an ugly, confusing and inefficient manner as discussed above.

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @add_ins = ({ qw(foo bar) }, { qw(biz boz) }); ;; my $add_in = (); @$add_in = $add_ins[0]; dd $add_in; print $add_in->[0]{'foo'} " [{ foo => "bar" }] bar


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2020-05-26 18:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (150 votes). Check out past polls.

    Notices?