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

good morning. I am working with an array reference of hash references. i am looking to match a particular set of keys, but I am returning a subset of that array ref hash refs and searching that array for the full combination of keys. then I set the $inn_href undef and grep over the original aref to remove any found sets of keys reducing the search size. The problem is that the array returned by grep are not references to $aref_hrefs_inn, so $aref_hrefs_inn is not reduced. Does anyone know how I can get the references correctly in my search array so that when i set it to undef i set the original array refs values to undefined?

Update: Just for some clarity, $aref_hrefs_inn is an array reference of hash references. I want to find a subset of that array, looking for one particular key->value pair of which there are multiple in the array reference. from there I want to loop through this subset to match other keys. I want to modify my large array reference of hash references in place by removing any rows that I found. My apologies if the codes isn't clear as code is sort of long, and I posted this example quickly. Ill try to come up with a functioning example easily.

thanks -theleftsock

@$search_array = grep{$_->{key_inn} eq $$out_href{key_search}} @$aref_ +hrefs_inn; foreach my $inn_href (@$search_array) { if (check_match($args_href) == 1) { if ($$flags_href{unique} == 1) { $inn_href = undef; } last; } } @$aref_hrefs_inn = grep {$_} @$aref_hrefs_inn;

Replies are listed 'Best First'.
Re: grep return a set of references
by tobyink (Canon) on Jan 11, 2013 at 16:35 UTC

    You could use map and return scalar references to the original hashrefs...

    @$search_array = map {$_->{key_inn} eq $$out_href{key_search} ? \$_ : () } @$aref_hrefs_inn;

    Of course, in your code which loops through @$search_array you'll need to apply ${...} here, there and everywhere to dereference the scalar refs.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: grep return a set of references
by kennethk (Abbot) on Jan 11, 2013 at 16:37 UTC
    I'm having some trouble following your question/spec. See How do I post a question effectively?. I particular, providing input and expected output would probably provide a lot of clarity.

    I think you are saying you want to remove already processed elements of the array referenced in $aref_hrefs_inn. You've correctly identified that the element of @$search_array is not linked by lvalue. So, why not use a hash to cache which elements need to go?

    my %to_be_deleted; foreach my $inn_href (@$search_array) { if (check_match($args_href) == 1) { if ($$flags_href{unique} == 1) { $to_be_deleted{$inn_href}++; } last; } } @$aref_hrefs_inn = grep !$to_be_deleted{$_}, @$aref_hrefs_inn;

    Code untested, as you've got a lot going on there.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.