Solved

How to read this Perl code?

Posted on 2011-03-24
2
270 Views
Last Modified: 2012-05-11
Hi,
What does these two lines mean?


SOME CODE

 my %validC = map { ($_ => 1) } @validC;

            return $validC{$cluster};
}

Open in new window


What does this return?
0
Comment
Question by:Tolgar
2 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 63 total points
ID: 35211350
Those lines convert the array @validC to the hash %validC (which works but good practice is to name them differently).

For instance:

@validC = (qw(jim bob ron tom))

then

%validC = (jim=>1, bob=>1, ron=>1, tom=>1)

The return will return the value of $cluster in %validC.  So, in the example if $cluster is jim, bob, ron, or tom then it will return 1 - otherwise, it will return undef.
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 62 total points
ID: 35211358
It essentially creates a hash (%validC) and sets the value to 1 for all elements of the array @validC

The line

return $validC{$cluster};

returns what ever the value of $validC{$cluster} is.  As you don't show the code where $cluster is set, I'm guessing the value will either be 0 or 1.

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question