Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Array searching

by scoobyrico (Beadle)
on Apr 12, 2007 at 23:25 UTC ( [id://609784]=perlquestion: print w/replies, xml ) Need Help??

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

I have two arrays of numbers, I want to removed a number (in this case 2) from the first and second arrays and replace it with a number from the second array with out duplicating numbers in the first array. Here is my attempt, though it's not working:
#!/usr/bin/perl use strict; use warnings; my @array1 = (1,2,3); my @array2 = (1,2,3,4,5); my %hash; my %hash2; @hash{@array1} = @array1; @hash2{@array2} = @array2; # get initial length my $len = keys %hash; # remove the unwanted "value" my $old = delete $hash{2}; my $old2 = delete $hash2{2}; # insert a new one foreach my $x (@array2) { next if ($x == $old); $hash{$x} = undef; last if (keys %hash == $len); } @array1 = keys %hash; foreach my $yy (@array1){ print "##".$array1$[yy]."##\n"; }

Replies are listed 'Best First'.
Re: Array searching
by GrandFather (Saint) on Apr 12, 2007 at 23:55 UTC

    It may be that something like the following is what you are after:

    use strict; use warnings; my @array1 = (1,2,3); my @array2 = (1,2,3,4,5); my %hash; @hash{@array1} = @array1; my @only2 = grep {! exists $hash{$_}} @array2; # remove the unwanted "value" @array1 = grep {$_ != 2} @array1; @array2 = grep {$_ != 2} @array2; push @array1, shift @only2; print "array 1:\n"; print "$_\n" for @array1; print "array 2:\n"; print "$_\n" for @array2;

    Prints:

    array 1: 1 3 4 array 2: 1 3 4 5

    DWIM is Perl's answer to Gödel
Re: Array searching
by thezip (Vicar) on Apr 12, 2007 at 23:59 UTC

    I think this might meet your specs:

    Updated -- added last; statement in loop
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @array1 = (1,2,3); my @array2 = (1,2,3,4,5); my %hash = map { $_ => 1 } @array1; my %hash2 = map { $_ => 1 } @array2; print "Before:\n" . Dumper(\%hash) . Dumper(\%hash2); # remove the unwanted "value" my $to_delete = 2; my $old = delete($hash{$to_delete}); my $old2 = delete($hash2{$to_delete}); for my $x (keys %hash2) { next if exists($hash{$x}); $hash{$x} = 1; delete($hash2{$x}); last; # updated -- added this line } print "After:\n" . Dumper(\%hash) . Dumper(\%hash2); __OUTPUT__ Before: $VAR1 = { '1' => 1, '3' => 1, '2' => 1 }; $VAR1 = { '4' => 1, '1' => 1, '3' => 1, '2' => 1, '5' => 1 }; After: $VAR1 = { '4' => 1, '1' => 1, '3' => 1, }; $VAR1 = { '1' => 1, '3' => 1, '5' => 1, };


    Where do you want *them* to go today?

      Umm, no. OP wrote "... and replace it with a number from the second array with out duplicating numbers in the first array".

      It's not clear if OP intended to add a new element to the first array or replace the deleted element's value. That is, it's not clear if the ordering of the array is important of not, but it is clear that the first array should get a new element from the second array that doesn't already exist in the first array.


      DWIM is Perl's answer to Gödel

        I spoke with OP in CB and he stated that order was *not* important, and that duplication was not allowed.

        Based upon this additional info, I solved the task using hashes.


        Where do you want *them* to go today?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-23 06:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found