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??

I have some data scrubbing to do, and to divide and conquer the problem, I decided to fix the easy things first, so that I can work in peace on the trickier stuff.

In essence, I have a hash of hashes that represent the count of attributes seen for a collection of things. Sometimes I see the same attribute more than once, sometimes I see many attributes once, and any combination in between.

Most of the things, though, have one attribute, seen once. I therefore wanted to go throught the hash and put them to one side, and put what remains on the other. The trouble is, that solitary attribute has an abitrary name, so I can't just see if it exists. After futzing around a while and getting nowhere fast and more and more complicated, the following suddenly popped out at me.

Note that the $h{key}{subkey} stuff below is actually coming from something like:

while( <> ) { chomp; my( $key, $subkey ) = split; $h{$key}{$subkey}++; }

(It's the $h{$c}{(keys %{$h{$c}})[0]} == 1 bit that pleases me).

#! /usr/bin/perl -w use strict; my %h; # two different subkeys $h{foo}{bar}++; $h{foo}{rat}++; # one subkey $h{fob}{rat}++; # one subkey seen more than once $h{fod}{car}++; $h{fod}{car}++; for my $c( keys %h ) { if( scalar keys %{$h{$c}} == 1 and $h{$c}{(keys %{$h{$c}})[0]} == +1 ) { print "$c has a ${\(keys %{$h{$c}})[0]}\n"; } else { print "$c is complicated\n"; } }

In reply to Seeing if a hash has one arbitrary key by grinder

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: (2)
As of 2024-04-19 20:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found