ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
you have some Perl experience judging from your previous posts and i really doubt you can't do it. Have you tried running the code? seems not i guess. I suggest you run the code and see what it says.
would be printed...
food
food
drink
drink
but, how printing once only of food or drink, for inctance...
food
drink
First, you're doing an assignment, so you need my %map =. Second, you want '=>' not '->' to associate hash items. Third, you're missing a single-quote mark after 'beer, which is going to confuse the hell out of the Perl interpreter. Next, unless you add an explicit "\n", you won't get a newline in your print. Finally, as things stand you are running through all the keys in your hash and trying to print them. The keys are the items on the left hand side of each hash couplet. So what you will get printed, if you fix the code you're starting with, is "chips, hamburger, water, beer" (on new lines if you add those).
What are you really trying to do? It might help us to give you advice.
Oh, and you should also always use strict and warnings in your code. They help you find little problems and save you from big ones.
Sample code:
Code:
#!/usr/bin/env perl
use strict;
use warnings;
my %map = (
'chips' => 'food',
'hamburger' => 'food',
'water' => 'drink',
'beer '=> 'drink', # The last comma is optional, but it's
# a good idea since you might add items later
);
foreach ( keys (%map) ){
print $_, "\n";
}
If you're looking for a good initial book about Perl, Beginning Perl is available freely online now (since it's out of print). It's a bit out of date, and so lacks some newer features, but for the basics of the language it's still very good.
Last edited by Telemachos; 05-19-2009 at 07:00 AM.
I think the idea you need is to take the values from this hash and put them into a second hash as the keys of the second hash. Then in a second loop, print the keys. Since the keys in a hash must be unique, this will reduce each unique value to a single instance and result in only one line per unique value.
Last edited by kellinwood; 05-19-2009 at 09:13 AM.
Reason: clarification
ok, that printing
food
food
drink
drink
Each hash should be printed once only.
food
drink
Code:
foreach ( keys (%map) ){
print $map{$_}, "\n";
}
No, the code you printed should do exactly what it does do. It prints the value of each item in the hash. Hash keys must be unique (a second, third, fourth, etc. key identical to an earlier one will simply overwrite the earlier one), but there's no such restriction on hash values. So if you want 'food' and 'drink' to be unique, you have to do some extra work.
As Kellinwood says, you could create a second hash where the keys are the values of the previous hash. Or you can use Perl's grep function to test for values that have already been seen. See perldoc -q duplicate for more on this:
Code:
#!/usr/bin/env perl
use strict;
use warnings;
my %map = (
'chips' => 'food',
'hamburger' => 'food',
'water' => 'drink',
'beer '=> 'drink', # The last comma is optional, but it's
# a good idea since you might add items later
);
my %seen;
my @unique_values = grep { ! $seen{$_}++ } values %map;
print "$_\n" foreach @unique_values;
Last edited by Telemachos; 05-19-2009 at 02:14 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.