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!

Alphabatize

Status
Not open for further replies.

hunter13605

Technical User
Apr 7, 2006
44
US
Is there a command that would automatically alphabatize an array of data? I have written one in c++ but it is klunky and very unefficient.
 
I'm kind of new at this, could you please explain how i would place this and what, and how, it does.
 
takes @not_sorted array, and sorts it alphabetically based on the lowercased version of the array item, and assigns that to the @sorted array

Check out perldoc.perl.org for the sort function

HTH
--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
not to be a pain, but could you please tell me where in the code it should be placed. I want to open the list of data after a new name is added and have it sorted into its alphabetical location.
 
We all assume you know how to make your array because you asked:

Is there a command that would automatically alphabatize an array of data?

So after you create your array you insert the above sort routine, say you have a file you read into an array:

Code:
open(FH,'file.txt');
my @unsorted_array = <FH>;
close(FH);
my @sorted_array = sort {lc($a) cmp lc ($b)} @unsorted_array;
print "$_" for @sorted_array;

If your data is not mixed case text you can just use sort without any other block of code:

my @sorted_array = sort @unsorted_array;
 
One more question. If i run this, will it keep all of my fields together? I have 6 different variables, First name, last name, login, and password. I want to sort it by the last name (all caps) but i need to keep each piece of data together.
 
the sort as shown above will sort the entire line of data starting from the first character on the left. So "Andrew,Smith" will sort before "Jane,Doe". If you need to sort on a specific field you need to do more work with the data before sorting it: splitting it up into seperate fields. Post a few sample lines of the data you want sorted.
 
I think his question was about having multiple arrays, like @lastnames, @firstnames, @logins, @passwords, and having a sort on one array rearrange the items in the other arrays as well.

The simple answer: this isn't easy to do.

Your best bet would be to merge all the data as a single scalar and put it into the array, i.e.

push (@users, "lastname,firstname,login,password");

And then sorting @users would sort by last name, then you could get the other vars back for a particular lastname:

my ($lastname,$firstname,$login,$password) = split(/\,/, $users[2], 4);
 
I have it all in that format in a .txt file for storage. Is there any way to sort the text file? Would it be easier?

Enter admin in the first name spot (lower case) and test as a student id (also lower case). after you log in click edit students. I have the first column set up so that it prints the last name field and then the first name field. they are not combined in the same field.corbett

At the present time it is all in alphabetical order except for me at the bottom... In the future, when i turn it over to the year book staff they want to have new users sorted into the proper alphabetical position.

Feel free to test it out. I have a back up of the data files so you're not going to lose any information. Thanks to all of you for all of your help
 
Another possibilty would be to merge your data into a scalar with a delimeter, and use that as the value of a keyed hash, where the key is the surname, or forename, or you could go into multi dimensional arrays/hashes

Just a thought

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
so do you want me to post the data that is in the text file?
 
not all of it, just a few sample lines, 10 or 20 lines should be more than enough. Even one line will do as long as it's representative of all the lines in the text file.
 
A few lines would have been sufficient....

anyways, you said: "I have 6 different variables" but I only see 5. But I think you want it sorted by the last name, in that case the old Shwartzian Transform (an impressive name) will do:

Code:
open (FH,'users.txt') or die "$!";
my @sorted = map  {$_->[0]}
             sort {$a->[3] cmp $b->[3]}
             map  {[$_,split(/,/)]} <FH>;
close(FH);
print for @sorted;

but the file already appears to be sorted by last name?
 
I don't ever remember saying that there were six variables but it dosen't make a difference. I wasn't sure how many you wanted so i just posted a bunch of them. They are all in order by last name right now but the administrator of the service has the option to add and remove users. When a new user is added they get added to the end of the list and i would like them put in where they should go. Maybe theres a simpiler way to sort the one name into the right place instead of alphabetizing the whole list afterwards.
 
That sounds like what i am loooking to do and i appreciate your help. Becuse i am very new to perl and this is really my first big program, could you please explain whet this code is doing. This way, i will be able to adapt it to what i am doing as easy as possible.
 
Code:
my $in_rec = 'kevin, KEVIN,test,xxxxxxxx,endline';
This is the record we are trying to insert. We want the surname so we split it on the commas
Code:
split( ',', $inrec )
returns an array and we want the third element, so we can say
Code:
my $target = ( split( ',', $inrec ) )[2];
This gives us the surname in $target.

We then open our input file and a temporary file to store the sorted output. We read the input file a record at a time and then extract the surname from the current record into $current
Code:
my $current= ( split( ',' ) )[2];

Because we know the input file is already sorted, all we have to do is copy it record by record into the output file until we have read (but not written) the record that would come after the one we are trying to insert.

Noe the "else" branch is entered and we write our insertion record to the output. We then use a simple loop
Code:
print TMP $_ while(<IN>);
to spool the rest of the input into the output.

The rest is just housekeeping: we close our files, renae the master file and rename our temporary file in it's place.

I hope that this is the right sort of level of detail for you.

Yours,

fish



[&quot;]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.[&quot;]
--Maur
 
Thanks, thats exactly what i was looking for. Another quick question while i have it on my mind. Is there a function that will automatically print a list of information to the default printer when you push a button?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top