Solved

Sorting a file.

Posted on 2000-05-16
8
160 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
Independent Software Vendors: 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remove Malware code from PHP file 6 107
hard perl script 16 168
remove duplicates from the csv file 13 124
Perl: How to add backslashes to every period in a string 1 88
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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

751 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