Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl split, sort question 1

Status
Not open for further replies.

shadyness

MIS
Dec 21, 2001
89
US
I have very limited Perl scripting experience, and I have a question hopefully someone can help me with. I need to write a CGI that receives values from an HTML form text field named "values" that will sort a given Array without using any inbuilt sort function.

The html file asks for input array elements (separated by commas) and your CGI script gives sorted array as output.

Input : 5,3,8,2,7,4
Output : 2,3,4,5,7,8

I have been unable to successfully do this so far. Any help would be greatly appreciated.

--ian <== Some people say they are afraid of heights. With me its table widths. ==>
 
Why exactly can you not use an inbuilt sort then?
Using such a sort makes this trivial:
Code:
#!/usr/bin/perl -w

use CGI qw(:all);

my $fields = param('fields');
my @fields = split(&quot;,&quot;,$fields);
my @sorted = sort { $a <=> $b } @fields;
{
  local $&quot; = &quot;,&quot;;
  print &quot;@sorted\n&quot;;
}
Run this like:
Code:
test.pl fields=4,5,2,1,3
This returns:
Code:
1,2,3,4,5
Obviously, your code would need some more robust error checking. Cheers, Neil :)
 
Ian,

If you're having a specific problem with something you're trying to do -- whether it's homework or not -- then ask a specific question.

For instance - if you need to understand how to do, say a bubble sort, then have a go yourself first and ask a question if you get stuck. Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884
 
Mike and raider,

This is not a homework assignment, as much as it was question I had based from a discussion at work. I work almost totally in VB, SQL, and ASP, never straying far from the M$ world and now my work recently created a dedicated Apache server for the FE, that is accessing a MySQL base. The only Perl reference I have is “Essential Perl 5 for Web Professionals”, and this text, though good, only offers several examples and does not stray far in the direction of array manipulation. I have programmed this same thing before using VB with no problem at all (counting characters between a delimiter, using the left-right function, and then evaluating the numeric value of each item in the new collection created.) I could not accomplish this same action in Perl, and simply came here prior to purchasing another Perl reference.

As far as the other comments go, I wouldn’t go to grad school because I would rather get paid to solve questions like this. I am not looking for “full code solution” as much as a methodology for performing such an action. So, when you mention “ask a specific question”, I felt that was what I had done. Explained my experience level using this language, and then expressed what I wanted to do. Since, I wasn’t looking for a code answer I didn’t post what I had attempted so far, which not only didn’t work, but was not the manner by which I wanted to find a solution, regardless.


#!/usr/bin/perl

# Get the input
read(STDIN, $buffer, $ENV{'terms'});

# Split the name-value pairs
@pairs = split(/,/, $buffer);

foreach $pair (sort @pairs) {

print &quot;<p>\n <center>\n <h1>Results</h1>\n </center>\n&quot;;
print &quot;<li><b>Values:</b> &quot;;
$i = 0;
foreach $term (@terms) {
print &quot;$term&quot;;
}
}


Thank you for all of the replies. Thank you, neil for your help.
<== Some people say they are afraid of heights. With me its table widths. ==>
 
Fair enough, we get a bit paranoid about people being lazy about homework stuff here, drives everyone mad :)

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884
 
I would suggest you:

[1] Check out the official Perl site for some good reference material:




[2] Consider grabbing some functions from the CGI packages. This should already be available with your perl distribution. It makes reading parameters from GET and POST requests a doddle. Try looking for some information on:

CGI, CGI::Lite, CGI::Application

[3] Use the perl man pages to give you some more information. These may seem technically daunting at first, but persevere :) Try the following:

man perl
man perlfunc
man perlvar
man perlop

Hope some of this helps :)
 
And to print the output as a table:
Code:
#!/usr/bin/perl -w

use CGI qw(:all);

my $fields = param('fields');
my @fields = split(&quot;,&quot;,$fields);
my @sorted = sort { $a <=> $b } @fields;
print &quot;<TABLE>\n&quot;;
{
  local $&quot; = &quot;\n&quot;;
  print &quot;@{ [ map { TR(td($_)) } @sorted ] }\n&quot;;
}
print &quot;</TABLE>\n&quot;;
Cheers, Neil :)
 
That's exactly what I was looking for. Those links are helpful, but your number two point, up there, I think is what I am really missing. Good parameters for handling GET/POST data. I am too used to the MS server handling all of this, &quot;un-webifying&quot; data for me, and just Requesting it. Thanks for the help. <== Some people say they are afraid of heights. With me its table widths. ==>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top