Sorting a file.

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?
LVL 1
puckoAsked:
Who is Participating?
 
maneshrCommented:
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
 
maneshrCommented:
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
 
puckoAuthor Commented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
ozoCommented:
sort {$a <=> $b} keys %LABEL
#but if the keys are all numbers, do you want to use an array instead of a hash?
0
 
geotigerCommented:
I agree with Ozo: array will work better in this case.
0
 
puckoAuthor Commented:
It works just as I like to now.. But it would be nice to se an example with an array to.
0
 
puckoAuthor Commented:
Thnks man!
0
 
maneshrCommented:
most welcome :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.