There's more than one way to do things PerlMonks

### changing the position of an element in an array

 on Nov 17, 2005 at 06:49 UTC Need Help??
s_gaurav1091 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am trying to find the position of an element in an array and then moving that element to the last position in the array.i.e the last element will then become the second last.How should i do that...please help...
• Comment on changing the position of an element in an array

Replies are listed 'Best First'.
Re: changing the position of an element in an array
by jbrugger (Parson) on Nov 17, 2005 at 06:55 UTC
Trying yourself is allways good :)
hint: Use grep to find your element, splice to delete it, then push it on the array.
Ps. Also use the Super Search, all of your 3 sub-questions have been answered here many times, and there are many ways to do it.

"We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: changing the position of an element in an array
by ikegami (Pope) on Nov 17, 2005 at 07:19 UTC

Finding the position could be done as follows:

```my (\$pos) = grep { ... } 0..\$#array;

where ... checks \$array[\$_] for the condition.

Once you find the position,

```push(@array, splice(@array, \$pos, 1))

will do the trick nicely.

Actually a simple splice/push will not change the position of an element in an array, it will create a new element that comtains a copy of the value from the old of element.
```my @array = ( 'foo', 'bar', 'baz' );
my (\$pos) = grep { \$array[\$_] eq 'bar' } 0..\$#array;
my \$ref_to_element = \\$array[\$pos];
push (@array, splice(@array, \$pos, 1));
print "@array\n"; # Prints 'foo baz bar'
\$\$ref_to_element = 'zap';
print "@array\n"; # Still prints 'foo baz bar'

Had \$array[1] been moved to the end of the array then \$\$ref_to_element would point to \$array[2] after the move.

Admittedly, this is usually not an issue.

On the rare occasions when you really want to move elements of an array use Array::Splice. In the above example you would use push_aliases() in place of the built-in push().

Re: changing the position of an element in an array
by neosamuri (Friar) on Nov 17, 2005 at 07:00 UTC

Here is one type of approach that you can use.

```for( \$i = 0; \$i < @array; \$i++){
if( \$array[\$i] == \$look ) {
@out = (@array[0..(\$i-1)], @array[(\$i+1)..scalar(@array)], \$arra
+y[\$i]);
last;
}
}
Re: changing the position of an element in an array
by blazar (Canon) on Nov 17, 2005 at 13:29 UTC
```my \$pos;
for (0..\$#array) {
\$pos=\$_ and last
if \$array[\$_] eq \$wanted;
}
@array[\$pos,-1]=@array[-1,\$pos];

And what if no one of them matched? (Left as an exercise to the reader)

Update: it was a wonderfully neat solution (which is the reason why I posted it!) to another problem!!

Not just as neat:

```my \$pos;
for (0..\$#array) {
\$pos=\$_ and last
if \$array[\$_] eq \$wanted;
}
@array=@array[0..\$pos-1, \$pos+1..\$#array, \$pos];

And what if no one of them matched? (Left as an exercise to the reader ;-)

But then if you are sure in advance that one and only one element will match, here's a neat concise solution:

```@array=((grep \$_ ne \$wanted, @array), \$wanted);
You can patch it so that it works also if the above conditions are not verified, but won't be just as elegant. (Also left as an exercise to the reader!)

Create A New User
Node Status?
node history
Node Type: perlquestion [id://509310]
Approved by jbrugger
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2017-06-27 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (615 votes). Check out past polls.