perl: passing 2 individual arrays/lists as args
Suppose I'm writing a function that I want to operate on two different arrays:
Code:
#!/usr/bin/perl I'm looking at the docs for the built-in 'push' function. It says that operator takes an ARRAY and then also a LIST. What's the difference? How does push know where the first one ends and the next one begins? |
BTW, I realize that I can just use references to arrays to do what I want to do, however, I'd like to understand how builtins like 'push' do their magic.
|
IIRC, you can do
Code:
sub foo (@@) { |
perl doesn't know where one ends and the other begins. I believe the solution is to use references. the fact that perl throws all of it's subroutine arguments into one big list is handy most of the time, unless your dealing with arrays. So what you would do is create references to the arrays (kind of like pointers) and pass the references to the sub, then dereference them in the sub. for example:
Code:
#!/usr/bin/perl scalar: $$ref array: @$ref hash: %$ref hope this helps |
Ahh... that (@@) function declaration notation seems to be what I was missing. I'll look further into it. Thanks.
|
Thanks sirclif. I've also found "man perlreftut" and, of course, "man perlref" pretty helpful as well. :)
|
sorry i didn't see your replay saying that you already knew about references. the built in function push takes an array and 'tacks on' a scalar value or a list to the end of the array. there isn't much difference between an array and a list. an array is just a list with a name really. anyhow, given an array array1 you can push and pop elements on and off.
@array1 = (1,2,3); push @array1, 4; array1 now looks like (1,2,3,4) vise versa if you pop the array into a scalar $tmp = pop @array1; tmp is now 4. but again, push, pop, unshift, and shift have no idea that an array is supposed to be more than one array, they just add and remove elements from the back and front of an array. |
> but again, push, pop, unshift, and shift have no idea that an
> array is supposed to be more than one array, they just add > and remove elements from the back and front of an array. Yes, but there's magic going on there. "push", of course, *modifies* @array1. If the args passed to "push" were all simply flattened into one list, there would be no way for "push" to know about the array it was supposed to modify. This is why it seems (to me) like "(@@)" is key. Looks like "man perlsub" explains it under the heading "Prototypes" (which I'm currently having a look at :). Thanks. |
ah, i see what your saying. the builtin function push takes two arrays and still knows how to deal with it. buitins also do not require parentheses around thier arguments, or the & operator.
|
Quote:
Code:
15:38 aluser@alf:~$ perl -e 'sub foo { print "hi there $_[0]\n" } foo "bob"' |
Quote:
|
wow, sure enought.
why can't i get it to work in a script file? |
Quote:
Code:
#!/usr/bin/perl For user functions, I tend to always use parentheses; probably because I've had that error from not predeclaring the sub at some point and mostly because it makes things less clear not to have them. |
All times are GMT -5. The time now is 03:35 AM. |