Date: Wed, 31 Jul 2002 17:37:12 -0500 (Central Daylight Time) From: Mark Jacobson To: 810-088-06@uni.edu Subject: Question about SORT, sort of answered... ---------- Forwarded message ---------- Date: Wed, 31 Jul 2002 5:30 p.m. From: Mark Jacobson To: Subject: Re: Sorting, when to do it... On Wed, 31 Jul 2002, Frodo B. Baggins wrote: > Mark, > One thing I'm not understanding is exactly how to sort. I think that > seems to be the biggest gliche I'm having right now. I've got the while > loop where I > > 1.split > 2.shift > 3.push > 4.join > 5. $movies[$n++] > > So now I'm not sure how to sort and where in this it goes. I thought you > said Tuesday that it went between #1 and #2 in the while loop, but I > could be wrong. > > Thanks. > Frodo Hello Frodo, The sorting goes AFTER the while loop is all done, AFTER everything has been read into the array from the file. ----- Study the example on class36.html to see how I did it. The file was READ INTO AND STORED IN THE ARRAY during lines 11 to 16 of the CGI script: See URL http://student.cns.uni.edu/~venkman/cgi-bin/showSortByLength.cgi 18 for ($i = 0; $i < @movies; $i++) { 19 @record = split (/\t/, $movies[$i]); 20 $quoteLen = &leadZeroLen( length $record[0] ); 21 $nameLen = &leadZeroLen( length $record[1] ); 22 $movieName = $record[1]; # movies with same length names 23 unshift(@record, $quoteLen); # or could do these TWO 24 unshift(@record, $movieName); # $r = $nameLen . $movieName . 25 unshift(@record, $nameLen); # $quoteLen; 26 # unshift(@record, $r); 27 $rec = join ("\t", @record); 28 $movies[$i] = $rec; } What did LINES 18-28 do? Added the new information to the each record of the @movies ARRAY. Rocky movie record with quote Yo, Adrian! would now have this format: 0005\tRocky\t0011\tYo, Adrian\tRocky\tSylvester Stallone\tTalia Shire\tDrama ---- ----- ---- The new information is UNDERLINED ---------- Rocky is length 5, hence 0005 Yo, Adrian! is length 11, hence 0011 29 @movies = sort( @movies ); And that is what we SORT the @movies ARRAY on, now that its all ready to be sorted with our records preceeded by the proper info. You do NOT have to do anything this complicated for your project, but the above illustrates the basic techniques you can adapt to the simpler task. 30 foreach (@movies) { 31 @record = split (/\t/, $_); 32 shift(@record); shift(@record); shift(@record); 33 $rec = join ("\t", @record); 34 $_ = $rec; } What does line 32 do? It throws away the 005, the Rocky and the 0011. 35 return @movies; What does the last line of code in the sub do? Returns the array SORTED and arranged just as we need it for producing the desired web page output. Return statement RETURNs the array containing the original file contents, but processed and rearranged as we wished, to the main program, i.e. to this statement: 55 @myMovies = &sortByNameAndQuoteLength("moviesPERL.txt"); The @myMovies array catches the quarterback's pass and goes downfield to the 56 yard line, or statement #56, and there does a lateral to the makeNestedOLtags() function or sub or procedure. 56 $nestedOL = &makeNestedOLtags(@myMovies); makeNestedOLtags is a sub, but I will not show its code here. It is very relevant for your assignment tasks. ------------- Study the game films and watch how makeNestedOLtags performs on the football field. It is very educational and will help you in your upcoming game. The opponent is tough, but studying how &makeNestedOLtags() goes in and takes on his or her opponents on the field will make it much easier to be ready to go by Friday. 56 $nestedOL = &makeNestedOLtags(@myMovies); This sub is a 2nd string player (get it, get it, i hope so). It returns the string of OL and LI and FONT COLOR=blue and \n and P and /FONT and /OL and B and /B and FONT COLOR=red and movie names and movie quotes. The main program then runs the football into the end zone, which is the print statments that send it to the client browser. Line 58 was the whistle that started the play after a time out. Lines 59 through 66 take it on home with ONE PRINT statement, as time runs out and the referee blows the whistle ending the game. 59 print start_html(-title => "SORTED by length - July 31st, 2002", 60 -BGCOLOR => "EFEFAB"), 61 "\n\n
\n", 62 h1("Movies sorted by Length and Nested OL lists"), 63 "\n
\n", 64 $nestedOL, "\n
\n", 65 end_html, 66 "\n\n"; Hope this helps. Be sure to study this over from page class36.html where you can see all the code and also run the example program. As soon as I figure out the JavaScript, I will have one of them open a separate browser window, so you can see them side by side - the PERL/CGI code and the resulting web page output. http://www.cns.uni.edu/~jacobson/088/class36.html Mark