perl-data-structures

How can I merge several hashes into one hash in Perl?

In Perl, how do I get this: $VAR1 = { '999' => { '998' => [ '908', '906', '0', '998', '907' ] } }; $VAR1 = { '999' => { '991' => [ '913', '920', '918', '998', '916', '919', '917', '915', '912', '914' ] } }; $VAR1 = { '999' => { '996' => [] } }; $VAR1 = { '999' => { '995' => [] } }; $VAR1 = { '999' => { '994' => [] } }; $VAR1 = { '9...

How do I create a hash of hashes in Perl?

Based on my current understanding of hashes in Perl, I would expect this code to print "hello world." It instead prints nothing. %a=(); %b=(); $b{str} = "hello"; $a{1}=%b; $b=(); $b{str} = "world"; $a{2}=%b; print "$a{1}{str} $a{2}{str}"; I assume that a hash is just like an array, so why can't I make a hash contain another? ...

New Perl user: using a hash of arrays

I'm doing a little datamining project where a perl script grabs info from a SQL database and parses it. The data consists of several timestamps. I want to find how many of a particular type of timestamp exist on any particular day. Unfortunately, this is my first perl script, and the nature of perl when it comes to hashes and arrays is c...

Perl - Hash of hash and columns :(

Hi folks, I've a set of strings with variable sizes, for example: AAA23 AB1D1 A1BC AAB212 My goal is have in alphabetical order and unique characters collected for COLUMNS, such as: first column : AAAA second column : AB1A and so on... For this moment I was able to extract the posts through a hash of hashes. But now, how can I ...

perl sort key then by subkey--if subkey undef set to null string

my %data ( KEY1 => { SUBKEY1 => "Canada", SUBKEY3 => "75.00", SUBKEY2 => "50.00", }, KEY3 => { SUBKEY2 => "150.00", }, KEY2 => { SUBKEY3 => "200.00", SUBKEY1 => "Mexico", }, ); How do I print a list that is sorted by Keyname and for each keyname sorted by subke...

References in Perl: Array of Hashes

I want to iterate through a reference to an array of hashes without having to make local copies, but I keep getting Can't use string ("1") as an ARRAY ref while "strict refs" errors. Why? How do I fix it? sub hasGoodCar { my @garage = ( { model => "BMW", year => 1999 ...

How can I create a hash of hashes from an array of hashes in Perl?

I have an array of hashes, all with the same set of keys, e.g.: my $aoa= [ {NAME=>'Dave', AGE=>12, SEX=>'M', ID=>123456, NATIONALITY=>'Swedish'}, {NAME=>'Susan', AGE=>36, SEX=>'F', ID=>543210, NATIONALITY=>'Swedish'}, {NAME=>'Bart', AGE=>120, SEX=>'M', ID=>987654, NATIONALITY=>'British'}, ] I would like to write a subroutine that w...

What's the best practise for Perl hashes with array values?

What is the best practise to solve this? if (... ) { push (@{$hash{'key'}}, @array ) ; } else { $hash{'key'} =""; } Is that bad practise for storing one element is array or one is just double quote in hash? ...

How should I store values in a multi-level Perl hash?

I am looking to do something like this. I remember I had some issues with values disappearing when programming like this. Is this type of structure "correct/valid" for a hash? my %VAR; $VAR{SCALAR} = "test scalar"; $VAR{ARRAY}[0] = "test array"; $VAR{HASH}{NAME}[0] = "test hash array 1"; $VAR{HASH}{NAME}[1] = "test hash array 2"; $VAR{H...

How can I dereference an array of arrays in Perl?

Can anyone please tell me how to dereference an array of arrays when passed to a function. I am doing like this: my @a={\@array1, \@array2, \@array3}; func(\@a); func{ @b=@_; @c=@{@b}; } Actually I want the array @c should contain the addresses of @array1, @array2, and @array3. ...

Using a pre-parsed protocol definition in a script and keeping it up-to-date

For my work, I sometimes have to deal with logfiles from a binary protocol (the logfiles contain hexdumps of the messages). I want to write a Perl script that can interpret the binary data for me and print the contents in a more friendly format. I have a (machine readable) description of the protocol messages in a proprietary format and...

Merging inner hashes in Perl

I have 3 dimensional hash and a 2 dimensional hash, and I want to merge the 2 dimensional hash with one of the inner hashes of the 3 dimensional hash, something like this, which is similar to what I do to merge a pair of 2d hashes: my %3dhash; my %2dhash; my $key = "some string"; %3dhash{$key} = ($3dhash{$key}, %2dhash); But when I tr...