[H-GEN] file conversions

Jason Henry Parker jasonp at uq.net.au
Thu Feb 15 07:17:31 EST 2001


[ Humbug *General* list - semi-serious discussions about Humbug and  ]
[ Unix-related topics.  Please observe the list's charter.           ]
[ Worthwhile understanding: http://www.humbug.org.au/netiquette.html ]

staeci at yahoo.com writes:

This is the sort of code that *should* give perl a bad name!

> #!/usr/bin/perl

#!/usr/bin/perl -w

use strict;

# Always use strict; and -w.  Even for little toys and examples like
# this.

> open(data_file, $ARGV[0]);
> @data=<data_file>;
> close data_file;

my $fn = shift
        or die "no argument supplied!\n";

open DAT, $fn
        or die "can't open $fn: $!\n";

close DAT
        or die "can't close $fn: $!\n";

# You also have a useless and potentially bug-causing parenthisation
# habit.  Break it.

> 
> $end=@data;
# What the heck is this supposed to do?
# You're evaluating @data in a scalar context to get a count of the
# number of elements (good), but you only ever use $x in a scalar
# context!

> 
> for($x=0;$x<$end;$x++)
# This is also silly.  Perl is not C.  If your perl looks like C, you
# have a problem.  In this case, bothering to write a for() loop,
# checking for off-by-one errors and initialising variables is just
# too much work for a simple task.

for(my $x = 0; $x < @data; $x++) { # if you must do it with a for()
map { s/:/\n/g; print } @data      # if you want to do it idiomatically

Grrrr.  If you must do it that way, use something like this (which
will act as a unix filter, as opposed to editing files in-place):

#!/usr/bin/perl -w
use strict;

while(<>) {       # reads from files name on command-line,
                  # or stdin, assigns to $_
        s/:/\n/g; # also operates on $_
        print;    # guess what!
}

This will edit files in-place:

$ perl -i .bak -pe'y/:/\n/' *.input

There's no need to build a finite state machine to do the substitution
when you can simply use tr (aka the y/// operator).  This isn't just a
competition to squeeze the script into the smallest available
space---y/// will almost certainly be faster than s///g, and learning
the perl run-time arguments is always a good idea.  (The astute will
have noticed that one-liner does not use strict or -w.  You must know
the rules before you can break them.)

See the perlrun and perlop manual pages for more information.

(Of course, this may or may not solve the original problem.)

jason
-- 
``Just because one proposes a measure to prevent promotion
        of a risk-filled and controversial sexual behavior
                     doesn't make them divisive or bigoted.''
                                     -- Nicholas J. Yonker,
                    Concerned Citizens for Sound Education

--
* This is list (humbug) general handled by majordomo at lists.humbug.org.au .
* Postings to this list are only accepted from subscribed addresses of
* lists 'general' or 'general-post'.



More information about the General mailing list