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

Re: Hashes, keys and multiple histogram

by choroba (Cardinal)
on Aug 17, 2014 at 07:20 UTC ( [id://1097717]=note: print w/replies, xml ) Need Help??


in reply to Hashes, keys and multiple histogram

$hist1{@element}++;

@element in scalar context returns the size of the array @element. You shift the array before, so the key is the number of original elements minus 1. Are you sure that's what you want to hash by?

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Hashes, keys and multiple histogram
by f77coder (Beadle) on Aug 17, 2014 at 07:29 UTC

    Thanks for the reply. No, it's not. The first column is the data is a switch variable, I need to grab that value from the line and put the rest of the line into the histogram. Each array element should be a key except for the first.

    I want to shove the rest of the array into the keys, loop through the next lines and counts as values

      Each array element should be a key except for the first

      Maybe I had misread this line when I wrote my previous answer. Possibly you really want something like this:

      $hash1{$_}++ for @elements;
      Example under the debugger:
      DB<1> @elements = qw/ 1 3 5 4 6/; DB<2> $hash1{$_}++ for @elements; DB<3> x \%hash1 0 HASH(0x600509af0) 1 => 1 3 => 1 4 => 1 5 => 1 6 => 1

        Thanks for reply.

        At the end, I would expect 3 histograms with the keys being elements of the array and values equal to the count.

        line = "0 1 6 2 68fd1e64 80e26c9b 1f89b562 e5ba7672"

        0 is the variable for the switch, the rest of the line needs to be put into keys and then count/increment.

        hist1={ "0" => 43, "0468d672" => 1, "05db9164" => 7, "07c540c4" => 2, "0a519c5c" => 1, "0b153874" => 14, "1" => 11, "10" => 1, "14" => 1, "15" => 1, "18" => 1, "1e88c74f" => 2, "1f89b562" => 3, "2" => 9, "241546e0" => 1, "287130e0" => 1, "287e684f" => 1, "2c16a946" => 2, } hist2={ "6c9c9cf3" => 1, "7" => 2, "776ce399" => 4, "7cd19acc" => 1, "8" => 1, "80e26c9b" => 2, "8cf07265" => 2, "8efede7f" => 1, "9b5fd12f" => 1, "ad4527a2" => 1, "ae46a29d" => 1, "b0660259" => 1, "bc6e3dc1" => 1, "be589b51" => 1, "d4bb7bd8" => 3, "d833535f" => 1, "e5ba7672" => 7, "f0cf0024" => 2, } hist3={ "3" => 2, "31" => 1, "3486227d" => 1, "361384ce" => 1, "37e4aa92" => 1, "38a947a1" => 1, "38d50e09" => 1, "3c9d8785" => 1, "4" => 3, "439a44a4" => 1, "5" => 3, "510b40a5" => 1, "5a9ed9b0" => 1, "6" => 1, "64523cfa" => 1, "68fd1e64" => 5, }
      An array cannot be the key of a hash. Perl stringifies hash keys, so that hash keys are always strings. Even if you tried something like this:
      $hash1{\@elements}++;
      or
      $hash1{[@elements]++;
      it would not work, because your key would end up being a stringified array ref (and the array content would be lost).

      So either you want to use the string that you've read to be the hash key

      $hash1{$line}++;
      but that does not seem to be very useful in this context, or you want to store an array reference into the value of the hash
      $hash1{"some key"} = \@elements;
      but then I am not sure what you would want your key to be.

      I think you need to have (and provide us) a clearer idea of the data structure that you want to have at the end of your process.

      Quite possibly you really need an array of arrays, rather than a hash of arrays. Quick demonstration under the Perl debugger:

      DB<1> @elements = qw/ 1 3 5 4 6/; DB<2> push @array, \@elements; DB<3> @elements2 = qw/ 12 13 14 15/; DB<4> push @array, \@elements2; DB<5> x \@array 0 ARRAY(0x600509af0) 0 ARRAY(0x600500b38) 0 1 1 3 2 5 3 4 4 6 1 ARRAY(0x600500928) 0 12 1 13 2 14 3 15
      Update: Perhaps I misunderstood your requirement. Please read my next answer on Aug 17, 2014 at 08:50 UTC (immediately below)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-24 18:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found