There's more than one way to do things | |
PerlMonks |
Re: About personal functionby atcroft (Abbot) |
on Jul 19, 2018 at 05:20 UTC ( [id://1218802]=note: print w/replies, xml ) | Need Help?? |
(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):
Modification to fix "Illegal declaration" error:
Add "strict" and "warnings" (via -M switches):
Fix errors from adding "strict" and "warnings":
Fix "Scalar value better written as" warning:
Fix to display all of @x and @y, instead of the first element only:
Changing to pass @x and @y in by reference (will stringify the reference-NOT what you wanted):
Turn stringified references into one line per array:
Hope that helps.
In Section
Seekers of Perl Wisdom
|
|