Solved

Sorting a file.

Posted on 2000-05-16
8
154 Views
Last Modified: 2010-03-05
I have a textvile with my mp3-discs and albums on them.
The file looks like this:

Artist and album  %% disc_number
Artist and album   %& disc_number

For example:

A-Teens - The ABBA generation (1999) %% 1
Amanda Marshall - Tuesday's child - 1999 %% 2
Benny Jansson - Virtual Humanity - 1995 %% 2
Jennifer_lopez-on_the_6-1999-sinned-apc %% 1
Megadeth - Risk - 1999 %% 3
Red Hot Chili Peppers - Californication %% 3


What I like to do is to go throug the file and make a new file sorted after discs

So how can I do that.

I mean I like a output like this or something:

Disk 1:

A-Teens - The ABBA generation (1999)
Jennifer_lopez-on_the_6-1999-sinned-apc

Disk 2:

Amanda Marshall - Tuesday's child - 1999
Benny Jansson - Virtual Humanity - 1995

Disc 3:

Megadeth - Risk - 1999
Red Hot Chili Peppers - Californication


How can I fix that?
0
Comment
Question by:pucko
8 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2814918
Assuming that the text file where the info is stored is called mp3.txt, here is a solution that will print out labels in the format you want

i have put comments in the PERL code so that you can understand it better.

======================mp3.txt
A-Teens - The ABBA generation (1999) %% 1
Amanda Marshall - Tuesday's child - 1999 %% 2
Benny Jansson - Virtual Humanity - 1995 %% 2
Jennifer_lopez-on_the_6-1999-sinned-apc %% 1
Megadeth - Risk - 1999 %% 3
Red Hot Chili Peppers - Californication %% 3

========================label.pl
!/usr/bin/perl

$mp3="/tmp/mp3.txt";
open(MP3,$mp3) || die $!;

## Remove the next line if you are not running the script from the browser
print "Content-type: text/html\n\n";

while(<MP3>){
  chomp; ## remove the \n character from the line
  ($label,$disk)=split(/%%/,$_); ## break the line using %% as delimiter
  $LABEL{$disk}.=$label."\n"; ## create/append to an associative array/hash where the number is the key to the hash
}
close (MP3);

## sort & display the results
foreach(sort keys %LABEL){
  print "Disk ",$_,":\n\n",$LABEL{$_},"\n";
}
0
 
LVL 1

Author Comment

by:pucko
ID: 2815136
Works Almost as I like to.

One problem with the sorting.

I get them in this order:

Disc 1
Disc 10
Disc 100
Disc 101


And not
Disc 1
Disc 2
Disc 3
......
0
 
LVL 84

Expert Comment

by:ozo
ID: 2815228
sort {$a <=> $b} keys %LABEL
#but if the keys are all numbers, do you want to use an array instead of a hash?
0
 
LVL 12

Expert Comment

by:geotiger
ID: 2815517
I agree with Ozo: array will work better in this case.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:pucko
ID: 2816591
It works just as I like to now.. But it would be nice to se an example with an array to.
0
 
LVL 16

Accepted Solution

by:
maneshr earned 200 total points
ID: 2817241
here is your solution, based on arrays.

Basically its the same script as before, but with a small change.

now, instead of directly printing the Hash, i am first getting all the keys of the hash in an array, sorting that array numerically and then using that array to print the values from the hash.


=================mp3.pl
#!/usr/bin/perl

$mp3="/tmp/mp3.txt";
open(MP3,$mp3) || die $!;

print "Content-type: text/html\n\n";

while(<MP3>){
  chomp;
  ($label,$disk)=split(/%%/,$_);
  $LABEL{$disk}.=$label."\n";
}
close (MP3);

@sorted_keys=keys %LABEL;

##  Perform a numeric sort
@sorted_keys=sort bynum @sorted_keys;

foreach(@sorted_keys){
  print "Disk ",$_,":\n\n",$LABEL{$_},"\n";
}

sub bynum{ $a <=> $b;}
0
 
LVL 1

Author Comment

by:pucko
ID: 2819827
Thnks man!
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2821142
most welcome :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now