Hi unixhome,

I usually prefer to receive subroutine arguments with:

my ($var1, $var2, $parray) = @_;
because it lets me add or remove arguments easily.  The one trick is that, even with a single parameter, you can't remove the parentheses on the left side of the assignment, because it would use @_ in scalar context instead!

In your original code, you've omitted the '$' sigil on your variables, which you need to supply.

Also, note that you can only pass a single array (as the last argument), because passing more than 1 will cause all arrays to be "flattened" into a single array.  If that isn't what you want (ie. if you need to pass more than 1), you can use references (I usually prepend 'p' to a variable used as a pointer/reference, like as parray):

sub format_output { my ($var1, $var2, $parray1, $parray2) = @_; # One way to iterate through an array foreach (@$parray1) { print "The next item in the array is $_\n"; } # Another way to iterate through an array for (my $i = 0; $i < @$parray2; $i++) { printf "Array item #%d = %s\n", $i+1, $parray2->[$i]; } }

s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re: Help with scalar values and array as arguments to function by liverpole
in thread Help with scalar values and array as arguments to function by unixhome

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.