List Info

Thread: Re: help with passing Array and string into subroutine




Re: help with passing Array and string into subroutine
country flaguser name
United States
2007-03-29 00:43:01

Hi! Josh,

The good idea is to follow "perldoc perlfaq" always. Still, I have attached the code, see line no. 4-6, 20.

## start snippet ------------ --------- -------
sub findMatch {
#my (array,$text) = _; ##this is the line that doesn't work
my (array,$text);
array = {$_[0]}; # converting array reference to array
$text = $_[1];
foreach $line (array) {
if (index($line, $text) != -1)
{
tempArray = split(/=/,$line) ;
return $tempArray[1] ;
}
}
return '';
}
myArray = ("time=34","tom=29","josh=41", "michele= 42","owen= 41","todd=29");
$age = findMatch(myArray, "josh");
print $age; ## age always comes up blank
## end snippet ------------ --------- -------

----- Original Message ----
From: josh.bloom < josh_b_bloom%40yahoo.com">josh_b_bloomyahoo.com>
To: perl-beginner%40yahoogroups.com">perl-beginneryahoogroups.com
Sent: Thursday, March 29, 2007 6:44:25 AM
Subject: [PBML] help with passing Array and string into subroutine

Help please. I am new to perl and can't find an answer to this
simple question. I want to pass an array and a string into a
subroutine and I just can't figure out how to do it. Here is what I
have and the string always comes up empty:

## start snippet ------------ --------- -------
sub findMatch {
my (array,$text) = _; ##this is the line that doesn't work
foreach $line (array) {
if (index($line, $text) != -1)
{
tempArray = split(/=/,$line) ;
return $tempArray[1] ;
}
}
return '';
}
myArray =
("time=34&quot;," tom=29&quot;," josh=41&quot;, "michele= 42",&quot;owen= 41",&quot;todd= 29");

$age = findMatch(myArray, "josh");
print $age ## age always comes up blank
## end snippet ------------ --------- -------

__________________________________________________________
It's here! Your new message!
Get new email alerts with the free Yahoo! Toolbar.
http://tools.search.yahoo.com/toolbar/features/mail/

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )