(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.


In reply to Re: About personal function by atcroft
in thread About personal function by dideod.yang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.