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:
Mystery code, arrays and hashes, what am I looking at?

 



Iconx
Novice

Feb 12, 2014, 3:26 PM

Post #1 of 2 (1258 views)
Mystery code, arrays and hashes, what am I looking at? Can't Post

(This is cross-posted on DevShed)

I need some help here, I've been staring at this clip of code(part of a larger whole) and trying to make sense of it for hours now and I think I'm loosing my mind or there's some real brain damage in the writing here. A file is read in and split on colon's into an array and each split is put into a variable but what is happening with the hash here? What is the point of this, what is this %access hash here and whats with these sub hashes here like searchname and searchphone, are these elements of the access hash or new hashes or what... There's gotta be a better way to do this but I'm just trying to understand the logic here. Anyone help me out?


Code
my %access =(); 


my @record = split(':', $entry_in_file);

my $id = $record[0];
my $acct_name = $record[1];
my $street = $record[2];
my $phone = $record[3];

%{$access{$acct_name}} = (
id => $id,
name => $name,
street => $street,
phone => $phone,
);

$searchname{$name} = $entry_in_file;
$searchid{$id}{$name} = $entry_in_file;
$searchstreet{$street}{$name} = $entry_in_file;
$searchphone{$phone}{$name} = $entry_in_file;


Thanks,

Iconx


Laurent_R
Veteran / Moderator

Feb 12, 2014, 11:07 PM

Post #2 of 2 (1231 views)
Re: [Iconx] Mystery code, arrays and hashes, what am I looking at? [In reply to] Can't Post

Hi,


Code
  
%{$access{$acct_name}} = (
id => $id,
name => $name,
street => $street,
phone => $phone,
);

is building a hash of hashes (HoH). The $access{$acct_name} scalar value is really a reference to a sub-hash containing the id, name, etc. There is one thing, though, that I don't understand: I do not know where the $name variable is coming from, to me it is undefined in the snippet of code you have shown.

 
 


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

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