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:
an array ref? Not certain what this line of code means?

 



perlFun
Novice

Jul 9, 2013, 4:38 PM

Post #1 of 2 (165 views)
an array ref? Not certain what this line of code means? Can't Post

Sorry this question is so basic. I have the following line of code:

$obj->execute(command => ["delete",$file],
timeout => 60,
max_matches => 5,
match_table => [
("Do you have the file location") => "yes",
],
check_status => 1);


HERE'S my understand....

$obj is my object. I'm performing the subroutine "execute" on $obj. "execute" subroutine takes the following parameters: command, timeout, max_matches, match_table, check_status (the way this is called, are they then "named" parameters? like it doesn't matter whcih order I specified them in? just out of curiosity...)


now what confuses me is "match_table". It seems like the value passed in for the param match_table is an annonymous array (an array ref), evidenced by the square brackets. But I don't understand what the => does, nor what those parents inside the square brackets do. I understand that in perl "=>" is essentially a comma. This would make sense to me if this were a hash, but it seems like its an array.


The reason I'm confused is because in this particular sub, the "match_table" param, what it does is it takes the input, searches through the "table" to see if it matches what's there (in this case, "Do you have the file location") and if it matches that, then it sends as output, the thing after the => (so in this case it would send "yes" as output if the input matched "Do you have the file location"). But what would be the benefit of using an array here vs. a hash? (note, i don't have visibilty of the "execute" code, so I don't know it's implementation. But wouldn't a hash be better and more efficient? This isn't a hash is it?)


Zhris
User

Jul 10, 2013, 3:53 PM

Post #2 of 2 (151 views)
Re: [perlFun] an array ref? Not certain what this line of code means? [In reply to] Can't Post

Hey,

Below is my implementation of your object. The nototation of match_table suggests that the real implementation may retain the hash / array of key value pairs order i.e. by using an array splice. This is beneficial where params are anonymous.

code:

Code
package module; 

sub new
{
return bless { }, $_[0];
}

sub execute
{
my ( $self, %params ) = @_;

while ( my ( $key, $value ) = splice ( @{$params{match_table}}, 0, 2 ) )
{
print "$key => $value,\n";
}
}



package main;

my $file = '';

my $obj = module->new;

$obj->execute(command => ["delete",$file],
timeout => 60,
max_matches => 5,
match_table => [
one => 1,
two => 2,
("Do you have the file location") => "yes",
three => 3,
four => 4,
],
check_status => 1);


output:

Code
one => 1, 
two => 2,
Do you have the file location => yes,
three => 3,
four => 4,


Regards,

Chris


(This post was edited by Zhris on Jul 10, 2013, 4:10 PM)

 
 


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

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