Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by ObiPanda (Acolyte)
on Sep 24, 2023 at 20:34 UTC ( [id://11154644] : perlquestion . print w/replies, xml ) Need Help??

ObiPanda has asked for the wisdom of the Perl Monks concerning the following question:

I very much appreciate the thorough answers I receive here: they have helped me get reacquainted with Perl. The hardest part I still encounter is to understand what the symbols mean, not so much the concepts.

I'm simply asking for a "better" way to accomplish a task I already can do.

An example: I want to know if there's a way to send just one key & value per array/hash - preferably by reference - to a subroutine. In the example code below, I want to send all and only the {Sub_Name}s and their respective values. The code below works and I've come up with two ways to list the Sub_Name. I'm simply wanting to know if there's a better way to do it, so I don't have to make an array or send the entire array of hashes to the subroutine.

#!/usr/bin/env perl #use 5.36.1; use strict; use warnings; use Data::Dumper; use autodie; use File::Find; use File::Copy; use File::Rename; use feature 'fc'; use File::Path qw( make_path ); my $Wait_Time = 10; # Implement Time Delay # Subscription DATA sets my @Subscription = ( { Sub_Name => "Morph", Archive_File => "Morph Archive.txt", }, { Sub_Name => "Analogue", Archive_File => "Analogue Archive.txt", }, { Sub_Name => "Cat", Archive_File => "Cat Archive.txt", }, { Sub_Name => "Zoonotic", Archive_File => "Zoonotic Archive.txt", }, { Sub_Name => "Hydro", Archive_File => "Hydro Archive.txt", }, ); #Subscription of Names my @Subscription_Names_List; for (@Subscription) {push @Subscription_Names_List, $_->{Sub_Name};} List_Subscriptions(\@Subscription_Names_List); for (@Subscription) { Display_Subs(\%$_); } sub Display_Subs { my ($my_Sub) = @_; print "Testing Subscription to: $my_Sub->{Sub_Name}\n"; return $my_Sub; } sub List_Subscriptions { my (@Sub_ARRAY) = @{$_[0]}; # my (@Sub_ARRAY) = @_; print "The Current Subscription List:\n\n"; for (@Sub_ARRAY) { print " \t$_ \n"; } print "\n\n"; }

Replies are listed 'Best First'.
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)
by AnomalousMonk (Archbishop) on Sep 24, 2023 at 23:59 UTC

    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:  <%-{-{-{-<

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
by choroba (Cardinal) on Sep 24, 2023 at 21:07 UTC
    I'm not sure what you're after exactly. You can't send a single key and value from a hash as a reference, as nothing like that exists to be referred to: the hash has two keys, to have a hash with only one of the keys, you need to create a new one.

    If you feel the for loop is too verbose, you can switch to map:

    List_Subscriptions(map $_->{Sub_Name}, @Subscription);
    (It doesn't send a reference, but a list, you need to wrap the map into square brackets to create an anonymous array to refer to).

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
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
by kcott (Archbishop) on Sep 25, 2023 at 13:03 UTC

    G'day ObiPanda,

    From your description, I think you're after something like this:

    $ perl -e ' use Data::Dump; my @Subscription = ( { Sub_Name => "Morph", Archive_File => "Morph Archive.txt", }, { Sub_Name => "Analogue", Archive_File => "Analogue Archive.txt", }, { Sub_Name => "Cat", Archive_File => "Cat Archive.txt", }, { Sub_Name => "Zoonotic", Archive_File => "Zoonotic Archive.txt", }, { Sub_Name => "Hydro", Archive_File => "Hydro Archive.txt", }, ); some_sub([map +{Sub_Name => $_->{Sub_Name}}, @Subscription]); sub some_sub { my ($single_scalar) = @_; dd $single_scalar; return; } ' [ { Sub_Name => "Morph" }, { Sub_Name => "Analogue" }, { Sub_Name => "Cat" }, { Sub_Name => "Zoonotic" }, { Sub_Name => "Hydro" }, ]

    Breaking that down into more manageable chunks:

    • In some_sub(...), the ... represents what you "send ... to a subroutine".
    • What you're sending is [...]. That's an arrayref and covers "preferably by reference". Here, the ... represents the contents of the arrayref.
    • The arrayref contents are generated using map. You'll note that there are two forms: map BLOCK LIST and map EXPR,LIST. The second is used here but, as the EXPR is a hashref ({...}), the leading '{' can be confused with the leading '{' of a BLOCK. Adding a plus, "map +{...", removes that confusion — this is described in more detail about two-thirds of the way down the map page (look for the paragraph beginning with "{ starts both hash references and blocks ...").
    • The hashref EXPR is a key/value pair. The key is Sub_Name; the value is $_->{Sub_Name}; your OP code suggests you understand this — ask if that's not the case. The LIST is @Subscription.
    • In sub some_sub {...}, I've used dd, from Data::Dump, to show that a single scalar is received which is an arrayref with "one key & value per array/hash" as you put it.

    — Ken

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
by LanX (Saint) on Sep 24, 2023 at 21:59 UTC
    References are just scalars, you can easily share a reference to a heavy data structure with negligible costs.

    I'd say just share \@Subscription with your sub and let it browse your data there instead of a costly copy.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

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
by jwkrahn (Abbot) on Sep 24, 2023 at 23:12 UTC
    for (@Subscription) { Display_Subs(\%$_); }

    \%$_ says to take a reference to a hash $_ and de-reference it as a hash %$_ and then take a reference to that hash \%$_. Since you are not modifying that hash you could just do:

    for (@Subscription) { Display_Subs($_); }
    sub List_Subscriptions { my (@Sub_ARRAY) = @{$_[0]}; # my (@Sub_ARRAY) = @_; print "The Current Subscription List:\n\n"; for (@Sub_ARRAY) { print " \t$_ \n"; } print "\n\n"; }

    You are de-referencing the array (copying the whole array) when you could just use the reference:

    sub List_Subscriptions { my ($Sub_ARRAY) = @_; # my (@Sub_ARRAY) = @_; print "The Current Subscription List:\n\n"; for (@$Sub_ARRAY) { print " \t$_ \n"; } print "\n\n"; }
    Naked blocks are fun! -- Randal L. Schwartz, Perl hacker