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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting a File

Status
Not open for further replies.

SlykFX

Programmer
Oct 12, 2002
76
GB
I have a flat file db that contains a listing of movies that consist of :: type, name, description and a rating

the file looks similar to this (ive trimmed it down and cut parts of the description for time and effort sake) ::
(the && are the field seperators and each film is on its own line)

Action&&Die Hard&&Bruce Willis .... &&3
Comedy&&Space Balls&&Star Wars Spoof with .... &&2
Science Fiction&&Star Trek: Insurretion&&The Federation ....&&4
etc.


What I want to do is for my script to sort these according to the what the visitor clicks to sort by.
For example if they click on "sort by name" then it would look like:
Action&&Die Hard&&Bruce Willis .... &&3
Comedy&&Space Balls&&Star Wars Spoof with .... &&2
Science Fiction&&Star Trek: Insurretion&&The Federation ....&&4
etc.

But if they click sort by rating it would look like:
Comedy&&Space Balls&&Star Wars Spoof with .... &&2
Action&&Die Hard&&Bruce Willis .... &&3
Science Fiction&&Star Trek: Insurretion&&The Federation ....&&4
etc.

and so on

Is this possible?
If not, is it possible to just list them according to name?

ive searched on here and the perl forum but i cant find anything regarding what I want to do. I can't be bothered to have a sig!
 
I'd do it with DBI/DBD::CSV

You can tell the DBD to use a different field separator (&&) and then do your queries as SQL

selet * from table_name order by $sort_field

I don't have time now. If you're still chasing this monday, I'll post a short example. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
is it possible to do this purly by perl without using SQL? I can't be bothered to have a sig!
 
Here's a possiblity. You might have to tweak %sortmode to match your input and data and also figure out how to set the $mode variable based on user input.
Code:
my $mode = 'CATEGORY';
my %sortmode = (
        CATEGORY    => 0,
        TITLE       => 1,
        # ... other sort possibilities (?)
        RATING      => 3,
    );

@movies =
        map { $_->[0] }
        sort { $a->[1] cmp $b->[1] }
        map { [ $_, (split/&&/)[$sortmode{$mode}] ] }
        @movies;
This only works for ratings if the rating numbers are less than 10 (because they are sorted asciibetically rather than numerically).

jaa
 
thanks mate

ill go fiddle with that :) I can't be bothered to have a sig!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top