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

I am working with a binary file. At a particular point, I am calling a function with three values as arguments. The arguments are: a ":", a number 0, and an array (just a portion of the array, 6 bytes starting at offset 8). Am I allowed to do this? If I am allowed, how do I accomplish it? Also, how do I refer to the argument (contents of array) from within the function? Below is what I tried with no success.
sub format_output { my var1 = $_[0]; my var2 = $_[1]; my array = $_[2]; do_some_stff } my @array = 0x00; @tmparray = split('',$scalar); @array = splice(@tmparray, 8, 6); &format_address(":", 0, @array);
Any pointers will be greatly appreciated.

Replies are listed 'Best First'.
Re: Help with scalar values and array as arguments to function
by davorg (Chancellor) on Jun 14, 2006 at 15:26 UTC
    sub format_output { my ($var1, $var2, @array) = @_; # do_some_stff } format_address(":", 0, (split(//, $scalar))[8 .. 13]);
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Help with scalar values and array as arguments to function
by CountOrlok (Friar) on Jun 14, 2006 at 15:19 UTC
    This is one of many ways to do it:
    sub format_output { my $var1 = shift; my $var2 = shift; my @arrayref = @_; do_some_stuff; } my $scalar = 'abcdefghijklmnopq'; my @array = (); my @tmparray = split('',$scalar); @array = splice(@tmparray, 8, 6); &format_address(":", 0, @array);
    -imran
Re: Help with scalar values and array as arguments to function
by liverpole (Monsignor) on Jun 14, 2006 at 15:31 UTC
    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$..$/
Re: Help with scalar values and array as arguments to function
by Zaxo (Archbishop) on Jun 14, 2006 at 15:23 UTC

    Is your third argument a perl array, or are you thinking, C-like, of strings as arrays of characters?

    For a perl array, you can pass an array slice,

    format_address(':', 0, @array[8..13]);

    To get an array of six single characters from position eight in a string,

    my @array = split //, substr($scalar, 8, 6);
    With Perl's string handling, it might be better to write the function to simply take a string.

    After Compline,
    Zaxo

Re: Help with scalar values and array as arguments to function
by unixhome (Novice) on Jun 14, 2006 at 17:39 UTC
    Thank you everybody for your answers. Ended up with following:
    sub format_output { my $var1 = shift; my $var2 = shift; my @array = @_; my $tmp = 0; for ($tmp = 0 ; $tmp <= $#array ; $tmp++) { do_good_stuff_to_array_elements; } } my @array = (); # $scalar carries the binary data @array = split('', $scalar); &format_output(":", 0, @array[8 .. 13]);
    Thanks again, Ronald Vazquez
A reply falls below the community's threshold of quality. You may see it by logging in.