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: 980


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

Noch kein Kommentar vorhanden

Dieses Snippet kommentieren

Name *  

E-Mail (wird nicht angezeigt) *    

Website  

Kommentar *  

Sicherheitscode Sicherheitscode *    

RSS