Perl Function Array Arguement Passing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Function Array Arguement Passing
# 1  
Old 03-26-2009
Perl Function Array Arguement Passing

Hi All,

I have some questions regarding array arguements passing for Perl Function.
If @array contains 2 items , arguements passing would be like Code_A.
But what if @array needs to add in more items, the rest of the code like $_[some number] will have to be modified as well (highlighted in red), which is very troublesome, just like in Code_B where 1 more item is added to @array. Is there an easier way to overcome this problem ?

Code_A
Code:
#!/usr/local/bin/perl

my @array = ( "AAA" , "BBB" );


Generic( @array , "WE" , "HIGH" , "DOWN" );


sub Generic {
	foreach my $file ( $_[0] , $_[1] ) {
		print "Now Processing $file ...\n";
	};

	print "$_[2]\n";
	print "$_[3]\n";
	print "$_[4]\n";

}

Code_B
Code:
#!/usr/local/bin/perl

my @array = ( "AAA" , "BBB" , "CCC");


Generic( @array , "WE" , "HIGH" , "DOWN" );


sub Generic {
	foreach my $file ( $_[0] , $_[1] , $_[2]) {
		print "Now Processing $file ...\n";
	};

	print "$_[3]\n";
	print "$_[4]\n";
	print "$_[5]\n";

}


Last edited by Raynon; 03-26-2009 at 10:56 PM..
# 2  
Old 03-27-2009
There is a couple of ways to tackle the problem. Keep in mind when you pass arguments to a function they all become one big list. So either you pass the strings as the first arguments and the array last:

Code:
Generic( "WE" , "HIGH" , "DOWN", @array);
sub Generic {
           my ($w, $h, $d,@t) = @_;
	foreach my $file (@t) {
		print "Now Processing $file ...\n";
	};

	print "$w\n";
	print "$h\n";
	print "$d\n";

}

or you pass a reference to the array (it can be anywhere in the argument list, I place it first here only as an example):

Code:
Generic( \@array, "WE" , "HIGH" , "DOWN");
sub Generic {
           my ($ref, $w, $h, $d) = @_;
	foreach my $file (@{$ref}) {
		print "Now Processing $file ...\n";
	};

	print "$w\n";
	print "$h\n";
	print "$d\n";

}

# 3  
Old 03-27-2009
Hi KevinDAC,

Thanks for the advice, but what's the below code trying to do ?
And what does @_ represent ?
And what if there are 2 array list ? Which method would be better ?

Code:
my ($w, $h, $d,@t) = @_;

# 4  
Old 03-27-2009
@_ is the automagically created array containing all calling parameters (the array that you use when you access $_[x]). And if there's more than one array to pass around, refs are definitely the way to go, unless you have some other way to discern one array from the other.
# 5  
Old 03-27-2009
Hi pludi,

Thanks for the guidance!
Smilie Smilie
# 6  
Old 03-27-2009
Quote:
Originally Posted by pludi
@_ is the automagically created array containing all calling parameters (the array that you use when you access $_[x]). And if there's more than one array to pass around, refs are definitely the way to go, unless you have some other way to discern one array from the other.
I agree with pludi. If you have more than one array, or even really just one array, use references as I showed in my other post. As far as @_ goes, he explained it well enough so I will leave it at that.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variable value in a function to be used by another function

Hello All, I would like to ask help from you on how to pass variable value from a function that has been called inside the function. I have created below and put the variables in " ". Is there another way I can do this? Thank you in advance. readtasklist() { while read -r mod ver... (1 Reply)
Discussion started by: aderamos12
1 Replies

2. Shell Programming and Scripting

Problem in passing date to external function from perl script.

my $sysdate = strftime('%Y-%m-%d', localtime ); biDeriveByDate('Table_Str',$sysdate,\@lIndx,\@lResVals) In a perl script, when I'm trying to pass $sysdate to some external function it's not working since $sysdate is passed as a string mentioned above but my function is expecting a date value... (1 Reply)
Discussion started by: Devesh5683
1 Replies

3. Programming

[solved]help passing array of structs to function in c

this is my code to try and prinnt out a deck of cards. the print function worked when used inside main without being a function but now i cant get it to work as a function probably since i dont know how to pass a struct array in c. I gave it a shot but i keep getting an assortment of errors. The... (0 Replies)
Discussion started by: bjhum33
0 Replies

4. Programming

Passing two dimensional array to a function

Hi. I have a problem with passing two dimensional array to a function. First, let me show my code to explain what i am going to do: I have function:void initialize_board(char board);which is supposed to modify content of passed array. I have read here: Question 6.18 how such arrays should be... (3 Replies)
Discussion started by: Shang
3 Replies

5. Shell Programming and Scripting

[bash] passing array to function

Hi, I'm trying to write a function that reassigns an array to another local array but the method used in reassigning the array reformats the contents of the array which is what I am trying to prevent. The method used to load a file into an array works as expected and the entire array is... (4 Replies)
Discussion started by: ASGR
4 Replies

6. Shell Programming and Scripting

Passing arguments to Perl Function

Hi All, I am trying to pass an argument called "Pricelist" to a Perl function, then the function will open and print out the contents of the file named "Pricelist". But i can't seem to do it using my below code. Can any expert give some advice? #!/usr/local/bin/perl $DATABASE =... (1 Reply)
Discussion started by: Raynon
1 Replies

7. UNIX for Dummies Questions & Answers

How to pass array as an arguement ?

Hi experts, I am here again with another Issue. I need to pass the array as parameter / argument to another script. I tried it as follows . ( I got this idea from the link ) $ cat test1.sh #! /usr/bin/ksh set -a arr1 echo "...In Test1...." arr1="APPS_DEV" arr1="TEST_DEV" echo... (16 Replies)
Discussion started by: rajavu
16 Replies

8. Shell Programming and Scripting

Passing a file handler and an array from Perl to Shell Script

Hi there, I am trying to call a shell script from a Perl script. here is the code: @args = ("sh", "someshellprg.sh", "a file handler", "an array"); system(@args) == 0 or die "system @args failed: $?"; in the shell program, I examine if the arguments exits using: if then echo... (5 Replies)
Discussion started by: pinkgladiator
5 Replies

9. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

10. Shell Programming and Scripting

[BASH - KSH] Passing array to a function

Passing a array to a function, a basic feature in modern language, seems to be only possible in KSH. Not in BASH. Depite all my efforts I couldn't come to a solution. See the following examples: It works perfectly in KSH: #!/usr/bin/ksh function print_array { # assign array by indirect... (3 Replies)
Discussion started by: ripat
3 Replies
Login or Register to Ask a Question