Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Count array element in HASH of ARRAYS

by halfcountplus (Hermit)
on Jul 20, 2015 at 13:47 UTC ( [id://1135446]=note: print w/replies, xml ) Need Help??


in reply to Count array element in HASH of ARRAYS

#!/usr/bin/perl -w use strict; my %HoA = ( key1 => [ "50001", "01" ], key2 => [ "50011", "02" ], key3 => [ "50010", "01" ], key4 => [ "50041", "02" ], key5 => [ "50301", "02" ], key6 => [ "50701", "09" ], key7 => [ "50801", "09" ] ); my %containers; while (my ($k, $v) = each (%HoA)) { push @{$containers{$v->[0]}}, $k; } foreach (sort keys %containers) { print "Container $_ has ".@{$containers{$_}}." keys.\n" }
This retains the keys in the `%containers` hash, if you don't need to do that you could replace the loops with:
my %containers; while (my ($k, $v) = each (%HoA)) { $containers{$v->[1]}++; } foreach (sort keys %containers) { print "Container $_ has $containers{$_} keys.\n" }

Replies are listed 'Best First'.
Re^2: Count array element in HASH of ARRAYS
by AnomalousMonk (Archbishop) on Jul 20, 2015 at 14:05 UTC

    Just curious: Are the
        $containers{$con} = [ ] if !exists $containers{$con};
    or
        $containers{$con} = 0 if !exists $containers{$con};
    statements strictly (or warnings-ly) necessary with your Perl version? They aren't with 5.8.9 and 5.14.4 — but they certainly do no harm in any case.


    Give a man a fish:  <%-(-(-(-<

      Seems they aren't necessary even with strict, no. I'm just prone to:
      use paranoid qw(really really);
      Which is not to say it's a good thing. In fact I'll take those out...they might be taken to improve readability or something but not significantly IMO.
      $containers{$con} = 0 if !exists $containers{$con};

      Above one works. Statement "Strictly" is in use and perl version is 5.10.1, no error / warning

Re^2: Count array element in HASH of ARRAYS
by stm (Initiate) on Jul 20, 2015 at 14:06 UTC

    halfcountplus's method works. Thank you

      Yes, fine if this gives you what you want, but, you will notice that this solution first needs to reorganize the data structure into another data structure (%containers) to make the counting easy.

      So I think that you should really not neglect stevieb's useful comment (in post Re: Count array element in HASH of ARRAYS) that it might be better to build in the first place a data structure making your requirement easier to implement. It may or may not be possible in your specific case, but if there is a way of building something like %containers in the first place, then you are in a much easier solution.

        Yes, I agree. Stevieb made a valid point. I will try that.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1135446]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found