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

I'm looking for help understanding dereferencing with a complex data structure. I have an array of references to anonymous arrays which in turn contain a scalar as a the first item and a reference to an anonymous hash as the second item:

push @lecshun, [$barename , {package=>$gem, index=>$dexi}];

For some items in the array I want to add another key-value pair to the hash, but I just cannot figure out how to do that. I have only gotten so far:

$lecshun[$dexi - 1] ##? <<-- what to do here ?## = inv($gem);

The subroutine inv is this, although it could be anything:

sub inv { my $pkg = $_[0]; my @pki = qx{cygcheck -i --inst $pkg}; for my $ln (@pki) { next unless $ln=~/^Version\s+:\s(\S+)/; say "Pkg ".$1; return (datum => $1); } }

Maybe I am making this too complicated for myself.

Jan 16, 2025 at 02:07 UTC

A just machine to make big decisions
Programmed by fellows (and gals) with compassion and vision
We'll be clean when their work is done
We'll be eternally free yes, and eternally young
Donald Fagen -> I.G.Y.
(Slightly modified for inclusiveness)

Replies are listed 'Best First'.
Re: Adding a new key-value pair to a nested anonymous hash
by tybalt89 (Monsignor) on Jan 16, 2025 at 02:43 UTC
    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11163708 use warnings; $SIG{__WARN__} = sub { die @_ }; my @lecshun; my $barename = 'a barename'; my $gem = 'ruby'; my $dexi = 0; push @lecshun, [$barename , {package=>$gem, index=>$dexi}]; use Data::Dump 'dd'; dd 'before', \@lecshun; $lecshun[$dexi][1]{newkey} = 'newvalue'; use Data::Dump 'dd'; dd 'after', \@lecshun;

    Outputs:

    ("before", [["a barename", { index => 0, package => "ruby" }]]) ( "after", [ [ "a barename", { index => 0, newkey => "newvalue", package => "ruby" }, ], ], )
Re: Adding a new key-value pair to a nested anonymous hash
by Marshall (Canon) on Jan 16, 2025 at 03:03 UTC
    use strict; use warnings; use Data::Dump qw(pp); my @lecshun; push @lecshun, ["nameB" , {package=>"gemB", index=> 50}]; push @lecshun, ["nameA" , {package=>"gem", index=>44}]; pp \@lecshun; #[ # ["nameB", { index => 50, package => "gemB" }], # ["nameA", { index => 44, package => "gem" }], #] # add a key/value pair to anon hash associated with # name of 'nameA' foreach my $index (0..$#lecshun) { my ($name,$href) = @{$lecshun[$index]}; if ($name eq 'nameA') { $href->{ 'newkey' } = 'NewValue'; #Just add new key/value to the + href! } } pp \@lecshun; #[ # ["nameB", { index => 50, package => "gemB" }], # [ # "nameA", # { index => 44, newkey => "NewValue", package => "gem" }, # ], #] ################################ # Don't need to use indices, this is the same use strict; use warnings; use Data::Dump qw(pp); my @lecshun; push @lecshun, ["nameB" , {package=>"gemB", index=> 50}]; push @lecshun, ["nameA" , {package=>"gem", index=>44}]; pp \@lecshun; #[ # ["nameB", { index => 50, package => "gemB" }], # ["nameA", { index => 44, package => "gem" }], #] # add a key/value pair to anon hash associated with # name of 'nameA' foreach my $ref (@lecshun) { my ($name,$href) = @$ref; if ($name eq 'nameA') { $href->{ 'newkey' } = 'NewValue'; } } pp \@lecshun; #[ # ["nameB", { index => 50, package => "gemB" }], # [ # "nameA", # { index => 44, newkey => "NewValue", package => "gem" }, # ], #]
Re: Adding a new key-value pair to a nested anonymous hash
by Intrepid (Curate) on Jan 17, 2025 at 03:00 UTC

    I thank both tybalt89 and Marshall for their generous help. You guys put me on the right track with your examples and I'm not stuck anymore. FYI, the script I am writing is intended to do a survey of installed and uninstalled perl modules packaged by cygwin volunteers and installable via the setup.exe tool (a gui application). There are 420 perl packages in cygwin as of this writing, a considerable number.

      Intrepid

    Jan 17, 2025 at 02:57 UTC
      Sounds useful!
Re: Adding a new key-value pair to a nested anonymous hash
by BillKSmith (Monsignor) on Jan 17, 2025 at 21:02 UTC
    If you have a large number of references to this structure, it may be worth the trouble to build an index (%referer) to it.
    use strict; use warnings; use Data::Dumper; my @lecshun = ( ['namea', { package => 'gema', index => 'dexia', }], ['nameb', { package => 'gemb', index => 'dexib', }], ); my %referer = ( namea => $lecshun[0][1], nameb => $lecshun[1][1], ); print Dumper \@lecshun; $referer{namea}{new_key} = 'new_value'; print Dumper \@lecshun;
    Bill