CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Sorting User Input

 



erdawiro
New User

Sep 9, 2013, 1:21 PM

Post #1 of 4 (691 views)
Sorting User Input Can't Post

Hi, I'm having trouble writing a program that takes a string of user input and sorts it alphabetically.

First I tried assigning the user input to an array but that didn't work.

Then I tried assigning the user input to a scalar and then running the qw function on it but that didn't work.

What's the key to making this work? Am I entire off track?

This is what I have:

print "Please enter two or more words, separated by spaces\n";
my $userInput = <STDIN>;
my $unsorted = qw($userInput);

print $unsorted\n;


BillKSmith
Veteran

Sep 9, 2013, 3:09 PM

Post #2 of 4 (689 views)
Re: [erdawiro] Sorting User Input [In reply to] Can't Post

Use split to split your input string into an list of words.
Then sort that list using the default sort order.

Code
use strict; 
use warnings;
print "Please enter two or more words, separated by spaces\n";
my $userInput = <STDIN>;
my $unsorted = $userInput;

$, = " ";
print sort split( /\s/, $unsorted );


Use the following commands to access the documentation for these functions.

Code
perldoc -f split 

perldoc -f sort

Good Luck,
Bill


erdawiro
New User

Sep 9, 2013, 5:52 PM

Post #3 of 4 (684 views)
Re: [BillKSmith] Sorting User Input [In reply to] Can't Post

Thank you! We covered that in my course and I just didn't make the connection.

Going to have to sharpen up a bit. Smile


Laurent_R
Veteran / Moderator

Sep 10, 2013, 8:28 AM

Post #4 of 4 (671 views)
Re: [erdawiro] Sorting User Input [In reply to] Can't Post

You probably want to chomp the user input before using it.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives