Solved

Sorting a file.

Posted on 2000-05-16
8
161 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
[X]
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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Six Sigma Control Plans

630 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