Solved

How to read this Perl code?

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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 …
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…

734 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