in reply to Get total number of characters in a hash (not memory usage)
I'd like to be able to get the character count of any data structure: array of hashes, array of hashes with some array elements, etc... but this works for me for now.use strict; use warnings; use List::Util 'reduce'; sub hash_char_count { my $orders = shift; my $count = 0; foreach my $order (@$orders) { $count += reduce { $a += length $b } 0, %$order; } return $count; } my $orders = [ { 'OrderDate' => '2019-11-01 00:00:00', 'AlternateKey' => 'D', 'Customer' => '1238756', 'SalesOrder' => '10928', 'CustomerName' => 'Dunder Mifflin', 'ShippingInstrs' => 'Standard' }, { 'OrderDate' => '2020-01-15 00:00:00', 'SalesOrder' => '11349', 'Customer' => '1239451', 'AlternateKey' => 'R', 'CustomerName' => 'Vance Refrigeration', 'ShippingInstrs' => 'Standard' }, { 'OrderDate' => '2020-01-22 00:00:00', 'SalesOrder' => '12954', 'Customer' => '1240029', 'AlternateKey' => 'R', 'CustomerName' => 'A1A Car Wash', 'ShippingInstrs' => 'Next Day' } ]; print hash_char_count($orders);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Get total number of characters in a hash (not memory usage)
by tybalt89 (Monsignor) on Jan 29, 2020 at 18:20 UTC |