Du bist hier: Snippet-Verzeichnis » Perl (198)
Sprache:

align

Sprache: English
Programmiersprache: Perl
Veröffentlicht von: tbarron [nicht registriert]
Letzte Änderung: 15.05.2006
Aufrufe: 1033


Beschreibung

align ragged columns for easy reading and sorting

Code

1 #!/usr/bin/perl 2 # ======================================================================== 3 # @(#) align - align ragged columns for easy reading and sorting 4 # Copyright (c) 1997 Tom Barron <tbarron@mindspring.com> 5 # 6 # History: 7 # 97-04-05 tb created 8 # 9 # Notes: 10 # 11 # This program is free software; you can redistribute it and/or 12 # modify it under the terms of the GNU General Public License 13 # as published by the Free Software Foundation; either version 2 14 # of the License, or (at your option) any later version. 15 # 16 # This program is distributed in the hope that it will be useful, 17 # but WITHOUT ANY WARRANTY; without even the implied warranty of 18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 # GNU General Public License for more details. 20 # 21 # You should have received a copy of the GNU General Public License 22 # along with this program; if not, you can get a copy at 23 # http://computer-guy.hypermart.net/software/gpl.html or by writing 24 # to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 25 # MA 02139, USA. 26 # ======================================================================== 27 28 require "getopts.pl"; 29 30 Getopts( "c:f:o:d:" ); 31 32 if ($opt_f ne "") 33 { 34 close( STDIN ); 35 open( STDIN, "< $opt_f" ); 36 } 37 38 if ($opt_o ne "") 39 { 40 close( STDOUT ); 41 open( STDOUT, "> $opt_o" ); 42 } 43 44 $ics = (0 == $opt_c) ? 2 : $opt_c; 45 $delimiter = ("" eq $opt_d) ? " " : $opt_d; 46 47 for $line (<STDIN>) 48 { 49 chomp( $line ); 50 @words = split( $delimiter, $line ); 51 for ($idx = 0 ; $idx <= $#words ; $idx++) 52 { 53 if ($width[$idx] < length($words[$idx])) 54 { 55 $width[$idx] = length( $words[$idx] ); 56 } 57 } 58 } 59 60 seek( STDIN, 0, 0 ); 61 62 for $line (<STDIN>) 63 { 64 chomp( $line ); 65 @words = split( $delimiter, $line ); 66 for ($idx = 0 ; $idx <= $#words ; $idx++) 67 { 68 if (substr( $words[$idx], 0, 1 ) eq "0") # a number 69 { 70 for ($kdx = length($words[$idx]) ; $kdx < $width[$idx] ; $kdx++) 71 { 72 print( " " ); 73 } 74 print( "$words[$idx]" ); 75 } 76 elsif ($words[$idx] eq "") # empty 77 { 78 print( "-" ); 79 for ($kdx = 1 ; $kdx < $width[$idx] ; $kdx++) 80 { 81 print( " " ); 82 } 83 } 84 elsif ($words[$idx] + 0 == 0) # non-numeric string 85 { 86 print( "$words[$idx]" ); 87 for ($kdx = length($words[$idx]) ; $kdx < $width[$idx] ; $kdx++) 88 { 89 print( " " ); 90 } 91 } 92 else # a number 93 { 94 for ($kdx = length($words[$idx]) ; $kdx < $width[$idx] ; $kdx++) 95 { 96 print( " " ); 97 } 98 print( "$words[$idx]" ); 99 } 100 101 for ($kdx = 0 ; $kdx < $ics ; $kdx++) 102 { 103 print( " " ); 104 } 105 } 106 print( "\n" ); 107 } 108

Noch kein Kommentar vorhanden

Dieses Snippet kommentieren

Name *  

E-Mail (wird nicht angezeigt) *    

Website  

Kommentar *  

Sicherheitscode Sicherheitscode *    

RSS