user1613245
user1613245

Reputation: 351

Hash Key and Value in Perl

I have the question in Perl:Read a series of last names and phone numbers from the given input. The names and numbers should be separated by a comma. Then print the names and numbers alphabetically according to last name. Use hashes.

#!usr/bin/perl
my %series = ('Ashok','4365654435' 'Ramnath','4356456546' 'Aniketh','4565467577');
while (($key, $value) = each(sort %series))
{
 print $key.",".$value."\n";
}

I am not getting the output. Where am I going wrong? Please help. Thanks in advance

#!usr/bin/perl
my %series = ('Ashok','4365654435' 'Ramnath','4356456546' 'Aniketh','4565467577');
print $_.",".$series{$_}."\n" for sort keys %series;

If I execute any of the above 2 programs, I get the same output as:

String found where operator expected at line 2, near "'4365654435' 'Ramnath'" (Missing operator before  'Ramnath'?)
String found where operator expected at line 2, near "'4356456546' 'Aniketh'" (Missing operator before  'Aniketh'?)
syntax error at line 2, near "'4365654435' 'Ramnath'"
Execution aborted due to compilation errors

But according to the question, I think I cannot store the input as my %series = ('Ashok','4365654435','Ramnath','4356456546','Aniketh','4565467577');

Upvotes: 1

Views: 391

Answers (3)

Tudor Constantin
Tudor Constantin

Reputation: 26861

Besides what amonand Mat said, I'd like to notice other issues in your code:

  • your shebang is wrong it should be #!/usr/bin/perl - notice the first /
  • you don't have use strict; and use warnings; in your code - although this is not strictly a mistake, I consider this to be an issue. Those 2 commands will save you from a lot of trouble later on.

PS: you have to use commas between your number and names also, not only between names and numbers - you have to, because otherwise you get a compile error

Upvotes: 1

amon
amon

Reputation: 57590

In your hash declaration you have:

my %series = ('Ashok','4365654435' 'Ramnath','4356456546' 'Aniketh','4565467577');

This is generating the warnings.

A hash is simply an even list of scalars. Therefore, you have to put a comma between each pair:

my %series = ('Ashok','4365654435', 'Ramnath','4356456546', 'Aniketh','4565467577');
             #                    ^---                    ^---

If you want visual distinction between the pairs, you can use the => operator. This behaves the same as the comma. Additionaly, if the left hand side is a legal bareword, it is viewed as a quoted string. Therefore, we could write any of these:

# it is just a comma after all, with autoquoting
my %series = (Ashok => 4365654435 => Ramnath => 4356456546 => Aniketh => 4565467577);

# using it as a visual "pair" constructor
my %series = ('Ashok'=>'4365654435', 'Ramnath'=>'4356456546', 'Aniketh'=>'4565467577');

# as above, but using autoquoting. Numbers don't have to be quoted.
my %series = (
   Ashok   => 4365654435,
   Ramnath => 4356456546,
   Aniketh => 4565467577,
);

This last solution is the best. The last coma is optional, but I consider it good style—it makes it easy to add another entry. You can use autoquoting whenever the bareword on the left would be a legal variable name. E.g. a_bc => 1 is valid, but a bc => 1 is not (whitespace is not allowed in variable names), and +/- => 1 is not allowed (reserved characters). However Ünıçøðé => 1 is allowed when your source code is encoded in UTF-8 and you use uft8 in your script.

Upvotes: 2

Mat
Mat

Reputation: 206659

each only operates on hashes. You can't use sort like that, it sorts lists not hashes.

Your loop could be:

foreach my $key (sort keys %series) {
  print $key.",".$series{$key}."\n";
}

Or in shorthand:

print $_.",".$series{$_}."\n" for sort keys %series;

Upvotes: 4

Related Questions