FireBird34 has asked for the wisdom of the Perl Monks concerning the following question:
Any suggestions on either making this code cleaner and/or making it work properly? (keep in mind that this was a trial and error thing on my part -- reading the lines in the file -- so I am unsure if there is a more effective way)#!/usr/bin/perl use strict; #command line info my $usage = "$0 -file [file name]"; die "Usage: $usage \n"; if ((@ARGV == 0) || ((scalar @ARGV)%2 != 0)); my %CommandLine = @ARGV; my $file = $CommandLine(-file) || die "Usage: $Usage \n"; #end command line info print "Status: numbering each line in file \"$file\".\n"; &number; print "\nStatus: done numbering each line in file \"$file\".\n"; ## SUBROUTINE sub number { my $count; open(FILE, "$file") || die "Can't locate file \"$file\": $!"; my @file = <FILE>; # store contents of file into @file close FILE; open(FILE, ">>$file") || die "Can't locate file \"$file\": $!"; foreach my $line (@file) { (undef) = <FILE>; # stores number of lines into $. $count++; print FILE, "$count. "; if ($count == $.) { last; } } close FILE; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re: Append to beginning of line (multi line file)
by merlyn (Sage) on Jan 23, 2003 at 20:17 UTC | |
|
Re: Append to beginning of line (multi line file)
by Enlil (Parson) on Jan 23, 2003 at 20:32 UTC | |
by theorbtwo (Prior) on Jan 23, 2003 at 20:43 UTC | |
|
Re: Append to beginning of line (multi line file)
by krujos (Curate) on Jan 23, 2003 at 20:21 UTC | |
|
Re: Append to beginning of line (multi line file)
by castaway (Parson) on Jan 23, 2003 at 21:02 UTC | |
by FireBird34 (Pilgrim) on Jan 23, 2003 at 23:26 UTC |