Sorting Question...

They have: 7 posts

Joined: Mar 2000

Ok, I am using a form to write to a data file... the script writes in the following format:

name|url|description|etc|etc|etc

Then, the script will print out the file in nicely organized tables... the thing is, I would like to sort the data alphabetically by name before I print it (or before I write to the file?) How would I go about doing this?

------------------
Steve Caponetto http://whz.net

[This message has been edited by whz (edited 20 March 2000).]

Steve Caponetto
http://whz.net

They have: 850 posts

Joined: Jul 1999

Are you just appending the new entry to the file?

You could do somthing like this

code:

open(IN,"file.txt") or die "Cannot open file.txt $!";

@data = <IN>;
close(IN);

push( @data, $NEW_ENTRY);
@data = sort @data;

open(IN,">file.txt") or die "Cannot open file.txt : $!";

print IN @data;

close(IN);

[/code]



------------------
Too many people on the internet lie. 

They have: 7 posts

Joined: Mar 2000

Ok, I realize I didn't explain what I wanted very well in my first post..

Ok, The text file has a bunch of data in the form of

name|url|comments|etc|etc...

Then, I am going to be printing out each line of data in nicely formatted tables. What I want to do is when I print out each line of data, I would like them to be printed on the screen in alphabetical order based upon the first data entry - the name. How would I go about sorting these data entries in alphabetical order based upon the first "name" field?

------------------
Steve Caponetto
http://whz.net

Steve Caponetto
http://whz.net

He has: 150 posts

Joined: Apr 1999

The following code should do the job fine.

open (file,"file.dat");
@data = <file>;
close(FILE);
@data = sort @data;

They have: 7 posts

Joined: Mar 2000

Thanks Stefan. That worked

------------------
Steve Caponetto
http://whz.net

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.