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

Assign valuse of array within array to variables

by g_speran (Beadle)
on Feb 23, 2021 at 01:29 UTC ( #11128675=perlquestion: print w/replies, xml ) Need Help??

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

Good Evening Perl Monks, How can one assign the contents of an array within a multi-dimensional array to a set of variables passed to a subroutine?

use warnings; use Data::Dumper; my @arr; $arr[0][4] = (["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) = @{$_}; print "Var5: $VAR5\tVar6:$VAR6\tVar7: $VAR7\tVar8: $VAR8\n"; }
OUTPUT ================= main::Testing() called too early to check prototype at C:\temp\arr_ref +ernce.pl line 11. $VAR1 = [ undef, undef, undef, undef, [ 'S', 'M', 'R', 'B' ] ]; Var1: S Var2: M Var3: R Var4: B Use of uninitialized value $_ in array dereference at C:\temp\arr_refe +rnce.pl line 14. Use of uninitialized value $VAR5 in concatenation (.) or string at C:\ +temp\arr_refernce.pl line 15. Use of uninitialized value $VAR6 in concatenation (.) or string at C:\ +temp\arr_refernce.pl line 15. Use of uninitialized value $VAR7 in concatenation (.) or string at C:\ +temp\arr_refernce.pl line 15. Use of uninitialized value $VAR8 in concatenation (.) or string at C:\ +temp\arr_refernce.pl line 15. Var5: Var6: Var7: Var8:
Expecting output ============== Var1: S Var2: M Var3: R Var4: B Var5: S Var6: M Var7: R Var8: B

Replies are listed 'Best First'.
Re: Assign valuse of array within array to variables
by LanX (Cardinal) on Feb 23, 2021 at 01:48 UTC
    try

    sub Testing { my ($VAR5,$VAR6,$VAR7,$VAR8) = @{$_[0]}; print "Var5: $VAR5\tVar6:$VAR6\tVar7: $VAR7\tVar8: $VAR8\n"; }

    $_[0] is the first element of the array @_

    $_ is something completely different

    > main::Testing() called too early to check prototype at C:\temp\arr_refernce.pl line 11.

    Do

    sub Testing {

    not

    sub Testing (){

    if you want to pass arguments.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Thank you kindly!
Re: Assign valuse of array within array to variables
by AnomalousMonk (Bishop) on Feb 23, 2021 at 04:43 UTC
Re: Assign valuse of array within array to variables
by BillKSmith (Prior) on Feb 23, 2021 at 21:15 UTC
    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: perlquestion [id://11128675]
Approved by LanX
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2021-05-13 04:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (134 votes). Check out past polls.

    Notices?