fork download
  1. #!/usr/bin/perl
  2.  
  3. my @my_arr = ("test1", "test2", "test3", "test2", "test5", "test2", "test6");
  4. my $what = 'test2';
  5. my $then = 'best2';
  6.  
  7. my %index;
  8. for ( my $i = 0; $i < @my_arr; $i++ ) {
  9. my $value = $my_arr[ $i ];
  10. $index{ $value } = $i if not exists $index{ $value };
  11. }
  12.  
  13. if ( exists $index{ $what } ) {
  14. $my_arr[ $index{ $what } ] = $then;
  15. }
  16.  
  17. print $_, "\n" for @my_arr;
  18.  
Success #stdin #stdout 0s 6000KB
stdin
Standard input is empty
stdout
test1
best2
test3
test2
test5
test2
test6