Handles any nesting of [] and {}
#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11111985 use warnings; use List::Util qw( sum0 ); 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' } ]; sub charcount { my $tree = shift; ref $tree or return length $tree; sum0 map charcount($_), ref $tree eq 'HASH' ? %$tree : @$tree; } print charcount($orders), "\n";
In reply to Re^2: Get total number of characters in a hash (not memory usage)
by tybalt89
in thread Get total number of characters in a hash (not memory usage)
by TieUpYourCamel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |