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

Let's say, I have a complex variable (don't pay attention to var's data inconsistency. I only gave an example of a complex var to make a point of my question):
my %complex = ( key_of_hash=>{ opt1=>val1, opt2=>val2 }, key_of_arr=>[ 38,'abc',-1829, ['element0',8979,'something else'] ] );
I pass some portion of the variable as a reference somewhere:
my $complexSubVarRef = $complex{'key_of_arr'}[3];
Is there a perl's built-in way to get a reference of the parent, containing subarray, whose reference is saved in $complexSubVarRef?
Or the only ways to do it is iteratively search an entire complex variable or indexing it and searching the index?

Replies are listed 'Best First'.
Re: Getting a parent of a sub (array|hash) in a complex variable
by choroba (Cardinal) on Mar 01, 2018 at 06:28 UTC
    There's no way. Also notice that it's possible to have more than one "parent", some of the parents might even belong to a different structure:
    #!/usr/bin/perl use strict; use warnings; my $child = { find => 'me' }; my %struct1 = ( array => [ 1, 2, 3, 'four', [ 'five' ] , $child ], hash => [ six => 'seven', eight => $child ] ); my @another_struct = (9, 10, $child, 11);

    On the other hand, see Data::Diver for searching nested structures.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Getting a parent of a sub (array|hash) in a complex variable
by LanX (Saint) on Mar 01, 2018 at 11:37 UTC
    > Is there a perl's built-in way to get a reference of the parent,

    No these data structures hold only references (only a "is_child" relation if you want), and like choroba already pointed out many "parents" can have the same "child".

    > Or the only ways to do it is iteratively search an entire complex variable or indexing it and searching the index?

    Many more possibilities:

    • you pass a path ['key_of_arr',3] instead of a sub-ref "somewhere"
    • you keep the needed reference to the interesting parents in deeper nested structures. (beware of circular references and the risk of memory leaks then)
    • you keep the string "key_of_arr" inside deeper nested structures. (no risk of memory leaks then)

    The best strategy depends on the details (which we don't know). Most probably you just need to redesign your data.

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

Re: Getting a parent of a sub (array|hash) in a complex variable
by thanos1983 (Parson) on Mar 01, 2018 at 11:12 UTC

    Hello igoryonya,

    Well there is another module Data::DRef that you might find "kind of useful" for what you are trying to do. It does not search the complex data structures but simplifies the iteration and lookup. Maybe combining it with same a grep etc it will help you get what you want. I have put together a small sample of data extraction based on your sample of code and the module.

    Sample of code bellow:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; use Data::DRef qw( :dref_access :key_access ); my $complex = { key_of_hash => { opt1 => 'val1', opt2 => 'val2', key_of_hash_of_hashes => { opt3 => 'val3', opt4 => 'val4', } }, key_of_arr => [ 38, 'abc', -1829, ['element0', 8979, 'something else'] ] }; # say get_value_for_dref($complex, 'key_of_arr.3.0'); # print Dumper get_value_for_key($complex, 'key_of_hash'); # say get_value_for_keys($complex, 'key_of_hash', 'key_of_hash_of_hash +es', 'opt3'); # print Dumper $complex;

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!