Reputation: 303
At first: Apologies for my not-so-good English, I'm a 17-year old German ;-)
I do my apprentice as a web-developer and just stumbled upon a problem:
I need to re-arrange the indexes of an array. The array is basically like that:
@array = ( 'a', 'b', 'c' );
But: it changes dynamically, so it doesnt always have 3 elements, but something over 20. Now, what I need to do is to get it to:
@array = ( 'c', 'a', 'b' );
So, I thought I just needed to do something like that (in a for-loop that loops once for every element in the array and increments $counter)
my $last = $array[$#array];
for($#array)
{
$array[$counter] = $array[$counter + 1];
unshift(@array, $last);
pop(@array);
$counter++;
}
I thought it would do this:
For each element in array (lets say, we are at element 0): Set element index = element index + 1; then add the last array to the beginning and remove the now "real" last array (which is just a duplicate of the beginning now). Then adding 1 to the counter and redo that for the array. But it doesnt really do what I want.
Well, that was my "newbie" try, so again, thats what I just need:
I need to "move" all the elements indexes of the array +1 and cut off the last one then to be at the beginning. The maybe more complicated thing is, that the arrays length is just variable.
In another form: I need to turn
( 'a', 'b', 'c', 'd', ... 'z');
to
( 'z', 'a', 'b', 'c', ... 'y' );
Ive got no idea how to go on and I would appreciate any help.
Thanks :-)
Already thanks for all your anwers! I forgot to say that I cant test it before monday, because Im not at work anymore and I dont have the source with me right now. But thanks, Im sure at least one of your solutions will work!
Upvotes: 1
Views: 1565
Reputation: 160
I think you might want to take a look at List::Util, it's got a shuffle method that literally shuffles list contents.
Upvotes: 0
Reputation: 98388
@array = { 'c', 'a', 'b' };
assigns a single element (a hash reference) to @array; I think you mean
@array = ( 'c', 'a', 'b' );
This for loop:
for ($#array)
only loops over the single value $#array (the last index of @array).
I'm guessing you meant something like:
for ( 0..$#array )
(loop over all the array indicies).
Inside your loop, you seem to be doing two different things; one rotating things with unshift/pop and one moving things with an assignment of elements. Either approach would work (though in both you have errors), but both will not.
You can do:
my $last = $array[$#array];
for ( 0..$#array-1 ) {
$array[$_+1] = $array[$_];
}
$array[0] = $last;
(no separate counter needed; the loop provides one)
or rotate by one (moving the first element to be last) the array one fewer times than there are elements:
for ( 1..$#array ) {
push @array, shift @array;
}
or just do this to take the last element and make it first:
unshift @array, pop @array;
Or you can reassign all the elements at once, using a slice:
@array = @array[ $#array, 0..$#array-1 ];
or
@array[1..$#array, 0] = @array;
All of these produce the change you seem to want, but I don't feel confident you've explained clearly enough what that change is, given how complicated your attempt is compared to what would be needed to do what your example shows.
Upvotes: 7
Reputation: 149736
First, use round brackets for creating arrays in Perl:
@array = ('a', 'b', 'c');
If you want to move the last element to the beginning of the array, you can use:
unshift @array, pop @array;
Upvotes: 4