Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: changing the position of an element in an array

by blazar (Canon)
on Nov 17, 2005 at 13:29 UTC ( [id://509399] : note . print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    my $pos;
    for (0..$#array) {
    ...
          if $array[$_] eq $wanted;
    }
    @array[$pos,-1]=@array[-1,$pos];
    
  2. or download this
    my $pos;
    for (0..$#array) {
    ...
          if $array[$_] eq $wanted;
    }
    @array=@array[0..$pos-1, $pos+1..$#array, $pos];
    
  3. or download this
    @array=((grep $_ ne $wanted, @array), $wanted);
    
  4. or download this
    @array = do {
        my $i;
    ...
        (grep $i || $_ ne $wanted || $i++, @_),
          $i ? $wanted : ();
    }