perl-data-structures

How to get hashes out of arrays in Perl?

I want to write a little "DBQuery" function in perl so I can have one-liners which send an SQL statement and receive back and an array of hashes, i.e. a recordset. However, I'm running into an issue with Perl syntax (and probably some odd pointer/reference issue) which is preventing me from packing out the information from the hash that ...

How can I store multiple values in a Perl hash table?

Up until recently, I've been storing multiple values into different hashes with the same keys as follows: %boss = ( "Allan" => "George", "Bob" => "George", "George" => "lisa" ); %status = ( "Allan" => "Contractor", "Bob" => "Part-time", "George" => "Full-time" ); and then I can reference $boss("Bob") a...

How do I turn a table into a matrix?

If I got a table in a text file such like A B 1 A C 2 A D 1 B A 3 C D 2 A E 1 E D 2 C B 2 . . . . . . . . . And I got another symbol list in another text file. I want to transform this table into a Perl data structure like: _ A D E . . . A 0 1 1 . . . D 1 0 2 . . . E 1 2 0 . . . . . . . . . . But I only need some selected symbol...

Sort by value hash of hash of hashes Perl

I have a hash structure similar to the following: KeyA => { Key1 => { Key4 => 4 Key5 => 9 Key6 => 10 } Key2 => { Key7 => 5 Key8 => 9 } } KeyB => { Key3 => { ...

How can I sort Perl hashes whose values are array references?

Hey I was just wondering if there is a cool "one liner" that would sort my hash holding array references. So I have a bunch of key/values in my hash something like: $DataBase{$key} = \@value; However I would like to sort the hash by the array[0] element. Then loop through 'em. I had this to begin with: foreach my $key (sort {$DataBa...

Search for hash in an array by value

Hello, I have a function which extracts Excel data into an array of hashes like so: sub set_exceldata { my $excel_file_or = '.\Excel\ORDERS.csv'; if (-e $excel_file_or) { open (EXCEL_OR, $excel_file_or) || die("\n can't open $excel_file_or: $!\n"); while () { chomp; ...

Perl: Generating Arrays inside a Complex Hash

In the quest to make my data more accessible, I want to store my tabulated data in a complex hash. I am trying to grow a 'HoHoHoA' as the script loops over my data. As per the guidelines in 'perldsc': push @ { $hash{$column[$i]}{$date}{$hour} }, $data[$i]; The script compiles and runs without a problem, but doesn't not add any data to...

How can I create a nested hash as a constant in Perl?

I want to do, in Perl, the equivalent of the following Ruby code: class Foo MY_CONST = { 'foo' => 'bar', 'baz' => { 'innerbar' => 'bleh' }, } def some_method a = MY_CONST[ 'foo' ] end end # In some other file which uses Foo... b = Foo::MY_CONST[ 'baz' ][ 'innerbar' ] That is, I just want to declare a ...

How do I create an array of hashes and loop through them in Perl?

I'm trying to create an array of hashes, but I'm having trouble looping through the array. I have tried this code, but it does not work: for ($i = 0; $i<@pattern; $i++){ while(($k, $v)= each $pattern[$i]){ debug(" $k: $v"); } } ...

How do I store a 2d array in a hash in Perl?

I am struggling through objects in perl, and am trying to create a 2d array and store it in a hash field of my object. I understand that to create a 2d array I need an array of references to arrays, but when I try to do it I get this error: Type of arg 1 to push must be array (not hash element) The constructor works fine, and set_seqs wo...

How do I create and add anonymous hashes to a known Hash during script execution?

I'll attempt to illustrate this with an example. Take a common example of a Hash of Hashes: my %HoH = ( flintstones => { lead => "fred", pal => "barney", }, jetsons => { lead => "george", wife => "jane", "his boy" => "elroy", }, simpsons => { lead => "homer",...

How do I get a hash slice from a hash of hashes?

I have a hash like so: my %h = ( a => { one => 1, two => 2 }, b => { three => 3, four => 4 }, c => { five => 5, six => 6 } ); print join(',', @{$h{a}{qw/one two/}}); The error I get is: Can't use an undefined value as a...

How do I access the array's element stored in my hash in Perl?

# I have a hash my %my_hash; # I have an array @my_array = ["aa" , "bbb"]; # I store the array in my hash $my_hash{"Kunjan"} = @my_array; # But I can't print my array's element print $my_hash{"Kunjan"}[0]; I am new to Perl. Please help me. ...

How can I get the second-level keys in a Perl hash-of-hashes?

I need to get all of the values for a certain key in a hash. The hash looks like this: $bean = { Key1 => { Key4 => 4, Key5 => 9, Key6 => 10, }, Key2 => { Key7 => 5, Key8 => 9, }, }; I just need the values to Key4, Key5 and Ke...

How do I reference a Perl hash in an array in a hash?

This is the code snippet I am working with: my %photo_details = ( 'black_cat' => ( ('size' => '1600x1200', 'position' => -25), ('size' => '1280x1024', 'position' => 25), ('size' => '800x600', 'position' => 0), ), 'race_car' => ( ('size' => '1600x1200', 'position' => 10), ('size' => '800x600', 'position' => 5), ),...

How to iterate through Hash (of Hashes) in Perl?

Hello, I have Hash where values of keys are other Hashes. Example: {'key' => {'key2' => {'key3' => 'value'}}} How can I iterate through this structure? ...

How can I store an inventory using Perl hashes?

For an assignment in college, we have to make a script in Perl that allows us to manage an inventory for an e-store. (The example given was Amazon). Users can make orders in a fully text-based environment and the inventory must be updated when an order is completed. Every item in the inventory has 3 to 4 attributes: a product code, a ti...

How can I sort a Perl array of array of hashes?

@aoaoh; $aoaoh[0][0]{21} = 31; $aoaoh[0][0]{22} = 31; $aoaoh[0][0]{23} = 17; for $k (0 .. $#aoaoh) { for $i(0.. $#aoaoh) { for $val (keys %{$aoaoh[$i][$k]}) { print "$val=$aoaoh[$i][$k]{$val}\n"; } } } The output is: 22=31 21=31 23=17 but i expect it to be 21=31 22=31 ...

How do I delete a [sub]hash based off of the keys/values of another hash?

Lets assume I have two hashes. One of them contains a set of data that only needs to keep things that show up in the other hash. e.g. my %hash1 = ( test1 => { inner1 => { more => "alpha", evenmore => "beta" } }, test2 => { inner2 => { more => "charlie", somethingelse => "delta" } }, test3 => { inner9999 => { oh...

How do I access a value of a nested Perl hash?

I am new to Perl and I have a problem that's very simple but I cannot find the answer when consulting my Perl book. When printing the result of Dumper($request); I get the following result: $VAR1 = bless( { '_protocol' => 'HTTP/1.1', '_content' => '', '_uri' => bless( do{\(my $o = 'http://myaw...