LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-19-2009, 06:06 AM   #1
quantt
Member
 
Registered: Aug 2008
Posts: 62

Rep: Reputation: 15
perl: give me suggestion


hello,
To suggest me whatever so as to do it quite well.
Code:
my %map(
'chips' -> 'food',
'hamburger' -> 'food',
'water' -> 'drink',
'beer -> 'drink'
);
foreach (keys(%map)){
print $_; 
}
would be printed...
food
food
drink
drink
but, how printing once only of food or drink, for inctance...
food
drink
 
Old 05-19-2009, 06:15 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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.
 
Old 05-19-2009, 06:57 AM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by quantt View Post
hello,
To suggest me whatever so as to do it quite well.
Code:
my %map(
'chips' -> 'food',
'hamburger' -> 'food',
'water' -> 'drink',
'beer -> 'drink'
);
foreach (keys(%map)){
print $_; 
}
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.
 
Old 05-19-2009, 08:57 AM   #4
quantt
Member
 
Registered: Aug 2008
Posts: 62

Original Poster
Rep: Reputation: 15
ok, that printing
food
food
drink
drink
Each hash should be printed once only.
food
drink
Code:
foreach  (  keys (%map) ){
  print $map{$_}, "\n";
}
 
Old 05-19-2009, 09:11 AM   #5
kellinwood
Member
 
Registered: Jan 2006
Location: Culver City, California
Distribution: Fedora
Posts: 64

Rep: Reputation: 21
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
 
Old 05-19-2009, 02:12 PM   #6
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by quantt View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl: directories given to opendir via foreach give "No such file or directory" error Verve1986 Programming 1 02-19-2009 05:54 AM
What is the best IRC client in Linux? Can someone give me a suggestion? silentray Linux - Software 8 02-07-2008 09:50 AM
Can you guys give me any suggestion? ICO General 6 04-04-2005 11:30 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
Can any PERL gurus give me a hand, please? WorldBuilder Programming 16 10-23-2003 05:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration