Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

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

G'day sunil9009,

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

  • %hash — we already know it's a hash (by the % sigil), beyond that the name is meaningless: it neither reflects the keys (which are customer names) nor the values (which are times).
  • $word1, $word2, $word3 — all meaningless and tells us nothing about the data they hold.
  • $user — started off as customer names, now they're users.
  • $loc — fairly common as an abbreviation for "location" except it holds customer names not locations.

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:

  • You haven't used strict.
  • You haven't used warnings.
  • You've used the 2-argument form of open.
  • You call die without a message. If you don't want to hand-craft a message, use autodie instead.

-- 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":



  • 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 perusing the Monastery: (4)
As of 2024-03-28 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found