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

I have a simple program that takes three lines of text from the user, joins them together to make one array and then tries to sort them. The program seems to work ok untill it reaches the sort function. The syntax looks fine so I don't know why it doesn't work. Any advise would be much appreaciated. Here is my code... Thanks
main2(); sub main2{ print "Type first sentance: "; chomp (my $sentence = lc<STDIN>); print "\n"; my @words = split(" ", $sentence); print "Type second line: "; chomp (my $sentence2 = lc<STDIN>); print "\n"; my @words2 = split(" ", $sentence2); print "Type third line: "; chomp (my $sentence3 = lc<STDIN>); print "\n"; my @words3 = split(" ", $sentence3); my @string = join (" ",@words,@words2,@words3); print @string; my @sortedwords = sort @string; print "\n@sortedwords\n"; }

Janitored by davido: Added code tags, and moved from PerlMonks Discussion to Seekers of Perl Wisdom. Please use code tags, please post questions to Seekers of Perl Wisdom section.

janitored by ybiC: Retitle from "sort" because onewordnodetitles hinder site search

Replies are listed 'Best First'.
Re: Sorting doesn't seem to work
by dragonchild (Archbishop) on Oct 07, 2004 at 17:41 UTC
    Your problem is here:
    my @string = join (" ",@words,@words2,@words3); print @string; my @sortedwords = sort @string; print "\n@sortedwords\n";

    Basically, @string contains one element. So, when you sort one element, you end up with a one element array, coincedentally in the same order as when you started.

    If you want some help, a little context would be ... helpful. Things like:

    • What are you trying to do?
    • Why are you trying to do it?

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Sorting doesn't seem to work
by shemp (Deacon) on Oct 07, 2004 at 18:16 UTC
    As mentioned above, join makes a single string out of all the elements in the list. If im guessing correctly, you want to make 1 array out of the 3 words arrays, then sort that entire array. Just do:
    ... my @all_words = (@words, @words2, @words3); my @sorted_words = sort @all_words; ...
    If thats really what you're trying to do.
Re: Sorting doesn't seem to work
by gitarwmn (Beadle) on Oct 07, 2004 at 18:16 UTC
    Actually I'm doing this for a class. I'm supposed to take three seperate lines of text, combine them and then sort them. I see what you are saying but how can I combine the three arrays and then sort them? It was suggested we use the join function but that is apparently not working for me. Thanks for your help!
Re: Sorting doesn't seem to work
by gitarwmn (Beadle) on Oct 07, 2004 at 18:22 UTC
    Thanks, I got it working now!