CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Fun With Perl: Perl Quizzes - Learn Perl the Fun Way:
New Quiz



Feb 19, 2002, 12:32 PM

Post #1 of 3 (49145 views)
New Quiz Can't Post

First, if you are not new to Perl, please, don't reply. This
is a quiz for new Perl programmers to get them to learn
new tricks.

Okay. Here is your problem. This is not Perl Golf. I
encourage verbose programming for readability.

You have a hash %fruits with unique keys and values
(for learning sake).

%fruits = ( 'banana', 'yellow', 'apple', 'red', 'pear', 'green', 'mango', 'orange');

Your code, requires you to have a similar hash, where the
keys are colors, you can get the associated fruit.
Write the code that can give you a hash - %colors and
show that you can access a type of fruit by it's corresponding

Good Luck,

print unpack"u*",pack "h*",$a,"\n\n";


Feb 20, 2002, 6:54 AM

Post #2 of 3 (49134 views)
Re: [fashimpaur] New Quiz [In reply to] Can't Post

Okay, as the forum states, today I give the answer and an explanation.

The answer is:


%colors = reverse %fruits;


The explanation is quite simple. The perl function 'reverse'
expects one parameter, a LIST. If the method is invoked
where the return result expected is another LIST, the
value returned is a LIST in the opposite order. In this
case, since a hash is passed and it is a named
name/value pair list (as opposed to an anoymous LIST)
and we are expecting a list context (determined by the
left side of the equation), then an opposite ordered list is
stored in the %colors hash.

Because we said that the list we want to stuff our
returned value into is a special name/value pair list,
we can now use it's keys to get values returned.

Note that I specified that each value in the first hash had
to be unique. This is because when reverse occurs, had
two values been the same, the following scenario would
have occurred:

Assume that we had added $fruits{lemon} = yellow to
the hash.

reverse would process the first (no telling if it would be
banana or lemon) key 'yellow' the associated value
'lemon' (assuming that lemon was the first to process)

Then, later, when 'yellow' comes up again, the method
would see that there already was a key 'yellow' defined
and would set it's value to 'banana'. At this point, lemon
was lost. Hashes do not store the keys in any order. It is
purely random. Had the first processed been 'banana',
then $colors{yellow} would have the value 'lemon'
and 'banana' would be lost.

Then, you ask, why would you want to do this? Well, the
time you want to do this is when you have a known hash
than can only have unique values for the given keys.
Then, when it is reversed, you can reference a given
value to get it's key name. This might be useful for
figuring out what field name to store data in a database
after a form was submitted with only the value returned,
(like from a menu list on a cgi form).

Hope this was a useful note for you. If I have mistated
anything, I would appreciate a private reply so that I
may fix the error while this is editable.

print unpack"u*",pack "h*",$a,"\n\n";


Feb 21, 2002, 12:38 AM

Post #3 of 3 (49126 views)
Re: [fashimpaur] New Quiz [In reply to] Can't Post

If this is ment for beginners, you might better post it at the beginners forum... Some topics I also don't look in, simply because I think it's too difficult, or "not my cup of tea"

Yet Another Perl Programmer

~~> [url=] <~~
More then 3500 X-Forum [url=]Downloads! Cool


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives