Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Sorting a file.

Posted on 2000-05-16
8
158 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 12

Expert Comment

by:geotiger
ID: 2815517
I agree with Ozo: array will work better in this case.
0
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

809 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