Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-26-2007, 01:08 PM
|
#1
|
|
Senior Member
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,178
Rep:
|
Perl: Sorting by occurrence of specific character
I have data in the following format pulled from a MySQL database in an array:
$array[0] = firstname -- lastname -- city -- favorites
I would like to find a way to sort by # of favorites (they are comma-separated) if possible, so that someone with 8 favorites comes before someone with 4, regardless of length (a solution proposed elsewhere).
I've tried playing with my SQL query to sort by the # of commas in the 'favorites' field, but that hasn't lead anywhere either (that would be a very simple fix, should I get it to work).
I've looked at using tr/// but I can't quite figure out how to sort by its results. Any ideas?
|
|
|
|
09-26-2007, 01:54 PM
|
#2
|
|
Member
Registered: Sep 2004
Location: Stockholm, Sweden
Distribution: Ubuntu, RedHat, SuSe, Debian, Slax
Posts: 102
Rep:
|
Could You give an example of the MySQL table, Your SQL-statement, or the output of Your SQL-statement?
A text file with a line for each person?
Does it have to be Perl?
Last edited by lakris; 09-26-2007 at 02:03 PM.
|
|
|
|
09-26-2007, 02:07 PM
|
#3
|
|
Senior Member
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,178
Original Poster
Rep:
|
Unfortunately it does need to be perl. An example of the MySQL table is below.
Code:
+-------+----------+-----------------------------------------+
| fname | lname | favorites |
+-------+----------+-----------------------------------------+
| rob | baker | Ice cream, candy, parties |
| marky | mark | The Funky Bunch, all-star concerts |
| jeff | goldblum | String theory, Science, apes, Dinosaurs |
+-------+----------+-----------------------------------------+
I hope that's legible
I've found a way to do it using a complex series of foreach loops but I'd rather not keep that as it's pretty convoluted. Basic gist of it goes [check # of commas] -> [put # in front of string] -> [sort into new array] -> [remove count from string] -> [print]
If I could do it all in the MySQL query it'd be fantastic and I can cut most of the perl out of the project (to reduce complexity and server CPU time). What I have currently "works" but isn't very pretty.
|
|
|
|
09-26-2007, 03:17 PM
|
#4
|
|
Member
Registered: Sep 2004
Location: Stockholm, Sweden
Distribution: Ubuntu, RedHat, SuSe, Debian, Slax
Posts: 102
Rep:
|
Oh, sorry, haven't been doing any Perl for over 7 years. I'm sure I could whip something up in bash really quick (use exec?) hehe,  ,
It feels as if the correct MySQL statement could do the trick. I'll have a look at it.
Maybe a change in db-design could let you keep track of how many favorites are in each row?
Last edited by lakris; 09-26-2007 at 03:45 PM.
|
|
|
|
09-26-2007, 03:58 PM
|
#5
|
|
Senior Member
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,178
Original Poster
Rep:
|
It's been suggested that I change the DB design, but unfortunately that's not in my hands; in addition, the non-programmers who are going to be inputting data to the form want to learn as little MySQL as possible so they currently cut and paste their commands for insertion. *shrug*
Currently with MySQL I'm running into the stumbling block that I can't find the correct way to call count() to return the # of a particular character (in this case, commas), and use that value for sorting.
|
|
|
|
09-26-2007, 04:46 PM
|
#6
|
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
I'm not sure I have the correct data structure, but if I have, you can use this:
Code:
#!/usr/bin/perl
use warnings;
use strict;
my @array;
$array[0] = [ "rob", "baker", "Ice cream, candy, parties" ];
$array[1] = [ "marky", "mark", "The Funky Bunch, all-star concerts" ];
$array[2] = [ "jeff", "goldblum", "String theory, Science, apes, Dinosaurs" ];
print "Before sort:\n";
dump_data(@array);
my @array2 = sort my_cmp @array;
print "\nAfter sort:\n";
dump_data(@array2);
sub my_cmp {
my @a_favs = split(/,/, $a->[2]);
my @b_favs = split(/,/, $b->[2]);
return $#a_favs <=> $#b_favs;
}
sub dump_data {
foreach my $row (@_) {
printf "%-10s %-10s %s\n", @$row;
}
}
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:42 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|