Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
MY hash

Chicago, USA
Frankfurt, Germany
Berlin, Germany
Washington, USA
Helsinki, Finland
New York, USA

As someone else has already pointed out, this isn't a hash. It isn't even Perl. It's just a list of cities and their corresponding countries in English.

I suspect your hash is this:

( 'Chicago' => 'USA', 'Frankfurt' => 'Germany', 'Berlin' => 'Germany', 'Washington' => 'USA', 'Helsinki' => 'Finland', 'New York' => 'USA', )

Did I guess right?

but i want i put like

Finland: Helsinki.
Germany: Berlin, Frankfurt.
USA: Chicago, New York, Washington.

Once again, this isn't anything in Perl. It's just an English language list of countries and corresponding lists of cities in those countries.

To me, the most natural Perl data structure to use to represent lists of cities by their corresponding countries is a hash of arrays.

( 'Finland' => [ 'Helsinki' ], 'Germany' => [ 'Berlin', 'Frankfurt' ], 'USA' => [ 'Chicago', 'New York', 'Washington' ], )

So are you asking us how to create this second data structure (a hash of arrays) from the first data structure (a simple hash)?

use strict; use warnings; # Hash of countries by city my %countries_by = ( 'Chicago' => 'USA', 'Frankfurt' => 'Germany', 'Berlin' => 'Germany', 'Washington' => 'USA', 'Helsinki' => 'Finland', 'New York' => 'USA', ); # Hash of arrays of cities by country my %cities_by; # Generate a hash of arrays of cities by country # from a hash of countries by city... while (my ($city, $country) = each %countries_by) { push @{ $cities_by{$country} }, $city; } # Print a list of cities by country... for my $country (sort keys %cities_by) { for my $city (sort @{ $cities_by{$country} }) { print "$country\t$city\n"; } }

(This small example isn't Unicode-conformant.)

This Perl script prints…

    Finland Helsinki
    Germany Berlin
    Germany Frankfurt
    USA     Chicago
    USA     New York
    USA     Washington

The other day in the Chatterbox, I recommended you take a beginning Perl course to learn the rudiments of the language. Have you enrolled in a course yet? If not, you should. (I can see you're in the Chatterbox right now asking how to perform simple text substitution in Perl using substr() instead of the substitution operator s///. This is more evidence that you really need to learn the fundamentals of the Perl programming language either from a good teacher or a good book. Trying to learn it by persistently asking poorly-articulated questions on PerlMonks is very unlikely to succeed.)

Jim


In reply to Re: Hash in Perl by Jim
in thread Hash in Perl by rammohan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-19 06:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found