in reply to Add value from one array element into another of same ID

You seem to need to first index the names by the ids for later retrieval. After populating the new array, you also want to add items that weren't used for replacement (JEAN here). So, we need to remember which ones were used.
#!/usr/bin/perl use warnings; use strict; my @data = map { my %h; @h{qw{ ID Name Number CODE }} = @$_; \%h } ['1212', 'JOE', 'XY1', '6'], ['1212', '', 'XY1', '10'], ['4456', 'MARIA', 'TYX', '6'], ['4456', '', 'TYX', '10'], ['8765', 'JEAN', 'HPO', '6']; my %name; for my $d (@data) { $name{ $d->{ID} } = $d->{Name} if length $d->{Name}; } my @new; my %used; for my $d (grep ! length $_->{Name}, @data) { push @new, { %$d, Name => $name{ $d->{ID} } }; undef $used{ $name{ $d->{ID} } }; } for my $d (@data) { push @new, $d if length $d->{Name} && ! exists $used{ $d->{Name} } +; } use Data::Dumper; print Dumper \@new;
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Add value from one array element into another of same ID
by Anonymous Monk on May 27, 2021 at 18:26 UTC
    This worked for me, just trying to get rid of the warnings at these line:

    #Use of uninitialized value in length... for my $d (grep ! length $_->{Name}, @data) { ...
    and here:
    #Use of uninitialized value in length... push @new, $d if length $d->{Name} && ! exists $used{ $d->{Name} };
      What version of Perl are you running? length undef should return undef without warnings since 5.012 (April 2010), if I remember correctly.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        v5.10.1