in reply to How can I send a "transposed slice" of an array of hashes and by extension an array of arrays, or hash of hashes to a subroutine

I, too, am a bit confused about just what you want. See much more about working with complex data structures in the Perl Data Structures Cookbook.

However, here's an approach that might be useful:

Win8 Strawberry 5.8.9.5 (32) Sun 09/24/2023 19:20:01 C:\@Work\Perl\monks >perl use strict; use warnings; # Subscription DATA sets my @Subscription = ( { 'Sub_Name' => "Morph", 'Archive_File' => "Morph Archive.txt", } +, { 'Sub_Name' => "Cat", 'Archive_File' => "Cat Archive.txt", } +, { 'Sub_Name' => "Hydro", 'Archive_File' => "Hydro Archive.txt", } +, { 'Sub_Name' => "Foo", 'Zip' => "Xyzzy Archive.txt", } +, { 'Sub_Name' => "Bar", } +, { 'Xyzzy' => "Foo", 'Archive_File' => "Xyzzy Archive.txt", } +, { 'Xyzzy' => "Foo", 'Zot' => "Xyzzy Archive.txt", } +, { 'Xyzzy' => "Bar", } +, ); key_extract_and_handle(\@Subscription, 'Sub_Name', \&hashref_handler_e +xample); exit; sub key_extract_and_handle { my ($ar_hashes, # required: ref to array of refs to hashes (A +oH) $key_to_handle, # required: string: key in each hash to handl +e $cr_handle, # required: ref to code of handler ) = @_; for my $hash_ref (@$ar_hashes) { $cr_handle->($hash_ref, $key_to_handle); } } sub hashref_handler_example { my ($hashref, # required: ref to hash $key_to_handle, # required: string: key in hash to handle ) = @_; print "checking subscription "; if (exists $hashref->{$key_to_handle}) { print "'$hashref->{$key_to_handle}' "; } else { print "(exception: subscription key '$key_to_handle' absent) \ +n"; return; # or maybe warn or die } print "via archive file "; if (exists $hashref->{'Archive_File'}) { print "'$hashref->{'Archive_File'}': "; } else { print "(exception: archive file absent) \n"; return; } # do actual subscription currency check here print "ok \n"; # or whatever... } ^Z checking subscription 'Morph' via archive file 'Morph Archive.txt': ok checking subscription 'Cat' via archive file 'Cat Archive.txt': ok checking subscription 'Hydro' via archive file 'Hydro Archive.txt': ok checking subscription 'Foo' via archive file (exception: archive file +absent) checking subscription 'Bar' via archive file (exception: archive file +absent) checking subscription (exception: subscription key 'Sub_Name' absent) checking subscription (exception: subscription key 'Sub_Name' absent) checking subscription (exception: subscription key 'Sub_Name' absent)

Update: Posting error at about line 18
    key_extract_and_handle(\@Subscription, 'Sub_Name', \&hashref_handler_example);
Semicolon was missing at end of statement. Fixed.


Give a man a fish:  <%-{-{-{-<

  • Comment on Re: How can I send a "transposed slice" of an array of hashes and by extension an array of arrays, or hash of hashes to a subroutine (updated)
  • Select or Download Code