Solved

How to read this Perl code?

Posted on 2011-03-24
2
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans

617 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