1: delete @bar[$i] is not quite good coding practice, although it does compile and do what you mean. If you were using warnings, you would have seen the message "Scalar value @bar[$i] better written as $bar[$i]." The code is generally easier to understand when you use the suggested syntax for deleting a single element from array (or usually a hash).
2: It sounds like you want @bar to be equivalent to be ('t','i','m','e') at the end of the loop. In that case, you don't want to use delete to get rid of them. This removes them from the array, but doesn't shift the remaining elements over to fill the spot. You probably want to use splice instead.
As for getting them in the order you want, I'm a little confused as to why (see below)..my @x = qw/a b c d e f g/; delete $x[3]; print join(':', @x), "\n"; # prints a:b:c::e:f:g <-- note the space between c and e @x = qw/a b c d e f g/; splice @x, 3, 1; print join(':', @x), "\n"; # prints a:b:c:e:f:g
3: Your foreach $foo(@bar) loop never uses the value of $foo. Maybe a foreach $i (0 .. $#bar) would be better suited for the stuff inside the loop.
Without knowing exactly what this code is trying to do, it's hard to help. You want to take a sorted list of characters and do something to them (take chars out, add chars in) so that you get a word, which is nothing close to the original list of characters? What is the real-world significance, if any, of the letters you've chosen?
I'm sorry I can't give any more specific help on your problem, but I'm not sure how (or why) one would sort the letters t,i,m, and e into that order! ;) Let us know what you're heading with this code, and we'll be happy to help more.
blokhead
In reply to Re: Simple array sorting
by blokhead
in thread Simple array sorting
by FireBird34
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |