in reply to Simple array sorting

@bar[$i] has got to be a mistake, i would say try $bar[$i] instead, but that is wrong as well. delete is for hashes, not arrays. Have you tried looking at what @bar really contains via Data::Dumper?:
$VAR1 = [ undef, ${\$VAR1->[0]}, 'e', ${\$VAR1->[0]}, 'i', ${\$VAR1->[0]}, 'm', 't' ];
Not what you thought, huh? Now, if i wanted to create an array that contained every second letter of the alphabet, i would try something like this:
my $i = 1; my @bar = grep $i++ % 2, ('a'..'z');
Next, delete all elements but the second, fourth, and sixth:
@bar = @bar[2,4,6];
You could combine the two steps:
my $i = 1; my @bar = (grep $i++ % 2, ('a'..'z'))[2,4,6];
Finally, push 't' and "@bar" will evaluate to "e i m t". If we could just swap the 't' and the 'e' ...
($bar[0],$bar[-1]) = ($bar[-1],$bar[0]);
Is this what you wanted? Note that this line by itself will not yield the same results if you simply try to cut and paste into your script. Here is my complete script, for convenience:
use strict; use warnings; my $i = 1; my @bar = (grep $i++ % 2, ('a'..'z'))[2,4,6]; push(@bar,'t'); ($bar[0],$bar[-1]) = ($bar[-1],$bar[0]); print "@bar\n";

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: Re: Simple array sorting
by Hofmator (Curate) on Jan 09, 2003 at 08:19 UTC
    delete is for hashes, not arrays

    Picking nits ... from perldoc -f delete:

    delete EXPR Given an expression that specifies a hash element, array element, hash slice, or array slice, deletes the specified element(s) from the hash or array. In the case of an array, if the array elements happen to be at the end, the size of the array will shrink to the highest element that tests true for exists() (or 0 if no such element exists).

    -- Hofmator