gatornek has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I'm brand spankin' new to Perl and am still having trouble picking up the syntax necessary to execute some basic data movement structures. I'm trying to do a sort on an array of "read-in" letters using <STDIN> (yes, I know perl has many built in sorting functions, but that's not the point as I'm sure my teacher would iterate. Trying to use what I know of arrays in java, my perl code for a simple binary sort ends up looking like spaghetti. Does anyone have a quick implementation of a sort on an @array of letters?

Replies are listed 'Best First'.
Re: Tedious Array Sort
by gjb (Vicar) on Feb 05, 2003 at 08:15 UTC

    One of the rules of PerlMonks is not to do homework, so you won't get a reply to this question. The same applies to questions amounting to "please do my work for me".

    However, you may get some help if you post the code you already have and point out specific questions about that.

    At any rate, you'll find the Tutorials on this site helpful.

    Just my 2 cents, -gjb-

Re: Tedious Array Sort
by Coruscate (Sexton) on Feb 05, 2003 at 08:12 UTC

    I did not notice the words "as I'm sure my teacher would iterate" in the original post, and have therefore removed my complete ready-to-use answer. Damn, a question I answered correctly too :)

    As a replacement, I offer some advice. Don't ask others to do your homework. Ask for pointers as to where to look the information up and do the work yourself. I'll pretend that's what you asked of us and give you the pointers:

    • split will allow you to split a string using a pattern or character(s). Your example will make good use of a line such as split('', $string);. The '' splits a string on every character.
    • join is split's evil twin. Your example might want to use join('', $sorted_letters);.
    • sort: Guess what? You do want to use Perl's sorting functions. They make life easy. Specifically, learn the difference between <=> and cmp for comparing. Also, you'll want to use either lc or uc to sort alphabetically instead of by ASCII value.
    • my $line = <STDIN>; will get a line of text (ending in a newline "\n") from the command line.
    • chomp: Will remove the newline from a line of text. Using it to remove the newline from <STDIN> will be useful.

Re: Tedious Array Sort
by demerphq (Chancellor) on Feb 05, 2003 at 10:25 UTC
    I have to admit that your question does not make much sense to me. While there are hits on google for "binary sort", there doesn't seem to be a specific sorting method with this name. For instance the sorts who seem to have been called this include radix sort and binary insertion sort.

    Anyway, here is a solution. I'm not going to say which one it is, I suggest you look into it yourself If you are tempted to hand this in to your teacher.

    my $text="qwertyuiopasdfghjklzxcvbnm"; my @ltrs=split //,$text; my ($val,@set,@unset)=(1); while ($val) { push @{(ord($_) & $val) ? \@set : \@unset}, $_ foreach @ltrs; $val <<= 1; @ltrs=( splice( @unset, 0, @unset ), splice( @set, 0, @set ) ); } print join("-",@ltrs)."\n"
    PS, please show a bit more effort in your posts. You'll get more support and feedback.

    --- demerphq
    my friends call me, usually because I'm late....

Re: Tedious Array Sort
by bart (Canon) on Feb 05, 2003 at 09:52 UTC
    I'm brand spankin' new to Perl and am still having trouble picking up the syntax necessary to execute some basic data movement structures.
    I assume you mean "some basic data structure movement". One keyword: splice. That lets you insert any number of items in an array at any point — optionally deleting a few. Now splice() isn't the simplest of commands in Perl, so maybe I should point you to one of my previous replies on the subject that, so I've been told, has been promoted to tutorial: Re: [splice], explanation please?