Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to read this Perl code?

Posted on 2011-03-24
2
Medium Priority
?
304 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 252 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 248 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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

715 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