http://qs321.pair.com?node_id=1068896

rammohan has asked for the wisdom of the Perl Monks concerning the following question:

MY hash
Chicago, USA Frankfurt, Germany Berlin, Germany Washington, USA Helsinki, Finland New York, USA
but i want i put like
Finland: Helsinki. Germany: Berlin, Frankfurt. USA: Chicago, New York, Washington.
Please let us know.

Replies are listed 'Best First'.
Re: Hash in Perl
by LanX (Saint) on Jan 02, 2014 at 06:35 UTC
Re: Hash in Perl
by Athanasius (Archbishop) on Jan 02, 2014 at 06:35 UTC

    Create a second hash, with countries as the keys; the hash value for each country will be an anonymous array containing the cities belonging to that country:

    #! perl use strict; use warnings; my %h1 = ( 'Chicago, USA' => undef, 'Frankfurt, Germany' => undef, 'Berlin, Germany' => undef, 'Washington, USA' => undef, 'Helsinki, Finland' => undef, 'New York, USA' => undef, ); my %h2; for (keys %h1) { my ($city, $country) = split ', '; push @{ $h2{$country} }, $city; } for (sort keys %h2) { print "$_: ", join(', ', sort { $a cmp $b } @{ $h2{$_} }), "\n"; }

    Output:

    16:29 >perl 819_SoPW.pl Finland: Helsinki Germany: Berlin, Frankfurt USA: Chicago, New York, Washington 16:31 >

    Note: Perl autovivifies the new $h2{$country} elements as needed.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      why you again declare undef function.Actually we declare all values in hash then we need again that undef.?
        ... we declare all values in hash ...

        Declare them as what? As Anonymous Monk pointed out above, the data you show is not a hash, it’s a list (of strings). Since you said this data is in a hash, I made a guess as to how the hash was contstructed. A hash is a collection of key/value pairs. Every key must have a value (and every value must belong to a key). I assumed that the data was stored as hash keys, so I supplied an undef value for each — but that could have been any value, as it isn’t used.

        If your hash is really key/value pairs of the form: Chicago => 'USA' (another guess), adapting the answer I gave above should be quite straightforward.

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Hash in Perl
by Anonymous Monk on Jan 02, 2014 at 06:31 UTC
    What you call "my hash" is not perl code , so, see perlintro
Re: Hash in Perl (perreftut)
by Anonymous Monk on Jan 02, 2014 at 07:59 UTC

    What is your purpose in asking a question asked and answered by perlreftut?? That tutorial posts the Solution in the Solution section

      Good catch! ++

      So that's either motivated by homework or trolling... :(

      I doubt that further feeding will show any improvement...

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        I'd guess it's a question posed by an interviewer who doesn't know any perl to the OP.

Re: Hash in Perl
by Lennotoecom (Pilgrim) on Jan 02, 2014 at 07:01 UTC
    %h = ( 'USA' => ['Chicago', 'Washington', 'New York'], 'Germany' => ['Frankfurt', 'Berlin'], 'Finland' => ['Helsinki'] ); for $k (sort keys %h){ print "$k: @{$h{$k}}\n"; } %h = ( 'USA' => {'Chicago' => undef, 'Washington' => undef, 'New York' => + undef}, 'Germany' => {'Frankfurt' => undef, 'Berlin' => undef}, 'Finland' => {'Helsinki' => undef} ); for $k (sort keys %h){ print join ' ', $k, keys %{$h{$k}},"\n"; }
    The first option uses hash of arrays
    the second hash of hashes
Re: Hash in Perl
by Jim (Curate) on Jan 02, 2014 at 08:39 UTC
    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

Re: Hash in Perl
by rammohan (Acolyte) on Jan 02, 2014 at 07:14 UTC
    Thank you to all well explanation to all.. one more problem i have Thank you well explanation.
    my%hash = (Script => "Perl", script_1=>"Ruby", script_2=> "PHP"); for my$k (keys %{my$href}) { print "$k => ${$href}{$k}\n"; }
    it showing Global symbol $href requires explicit package name.why this error.?
      because $href you're using is not defined
      at least in this snippet
      for my $k (keys %hash) { print "$k => $hash{$k}\n"; }
        Ya but in perldoc showing like
        for my $key (keys %{$href}) { print "$key => ${$href}{$key}\n";
        this also please see and tell me http://perldoc.perl.org/perlreftut.html