ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I have data in the following format pulled from a MySQL database in an array:
$array = 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?
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.
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?
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.