Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: replacing order element in array

by GrandFather (Saint)
on Apr 18, 2007 at 20:55 UTC ( [id://610843]=note: print w/replies, xml ) Need Help??


in reply to replacing order element in array

Updating the code that I gave in your last post on this topic gives:

use strict; use warnings; my @array1 = (1,2,3,4); my @array2 = (1,5,3,6,8,2,4); my %hash; use constant kReplace => 3; @hash{@array1} = @array1; my @only2 = grep {! exists $hash{$_}} @array2; # remove the unwanted "value" from array 2 @array2 = grep {$_ != kReplace} @array2; # Substitute a new value in array 1 for (@array1) { next if $_ != kReplace; $_ = shift @only2; last; # Assuming there will only be one value to replace } print "array 1: @array1\n"; print "array 2: @array2\n";

which prints:

array 1: 1 2 5 4 array 2: 1 5 6 8 2 4

DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://610843]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-18 06:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found