Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: About personal function

by atcroft (Abbot)
on Jul 19, 2018 at 05:20 UTC ( #1218802=note: print w/replies, xml ) Need Help??


in reply to About personal function

(First of all, the code you gave throws the error message "Illegal declaration of anonymous subroutine", which would be fixed by removing the & from the sub declaration. Also, if "@test" is changed to "my @test" in line 8, the code will work under "use strict" and "use warnings", both of which may be helpful to you later on.)

When your &test routine is entered, @_ will contain the contents of both @x and @y, flattened into a single array (i.e., @_ contains ( "A", "B", "C", "C", "D", "F", ) ). You, however, are currently assigning to @temp the value of @_[0] (better written as $_[0]), which is the first entry of @_ (or "A"). There are two ways I can see going. The simplest is to change that line to something like @test = @_;, wherein you will get the string "A B C C D F".

If you want to get them as separate arrays, then you can pass them by reference, and then loop through the elements in @test in the loop (example derivation below; the -l switch appends an appropriate EOL for each print statement).

Original code (as 1-liner for testing):

$ perl -le 'my (@x, @y); @x = ( "A", "B", "C" ); @y = ( "C", "D", "F" +); &test( @x, @y, ); sub &test { @test = @_[0]; print "@test\n"; }' Illegal declaration of anonymous subroutine at -e line 1.

Modification to fix "Illegal declaration" error:

$ perl -le 'my (@x, @y); @x = ( "A", "B", "C" ); @y = ( "C", "D", "F" +); &test( @x, @y, ); sub test { @test = @_[0]; print "@test\n"; }' A

Add "strict" and "warnings" (via -M switches):

$ perl -Mstrict -Mwarnings -le 'my (@x, @y); @x = ( "A", "B", "C" ); @ +y = ( "C", "D", "F" ); &test( @x, @y, ); sub test { @test = @_[0]; pr +int "@test\n"; }' Variable "@test" is not imported at -e line 1. Possible unintended interpolation of @test in string at -e line 1. Variable "@test" is not imported at -e line 1. Global symbol "@test" requires explicit package name (did you forget t +o declare "my @test"?) at -e line 1. Global symbol "@test" requires explicit package name (did you forget t +o declare "my @test"?) at -e line 1. Execution of -e aborted due to compilation errors.

Fix errors from adding "strict" and "warnings":

$ perl -Mstrict -Mwarnings -le 'my (@x, @y); @x = ( "A", "B", "C" ); @ +y = ( "C", "D", "F" ); &test( @x, @y, ); sub test { my @test = @_[0]; + print "@test\n"; }' Scalar value @_[0] better written as $_[0] at -e line 1. A

Fix "Scalar value better written as" warning:

$ perl -Mstrict -Mwarnings -le 'my (@x, @y); @x = ( "A", "B", "C" ); @ +y = ( "C", "D", "F" ); &test( @x, @y, ); sub test { my @test = $_[0]; + print "@test\n"; }' A

Fix to display all of @x and @y, instead of the first element only:

$ perl -Mstrict -Mwarnings -le 'my (@x, @y); @x = ( "A", "B", "C" ); @ +y = ( "C", "D", "F" ); &test( @x, @y, ); sub test { my @test = @_; pr +int "@test\n"; }' A B C C D F

Changing to pass @x and @y in by reference (will stringify the reference-NOT what you wanted):

$ perl -Mstrict -Mwarnings -le 'my (@x, @y); @x = ( "A", "B", "C" ); @ +y = ( "C", "D", "F" ); &test( \@x, \@y, ); sub test { my @test = @_; +print "@test\n"; }' ARRAY(0x20071958) ARRAY(0x200718e0)

Turn stringified references into one line per array:

$ perl -Mstrict -Mwarnings -le 'my (@x, @y); @x = ( "A", "B", "C" ); @ +y = ( "C", "D", "F" ); &test( \@x, \@y, ); sub test { my @test = @_; +foreach my $arr ( @test ) { print "@{$arr}"; } }' A B C C D F

Hope that helps.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2020-10-01 16:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (16 votes). Check out past polls.

    Notices?