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

Ok, I finished the programming of a script that reads a text file and sorts it alphabeticaly, but i am doing something wrong. I have the text file set up like this:

A
D
F
E
G
C
D
G
D
E

Where A, D, F, E, G are in the same cat, so i have the script put them on one string. and so forth where every 5 are a group, and it only sorts the first word from each group.
It works so far, but when it resaves it to the file, it adds a space before and somethimes after on everyword. It didn't do this untill I implented the alphabetical sort. This is my code, can you please tell me what is wrong.
#!c:/perl/bin/perl print "Content-type: text/html\n\n"; use CGI; ####################################################### ## © Copyright 2002 All Rights Reserved ## ## Mr. Andrew Jackson ## ####################################################### ####################################################### ##This little bugger here takes the .txt database and## ##sorts it in alphabetical order from A to Z ## ####################################################### open(items, "itemdat.txt") or die "could not open file"; @list1 = <items>; close(items); $kn = 0; for ($nk = 0; $nk <= $#list1; $nk){ $item1 = @list1[$nk]; chomp($item1); $nk++; $item2 = @list1[$nk]; chomp($item2); $nk++; $item3 = @list1[$nk]; chomp($item3); $nk++; $item4 = @list1[$nk]; chomp($item4); $nk++; $item5 = @list1[$nk]; chomp($item5); $nk++; @list[$kn] = "$item1&$item2&$item3&$item4&$item5"; $kn++; } $nk = 0; foreach $list (@list){ local($item1, $item2, $item3, $item4 ,$item5) = split(/&/, $list) +; @list1[$nk] = "$item1 \n"; $nk++; @list1[$nk] = "$item2 \n"; $nk++; @list1[$nk] = "$item3 \n"; $nk++; @list1[$nk] = "$item4 \n"; $nk++; @list1[$nk] = "$item5 \n"; $nk++; } open(items, ">itemdat.txt"); print items "@list1"; close(items); #################################### ##This is the end of little bugger## #################################### print "<title>Diablo II Broth - Items</title><body bgcolor='black' tex +t='white'>"; open(items, "itemdat.txt") or die "could not open file"; @contetnsoffile = <items>; close(items); $nk = 0; print "<font style='font-size: 23; font-family: Tahoma; color: Blue'>< +center>The Items We Own as of Now<br><font style='font-size: 12; colo +r: red'>In the order entered into the database</font></center></font> +<p>&nbsp"; print "<center><table width='60%' border='0'>"; print "<td align='center'><b><font color='green'>Name</font></b></td>< +td align='center'><b><font color='green'>Status</font></b></td><td al +ign='center'><b><font color='green'>Type</font></b></td><td align='ce +nter'><b><font color='green'>Mods</font></b></td><td align='center'>< +b><font color='green'>Quanity</font></b></td><tr>"; $color = "red"; for ($nk = 0; $nk <= $#contetnsoffile; $nk){ print "<td width='40%' align='center'><font color='$color'>@ +contetnsoffile[$nk]</font></td>"; $nk++; print "<td width='15%' align='center'><font color='$color'>@ +contetnsoffile[$nk]</font></td>"; $nk++; print "<td width='15%' align='center'><font color='$color'>@ +contetnsoffile[$nk]</font></td>"; $nk++; print "<td width='25%' align='center'><font color='$color'>@ +contetnsoffile[$nk]</font></td>"; $nk++; print "<td width='5%' align='center'><font color='$color'>@c +ontetnsoffile[$nk]</font></td><tr>"; $nk++; if ($color eq red){ $color = "blue"; } else { $color = "red"; } } print "</table></center>";
Perl Monk, Andrew Jackson

Replies are listed 'Best First'.
Re: Alphabetical order with .txt file
by grep (Monsignor) on Apr 16, 2002 at 23:28 UTC
    I would guess you need a chomp here:
    @list1 = <items>; #This chomp(@list = <items>); #to this
    but it is hard to tell since I do not see you using the sort command anywhere. So I assume this is not the code you are having the problems with.

    Couple of things Before you post you should always

  • turn on warnings use warnings;
  • use strict;
  • post the broken code.


  • grep
    Unix - where you can thrown the manual on the keyboard and get a command
      When i try to run your editted script based of mine, i get a internal server error. Do you know whats wrong with it
Re: Alphabetical order with .txt file
by lemming (Priest) on Apr 17, 2002 at 01:21 UTC

    Like grep says: use strict; use warnings;

    That won't catch your problem though. You're adding a space with @list1[$nk] = "$item1 \n";. Try dropping the space before the newline.

    Other concerns:

    • @list1[$nk] is better written as $list1[$nk] ; Though just until Perl 6
    • You currently read the file
      Then sort (though it's not shown)
      write the file
      read it
      and then display it
    • Try reading it
      sorting it
      displaying it and writing it at the same time
    • IIRC, $#list1 notation is being dropped. Just go ahead and use @list1 for # of elements. scalar @list1 is you need to.
    • Another version of your code is in my pad
      I'll leave that there for your review for now

    Update: boo has an excellent point. I just glossed by that point.

Re: Alphabetical order with .txt file (boo)
by boo_radley (Parson) on Apr 17, 2002 at 02:03 UTC
    print "Content-type: text/html\n\n"; use CGI; ... print "<td width='40%' align='center'><font color='$color'> @contetnsoffile[$nk]</font></td>"; ...
    Is there more to this script? If not, why are you bothering to use CGI;? The first two lines there are especially odd.
      the sort is in the actual script, i just forgot to add it in the post, and i just slapped use CGI; on there because it is a CGI script. I am trying your advice right now.. I will get back to you soon