CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Pass array to function.

 



Jack
Novice

Sep 13, 2002, 1:12 PM

Post #1 of 16 (2255 views)
Pass array to function. Can't Post

Hi,

I have a function, can I call this function like this?

$ERR = db::update_order($dbh, @listingOrder) ;

this is the function in db Package.

sub update_order ($$) {
my ($dbh, $IDs) = @_;
my @tempArray=@($IDs);

while (@tempArray) {
my %tmp=%{shift @tempArray};
foreach (keys %tmp){
#print " $_ => $tmp{$_} <br>";
my $sqlStatement = "UPDATE Listings set lo = ? where ID=?";
my $sth = $dbh->prepare($sqlStatement);
$sth->execute($_, $tmp{$_});

}
if (!$sth->err) {
return 0;
} else {
return $sth->errstr;
}
}


Thanks.



fashimpaur
User / Moderator

Sep 13, 2002, 2:17 PM

Post #2 of 16 (2251 views)
Re: [Jack] Pass array to function. [In reply to] Can't Post

Watch your prototype for the function. I think it should be

sub updateOrder($@){
#....
}

Good Luck,
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


Jack
Novice

Sep 13, 2002, 2:53 PM

Post #3 of 16 (2248 views)
Re: [fashimpaur] Pass array to function. [In reply to] Can't Post

hi,

the reason I passed two parameters is one is db handler, one is array we discussed before.

update_order($dbh, @listingOrder).

thanks.




davorg
Thaumaturge / Moderator

Sep 13, 2002, 3:24 PM

Post #4 of 16 (2243 views)
Re: [Jack] Pass array to function. [In reply to] Can't Post

You need to change your prototype to

Code
sub update_order ($\@) {

Which forces the second argument to be passed as a reference to an array.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


davorg
Thaumaturge / Moderator

Sep 13, 2002, 3:26 PM

Post #5 of 16 (2242 views)
Re: [fashimpaur] Pass array to function. [In reply to] Can't Post


In Reply To
Watch your prototype for the function. I think it should be

Code
sub updateOrder($@){ 
#....
}



But that makes the second argument an array, whereas if you look at the definition of the function, it's expecting a reference to an array. The prototype should therefore be written as:

Code
sub updateOrder($\@){ 
#....
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Paul
Enthusiast

Sep 13, 2002, 3:33 PM

Post #6 of 16 (2240 views)
Re: [davorg] Pass array to function. [In reply to] Can't Post

>>
Which forces the second argument to be passed as a reference to an array.
<<

What exactly do you mean by that?


davorg
Thaumaturge / Moderator

Sep 13, 2002, 3:39 PM

Post #7 of 16 (2238 views)
Re: [RedRum] Pass array to function. [In reply to] Can't Post

Perhaps an example will clarify:

Code
#! /usr/bin/perl -w 

use strict;

sub test ($\@) {
my ($s, $a) = @_;

print "scalar [$s]\n";
print "array [@$a]\n";
}

my $scalar = 'x';
my @array = (1 .. 10);

test($scalar, @array);

This prints:

Code
scalar [x] 
array [1 2 3 4 5 6 7 8 9 10]


See, although the second argument was passed in as an array, the '\@' in the prototype forced Perl to take a reference to it and pass the reference to the subroutine.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Paul
Enthusiast

Sep 13, 2002, 3:42 PM

Post #8 of 16 (2237 views)
Re: [davorg] Pass array to function. [In reply to] Can't Post

I thought that was what you meant. The reason I asked was because it didn't work for me. Prototypes never seem to :(

my @array = qw/1 2 3 4 5/;

print test(@array);

sub test (\@) { return @_ }

....that prints 12345


davorg
Thaumaturge / Moderator

Sep 13, 2002, 3:48 PM

Post #9 of 16 (2236 views)
Re: [RedRum] Pass array to function. [In reply to] Can't Post

Testing your code with -w on, I got the warning "main::test() called too early to check prototype at ./test.pl line 7.".

Rearranging to put the definition of the subroutine before the call to it, the output becomes:

Code
ARRAY(0x8104b80)


That's one of the problems with Perl prototypes - they're so easy to circumvent :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Paul
Enthusiast

Sep 13, 2002, 3:49 PM

Post #10 of 16 (2235 views)
Re: [davorg] Pass array to function. [In reply to] Can't Post

Yeah I just checked my log coincidentally and found the same error ;(


Jack
Novice

Sep 13, 2002, 11:18 PM

Post #11 of 16 (2226 views)
Re: [fashimpaur] Pass array to function. [In reply to] Can't Post

Hi Dennis,
I just learned perl by myself, that is why I have a lot of questions, thanks for your help. Can you take a look at the code again? I couldn't figure out what's wrong with this.
here is html template
<b><input type="text" name="lo" size=5 value="<!--TMPL_VAR name="LO"-->" maxlength=5>&nbsp;</b></TD>
<INPUT TYPE="hidden" NAME="listingID" value="<!--TMPL_VAR name="ID"-->">


Here is Perl file. (Action will call this)

if ($action eq "set Listing Order") {
$template = ttools::chooseTemplate($global::templates->{admin});
my @tempLo=$q->param("lo");
my @tempID=$q->param("listingID");
my @listingOrder=();
while (@tempLo and @tempID){
my %res;
$res{LO}=shift@tempLo;
$res{ID}=shift@tempID;
push @listingOrder, \%res;
}
my $ERR = 0;

$ERR = db::update_order($dbh, @listingOrder) ;


This is update_order.

my $dbh=shift @_;
my @tempArray=@_;
while (@tempArray) {
my %tmp=%{shift @tempArray};
my $lo=$tmp{'LO'};
my $id=$tmp{'ID'};

my $sqlStatement = "UPDATE Listings set lo =? where ID=?";
my $sth = $dbh->prepare($sqlStatement);
$sth->execute($lo,$id);

}


by the way, what's the mean of this.

while (my $row = $sth->fetchrow_hashref) {
my $ID = $row->{AGENT_ID};
if (! $AGENTS->{$ID}) {
$AGENTS->{$ID} = get_agent_info($dbh, $ID);
}
$row->{FIRST} = $AGENTS->{$ID}->{FIRST};
$row->{LAST} = $AGENTS->{$ID}->{LAST};
$row->{EMAIL} = $AGENTS->{$ID}->{EMAIL};


row is rash table or what.

Thanks.



fashimpaur
User / Moderator

Sep 16, 2002, 10:51 AM

Post #12 of 16 (2215 views)
Re: [davorg] Pass array to function. [In reply to] Can't Post

Dave,

However, in the "Programming Perl" book from O'Reilly:


Quote
Any backslashed prototype character ... represents an actual argument that
absolutely must start with that character.

Thus, you are not forcing it to be an array reference. It is just saying that that prototype is saying it must have an array as the second argument. Tongue

Just a japh opinion,
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


fashimpaur
User / Moderator

Sep 16, 2002, 10:57 AM

Post #13 of 16 (2213 views)
Re: [Jack] Pass array to function. [In reply to] Can't Post

You never set the values in $template to anything.
i.e. $template->param('LO' => $q->param('lo'));

Beyond that, you may want attach a little more code for me to make complete
sense of this.

[edit]
By the way, $row is a reference to a hash.

Good Luck,
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


(This post was edited by fashimpaur on Sep 16, 2002, 10:58 AM)


davorg
Thaumaturge / Moderator

Sep 16, 2002, 12:09 PM

Post #14 of 16 (2210 views)
Re: [fashimpaur] Pass array to function. [In reply to] Can't Post


In Reply To
Dave,

However, in the "Programming Perl" book from O'Reilly:


Quote
Any backslashed prototype character ... represents an actual argument that
absolutely must start with that character.

Thus, you are not forcing it to be an array reference. It is just saying that that prototype is saying it must have an array as the second argument. Tongue

Just a japh opinion,

Dennis,

I agree completely that the \@ notation forces the argument to begin with a @. But that doesn't preclude it being turned into a reference as well. I admit that the docs and the Camel are both unclear on this point, but p230 of the Camel (3ed) says:

Quote
If you're writing new code and would like a unary operator that takes only a scalar reference, not any old scalar expression, you could prototype it to take a scalar reference.

Code
sub func (\$) { 
my $nref = shift;
print "you gave me $$nref\n";
}


I'm assuming that the same will apply to arrays and hashes.

Anyway, if an array isn't turned into a reference, how would you explain this behaviour:

Code
#!/usr/bin/perl 

use warnings;
use strict;

sub array (\@) {
print "The subroutine has ", scalar @_, " arguments\n";

print "The first argument is ";

my $ref = ref $_[0];

if ($ref) {
print "a reference to a $ref\n";
} else {
print "not a reference\n";
}
}

my @array = 1 .. 10;

array(@array);

On my system, this prints:

Code
The subroutine has 1 arguments 
The first argument is a reference to a ARRAY


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


fashimpaur
User / Moderator

Sep 16, 2002, 1:03 PM

Post #15 of 16 (2209 views)
Re: [davorg] Pass array to function. [In reply to] Can't Post

Dave,

Point taken. I figured that was what was going on in the background.
If you just use an @ symbol, all remaining arguments are pushed into
that array reference. \@ is probably a safer bet.

I do like the level of knowledge you constantly bring to this forum.
Keep up the good work.
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


davorg
Thaumaturge / Moderator

Sep 17, 2002, 1:01 AM

Post #16 of 16 (2201 views)
Re: [fashimpaur] Pass array to function. [In reply to] Can't Post

If you use a @ then all the remaining arguments are put into a _list_ not an array. This allows you to write code like:

Code
sub my_push (\@@) { 
my $arr = shift;

$arr->[@$arr] = $_ foreach @_;
}

my @arr = qw/a short list/;
my_push @arr, 'some', 'more', 'elements';

print "@arr\n";


Try doing that without a prototype :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives