Perl: sorting files by whats in'em


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: sorting files by whats in'em
# 1  
Old 10-12-2003
Perl: sorting files by whats in'em

i'm halfway into my script and realize i may need to use an associative array (a hash, i guess they are called in Perl). i'm fairly new to Perl and don't know how to use them correctly.

i have some files in a directory. each file contains a number in a specific place in it's header. what i would like to do is create a list of the files in a text file that has them sorted according to this number. there is also one other thing. some of the files' numbers "overlap" and i don't want these files included. since this is kind of weird to explain i'll use the following example:

below is the list of files along with their associated numbers before they are sorted. notice at "file12" the numbers start overlapping and starting with "file21" the numbers reach the point past -0.5 (which is file11's number). i basically do not want files 12 through 20 included in my text file.

"file1",-30.5
"file2",-27.5
"file3",-24.5
"file4",-21.5
"file5",-18.5
"file6",-15.5
"file7",-12.5
"file8",-9.5
"file9",-6.5
"file10",-3.5
"file11",-0.5
"file12",-26.5
"file13",-23.5
"file14",-20.5
"file15",-17.5
"file16",-14.5
"file17",-11.5
"file18",-8.5
"file19",-5.5
"file20",-2.5
"file21",0.5
"file22",3.5
"file23",6.5
"file24",9.5
"file25",12.5
"file26",15.5
"file27",18.5
"file28",21.5
"file29",24.5
"file30",27.5
"file31",30.5

here is the way the text file with the sorting should look:

"file1",-30.5
"file2",-27.5
"file3",-24.5
"file4",-21.5
"file5",-18.5
"file6",-15.5
"file7",-12.5
"file8",-9.5
"file9",-6.5
"file10",-3.5
"file11",-0.5
"file21",0.5
"file22",3.5
"file23",6.5
"file24",9.5
"file25",12.5
"file26",15.5
"file27",18.5
"file28",21.5
"file29",24.5
"file30",27.5
"file31",30.5

Note: the quotes aren't part of the file names and the file names for the files are not named so that you can "deduce" the order (file1 -> file31), they are for all intents and purposes random. Also, i know the numbers increase by 3 (and that is how they increase), but the gap in between the cut off is only 1 (from -0.5 to 0.5), and this is ok. i basically cannot have a gap more than 3.

what i'm thinking is that an associative array may be appropriate, but i'm not sure. my logic so far has been to first, find the smallest number and write it's corresponding filename to the text file. then find the filename with the first file's number + 3, and append that filename to the textfile. then, if no more files fit that criteria, start appending the filenames by which is larger than the last one. i think this would work but i don't know how to do it.

thanks in advance for helping. this will save me lots of time, since i have many directories like this and sorting them by hand takes a long time.
# 2  
Old 10-13-2003
Your task is composed of two subtasks:
1) Extracting all ($filename, $magic_number) pairs from all files to create a temporary list.
2) Sorting and scanning the list generated and picking the right entries.

For (1) it is assumed you know how to do it. A hash is not the right data structure to sort keys according to values. Arrays is my favourite. You can set up an array containing the pairs like this:

# ... set up a loop to run through all files
push @array, [$filename, $magic_number];
# loop ends here

As for (2) you can then sort the pairs ascendingly by

@sorted = sort { $$a[1] <=> $$b[1] } @array;

my $counter = $sorted[0]->[1];
# Before the gap
foreach (@sorted) {
if ($counter == $_->[1]) {
push @finallist, $_;
$counter += 3;
}
}

# After the gap
foreach (@sorted) {
if ($_->[1] > $counter-3) {
push @finallist, $_;
}
}

#print result
foreach (@finallist) {
# Print filename and magic number
print $_->[0], ", ", $_->[1], "\n";
}

Warning: untested code.
# 3  
Old 10-13-2003
Re: Perl: sorting files by whats in'em

Quote:
Originally posted by quantumechanix

i have some files in a directory. each file contains a number in a specific place in it's header. what i would like to do is create a list of the files in a text file that has them sorted according to this number. there is also one other thing. some of the files' numbers "overlap" and i don't want these files included. since this is kind of weird to explain i'll use the following example:
Your explanation of "overlap" is a bit confusing. By throwing out your "overlapping" numbers, you are already assuming that your numbers are mostly sorted descending. Explain what you are trying to do here. It seems like there is more behind your question than just sorting. You are eliminating numbers based on a special criteria.
# 4  
Old 12-12-2003
my solution

#! /usr/local/bin/perl -w

@array = (qw /-20 -17 -14 -11 -8 -5 -2 -6 -3 0 3 6 9 12 15 18/);
# i want 18 15 12 9 6 3 0 -2 -5 -8 -11 -14 -17 -20
# because i can't have intervals greater than 3
# AND i can have only one interval less than 3

# We'll need the list of numbers as a string separated by spaces
foreach (@array) {
$num_string .= $_ . " ";
}

print "$num_string\n";

$pre_num = 10000; # Shirley, they'll never be a number more than 10000...right?
$count = 0;
$less_than_counter = 1;
foreach $num (sort {$b<=>$a} @array) {
$count ++;
if (($num == ($pre_num-3)) || ($count == 1)) { # It's a keeper!
print "$num\n";
$pre_num = $num;
}
elsif ($num < ($pre_num-3)) { # Detects if increment is more than 3
die "A number is missing!!!\n";
}
elsif (($num > ($pre_num-3)) && ($less_than_counter == 1)) { # Detects if increment is less than 3, allows it once
print "$num\n";
$less_than_counter ++;
$pre_num = $num;
}
else {
# do nothing
}
}
# 5  
Old 12-14-2003
is it really so difficult

sort -n -t, -k2 file1

is this not what you are looking for.
correct me if i have misunderstood
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Sorting Help

Hey guys, I have started to learn perl recently because of a position I took. They want me to master perl and I've been reading books and practicing myself. Basically I,m having my perl script run through a text pad and give the output in a special way e.g input deviceconfig { ... (5 Replies)
Discussion started by: zee3b
5 Replies

2. Shell Programming and Scripting

Sorting dates in Perl

I have a directory of backup files. named like this: ldap.data.04-06-2012.tar ldap.data.03-06-2012.tar ldap.data.02-06-2012.tar ldap.data.01-06-2012.tar ldap.data.31-05-2012.tar ldap.data.30-05-2012.tar ldap.data.29-05-2012.tar ldap.data.28-05-2012.tar ldap.data.27-05-2012.tar... (6 Replies)
Discussion started by: robsonde
6 Replies

3. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

4. Shell Programming and Scripting

Sorting a particular column in PERL

I have a file abc.txt which contains data in th following format abc,23 hgfh,87 tweg,89 jdfjn,74 I want to sort on the basis of column (the second one). It should be numerical sort. output should be abc,23 jdfjn,74 hgfh,87 tweg,89 I know how to do it in unix. I need a PERL code (1 Reply)
Discussion started by: centurion_13
1 Replies

5. Shell Programming and Scripting

perl sorting variables

Good morning!! Im trying to practice withe Perl and sorting variables is not going good at all! #!/usr/bin/perl $username = $ENV {'LOGNAME'}; print "Hello, $username\n"; I want to add sort and 'mail' and 'home'. This below is what I have came up with,but of course its not working. ... (5 Replies)
Discussion started by: bigben1220
5 Replies

6. Shell Programming and Scripting

Perl sorting

Hi, I have a file in this format: a b c d e a b c d e a b c d e i need perl script to sort 2nd column in alphabatical order The script i use is #!/usr/bin/perl my @words=<>; foreach(sort mysort @words) { print; (4 Replies)
Discussion started by: Learnerabc
4 Replies

7. Shell Programming and Scripting

whats wrong with this line using perl

E:\>perl -00ne 'push @a,"$_\0$ARGV\n";END{print reverse split/\0/ for sort @a}' file1-obj_prof.out.txt file2-obj_prof.out.txt' Can't find string terminator "'" anywhere before EOF at -e line 1. (6 Replies)
Discussion started by: richsark
6 Replies

8. Shell Programming and Scripting

PERL data - sorting

Hello, I have a page where multiple fields and their values are displayed. But I am able to sort only a few fields. When I looked into the issue, it is seen that the for each row of info , an unique id is generated and id.txt is generated and saved. Only those fields which are inside that id.txt... (3 Replies)
Discussion started by: eagercyber
3 Replies

9. Shell Programming and Scripting

perl sorting

I have many files that I need to sort each week. I know how to do in Unix, but for this task it appears best to do native inside an existing perl program. So, simplified, I have a file similar to the following: Joe_________12_Main_St__A001________LX Benny_______5_Spring____A002________LX... (5 Replies)
Discussion started by: joeyg
5 Replies

10. Shell Programming and Scripting

renaming 50k files, whats the best way?

Because I am not creative, I did this: find . -type f -name '*.GIF'|cut -d'/' -f2|awk -F. '{print "mv "$1".GIF "$1".gif --reply=yes"}' > case.sh Then ran the case.sh - I was wondering if you guys could come up with something more efficient? Or even limit CPU useage? It is killing my poor ext3... (3 Replies)
Discussion started by: r0sc0
3 Replies
Login or Register to Ask a Question