Sponsored Content
Top Forums Shell Programming and Scripting Perl: sorting files by whats in'em Post 44945 by quantumechanix on Friday 12th of December 2003 11:34:49 AM
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
}
}
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
locale(3pm)						 Perl Programmers Reference Guide					       locale(3pm)

NAME
locale - Perl pragma to use or avoid POSIX locales for built-in operations SYNOPSIS
@x = sort @y; # Unicode sorting order { use locale; @x = sort @y; # Locale-defined sorting order } @x = sort @y; # Unicode sorting order again DESCRIPTION
This pragma tells the compiler to enable (or disable) the use of POSIX locales for built-in operations (for example, LC_CTYPE for regular expressions, LC_COLLATE for string comparison, and LC_NUMERIC for number formatting). Each "use locale" or "no locale" affects statements to the end of the enclosing BLOCK. Starting in Perl 5.16, a hybrid mode for this pragma is available, use locale ':not_characters'; which enables only the portions of locales that don't affect the character set (that is, all except LC_COLLATE and LC_CTYPE). This is useful when mixing Unicode and locales, including UTF-8 locales. use locale ':not_characters'; use open ":locale"; # Convert I/O to/from Unicode use POSIX qw(locale_h); # Import the LC_ALL constant setlocale(LC_ALL, ""); # Required for the next statement # to take effect printf "%.2f ", 12345.67' # Locale-defined formatting @x = sort @y; # Unicode-defined sorting order. # (Note that you will get better # results using Unicode::Collate.) See perllocale for more detailed information on how Perl supports locales. NOTE
If your system does not support locales, then loading this module will cause the program to die with a message: "Your vendor does not support locales, you cannot use the locale module." perl v5.18.2 2013-11-04 locale(3pm)
All times are GMT -4. The time now is 11:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy