LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-05-2009, 05:03 AM   #1
abs_77
LQ Newbie
 
Registered: Feb 2007
Location: India, Bombay
Distribution: Linux & BSD
Posts: 23

Rep: Reputation: 15
Perl Subroutine help


Hi, I need to know how to pass parameter dynamically to perl subroutine

This is my program :-

#===========================================================#
#!/usr/bin/perl

@first = (1,2,3,4);
@second = (4,5,8,9);
@third = (1,2);
@forth = (5,9);

@firstArr = &fun2(@first,@second);
@secondArr = &fun2(@third,@forth);

print "printing newArray: @newArr\n";
print "printing secondArr: @secondArr\n";

sub fun2()
{
@new = @first; #need to pass this as dynamically to @new either @first or @third
@old = @second; #need to pass this as dynamically to @old either @two or @forth
$i = 0;
{
foreach $val1(@new){
foreach $val2(@old){
$res[$i] = $i;
# print "res : $res[$i]\n";
$i++;
}
}
}

return @res;
}


#===========================================================#

Sorry for my English.
Any help would be appreciated.
Thanks,
ABS

Last edited by abs_77; 05-05-2009 at 05:17 AM.
 
Old 05-05-2009, 05:42 AM   #2
bhaslinux
Member
 
Registered: Oct 2003
Location: UnitedKingdom
Distribution: Debian Bullseye
Posts: 357

Rep: Reputation: 49
use references

@firstArr = &fun2(\@first,\@second);
and

@new = @{$first}
 
Old 05-05-2009, 06:05 AM   #3
abs_77
LQ Newbie
 
Registered: Feb 2007
Location: India, Bombay
Distribution: Linux & BSD
Posts: 23

Original Poster
Rep: Reputation: 15
Thanks for the help
 
Old 05-05-2009, 10:30 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Also, you don't need to use '&' to call a sub, that's deprecated in Perl 5 (was used in Perl 4).
 
Old 05-06-2009, 02:07 AM   #5
abs_77
LQ Newbie
 
Registered: Feb 2007
Location: India, Bombay
Distribution: Linux & BSD
Posts: 23

Original Poster
Rep: Reputation: 15
Noted.
Thanks, another good help
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Call Perl subroutine from console estratos Programming 2 04-22-2008 10:46 AM
perl subroutine call shifty_eyes Programming 2 10-01-2005 08:43 PM
Perl undefined subroutine /can't locate object method cobien Programming 1 11-17-2003 10:47 AM
Need Perl/CGI help! Undefined Subroutine CragStar Programming 4 03-05-2002 07:28 AM
Perl Subroutine problem sykkn Programming 0 02-20-2002 09:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration