Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Search and Replace Elements from 1 array to another.

by skp (Initiate)
on Apr 02, 2012 at 22:49 UTC ( #963136=perlquestion: print w/replies, xml ) Need Help??
skp has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I have a need to search and replace portions of array elements from elements of another array.

I am attempting to split both arrays and splice if there is a match. The ideal output would look like this for the first match of element of @dp2 and 4th of @stu2. I also need to keep intact any non matching elements of @stu.

103_dsu 1 35 nbu103 Disk 6 -1

@stu = (103_dsu 1 35 nbu103,104_dsu 1 35 nbu104,204_tape 1 1 ACS1,101_ +dsu 1 35 nbu101); @dp = (nbu103 Disk 6 -1,nbu104 Disk 6 -1,nbu101 Disk 6 -1); foreach $var (@stu) { foreach $var2 (@dp) { @stu2 = split(' ',$var); $s1 = $stu2[3]; @dp2 = split(' ',$var2); $s2 = @dp2[0]; if ($s1 eq $s2) { splice (@stu2,3,1,$var2); } } }

Replies are listed 'Best First'.
Re: Search and Replace Elements from 1 array to another.
by Riales (Hermit) on Apr 03, 2012 at 00:04 UTC

    Does your code compile? O.o

    Anyways, I think you might be overcomplicating things. This seems to do what you want:

    my @stu = ( '103_dsu 1 35 nbu103', '104_dsu 1 35 nbu104', '204_tape 1 1 ACS1', '101_dsu 1 35 nbu101', ); my @dp = ( 'nbu103 Disk 6 -1', 'nbu104 Disk 6 -1', 'nbu101 Disk 6 -1', ); foreach my $stu (@stu) { my (undef, undef, undef, $possible) = split ' ', $stu; my ($matched_dp) = grep { my ($dp) = split ' ', $_; $dp eq $possible } @dp; $stu = $matched_dp if $matched_dp; } use Data::Dumper; print Dumper(@stu);

      Thanks! That is what I was looking for. One addition needed is that if a match was found it would replace the match along with the rest of the pre split element if possible.

      Apologies for not wording that better.

      Current output $VAR1 = 'nbu103 Disk 6 -1'; $VAR2 = 'nbu104 Disk 6 -1'; $VAR3 = '204_tape 1 1 ACS1'; $VAR4 = 'nbu101 Disk 6 -1'; Needed output $VAR1 = '103_dsu 1 35 nbu103 Disk 6 -1'; $VAR2 = '104_dsu 1 35 nbu104 Disk 6 -1'; $VAR3 = '204_tape 1 1 ACS1'; $VAR4 = '101_dsu 1 35 nbu101 Disk 6 -1';

        Shouldn't be much of a change. The only somewhat tricky part might be a fancier use of split in order to avoid having to concatenate the first few values together after splitting them previously. I think this should work:

        # Instead of the existing split in the code I wrote... my ($pre, $possible) = split / ([^ ]+)$/, $stu;

        That way, you can just concatenate $pre to the beginning of $matched_dp when you assign to $stu.

        As a sidenote, am I the only one that was surprised to find that Perl does not have an rsplit function? This is possibly the first time I've found myself thinking 'Gee, if this were Python...' :P

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://963136]
Approved by ww
[marto]: I'm sure this comes as a surprise to no one
[LanX]: ?
[marto]: poorly thought out non products, sold to parents as "fun" things for kids to have, while providing "security" features (track your kid over the interwebs)
[LanX]: oh didn't see the link, poor contrast
[marto]: "Through a few simple steps, a stranger can take control of the watch and track, eavesdrop on and communicate with the child. They will be able to track the child as it moves or make it look like the child is somewhere it is not..." and so on
[LanX]: so Strangers can easily seize control of the children
LanX needs a smartwatch for his boss ...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2017-10-20 14:13 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (263 votes). Check out past polls.