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


In reply to Re: Simple array sorting by blokhead
in thread Simple array sorting by FireBird34

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.