Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Assign valuse of array within array to variables

by BillKSmith (Prior)
on Feb 23, 2021 at 21:15 UTC ( #11128719=note: print w/replies, xml ) Need Help??


in reply to Assign valuse of array within array to variables

The data structure that you create is probably not what you think. A lot goes on behind the scene in your initialization. I have expanded the initialization to more clearly show the same structure. The variable "@arr" is an array with only one element. That element is a reference to an anonymous array with five elements (The first four are undefined). The last element is a reference to an anonymous array with four elements (Your data).

Dumper requires scalar arguments. You have provided an array. Perl expands this to a list with only one element (which is a reference). Dumper displays the object of that reference. The argument to Dumper should be a reference to @arr to force Dumper to parse the entire structure.

I am not going to tell you how to get rid of the message about prototypes (You may be tempted to use them). Remove the prototype "()" from your subroutine definition.

You are passing a single argument to the subroutine. That argument is a reference to an anonymous array. A subroutine finds its arguments in the array @_. You only have one argument so it is in $_[0]. You must dereference this to get your values.

use warnings; use Data::Dumper; my @array; @arr = ( [ undef, undef, undef, undef, ['S', 'M', 'R', 'B'], ] ); print Dumper \@arr; my ($VAR1,$VAR2,$VAR3,$VAR4) = @{$arr[0][4]}; print "Var1: $VAR1\tVar2: $VAR2\tVar3: $VAR3\tVar4: $VAR4\n"; Testing($arr[0][4]); sub Testing { my ($VAR5,$VAR6,$VAR7,$VAR8) = @{$_[0]}; print "Var5: $VAR5\tVar6: $VAR6\tVar7: $VAR7\tVar8: $VAR8\n"; }

OUTPUT:

$VAR1 = [ [ undef, undef, undef, undef, [ 'S', 'M', 'R', 'B' ] ] ]; Var1: S Var2: M Var3: R Var4: B Var5: S Var6: M Var7: R Var8: B
Bill

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2021-04-12 18:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?