G'day sunil9009,

I strongly suspect poorly named variables are one of your biggest problems here:

Not surprisingly, when you got to printing your results, you had no idea what data was held where!

You probably want something closer to this:

$ perl -Mstrict -Mwarnings -le ' my @file_data = ( "12:10 a america", "12:11 b bombay", "12:12 c calcutta", "12:13 a australia", "2:30 b bhutan", "3:40 n neterland" ); my %name_data; for (@file_data) { my ($time, $name, $loc) = split; push @{$name_data{$name}{time}} => $time; push @{$name_data{$name}{loc}} => $loc; } for my $name (sort keys %name_data) { print "$name: @{$name_data{$name}{time}} : @{$name_data{$name} +{loc}}"; } ' a: 12:10 12:13 : america australia b: 12:11 2:30 : bombay bhutan c: 12:12 : calcutta n: 3:40 : neterland

Other issues:

-- Ken


In reply to Re: how to iterate over multiple hashes with same key value by kcott
in thread how to iterate over multiple hashes with same key value by sunil9009

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.