Solved

How to read this Perl code?

Posted on 2011-03-24
2
249 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now