in reply to Simple array sorting

A few things:

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.

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
As for getting them in the order you want, I'm a little confused as to why (see below)..

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

Replies are listed 'Best First'.
Re: Simple array sorting
by FireBird34 (Pilgrim) on Jan 09, 2003 at 06:02 UTC
    5 min after posting this, I got it =)

    Basically I'm putting up a series of programs for people to go through, find a serious error, fix it, then run it to get a hidden letter.

    The only 'real-world significance' of this, would be for debugging purposes, without taking a shortcut in easily reading the code (unless you spent the time, but faster to fix and run).

    Thanks aswell -- did this in a rush, so I didn't put in a few of those things you pointed out ($bar$i instead of @...). Thanks again.