c has asked for the wisdom of the Perl Monks concerning the following question:
my version of the same process is as follows:#!/usr/bin/perl -w system("ls -1 10.* > hostfilelist"); open (LIST, "hostfilelist"); push(@entrys, <LIST>); foreach my $entry (@entrys) { open (CURRENT, "$entry"); my @contents = <CURRENT>; @results = grep {/hostname/} @contents; #print $results[0]; @outputname = split (/ / , $results[0]); #print $outputname[1]; system("touch $outputname[1]"); open (OUTPUTFILE, ">$outputname[1]"); print OUTPUTFILE "@contents"; close (LIST); close (CURRENT); close (OUTPUTFILE); unlink ("hostfilelist"); }
he argues that my script is longer and takes more time and memory. i see his as making several system calls and multiple arrays. granted both work and neither would rock the processor, but i am really looking for some kind of justification that i am on the right path towards improving what skills i have and thinking about more than just "did it work?".#!/usr/bin/perl -w use strict; my @entrys; my $entry; my $host; #to specify the directory, just uncomment the next line and put in the #real path to the dir, then comment out, or erase the opendir line tha +t #references "." #opendir DIR, "/path/to/dir/with/configs" or die "Cant open dir $!\n"; opendir DIR, "." or die "Cant open dir $!\n"; @entrys = readdir DIR; #uncomment the next line if you are specifying a differenet directory #other than the pwd. #chdir "/path/to/dir/with/configs" or die "Cant change directory $!\n" +; foreach my $entry (@entrys) { next if ( $entry !~ /^10/ ); open CURRENT, $entry or die "Cant open file $entry $!\n"; while (<CURRENT>) { $host = $_; if ( $host =~ /^hostname/ ) { $host =~ s/^hostname\s(.*)/$1/; chomp $host; open OUTPUTFILE, ">$host" or die "Cant create file $!\n"; print OUTPUTFILE <CURRENT>; close OUTPUTFILE; } } close CURRENT; }
humbly, -c
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: A question of efficiency
by MZSanford (Curate) on Jul 18, 2001 at 18:20 UTC | |
by nysus (Parson) on Jul 18, 2001 at 18:32 UTC | |
Re: A question of efficiency
by premchai21 (Curate) on Jul 18, 2001 at 18:25 UTC | |
by Aighearach (Initiate) on Jul 18, 2001 at 18:53 UTC | |
by Ovid (Cardinal) on Jul 18, 2001 at 19:38 UTC | |
by Aighearach (Initiate) on Jul 18, 2001 at 20:08 UTC | |
by Ovid (Cardinal) on Jul 18, 2001 at 20:41 UTC | |
| |
Re: A question of efficiency
by bikeNomad (Priest) on Jul 18, 2001 at 18:21 UTC | |
Re: A question of efficiency
by Hofmator (Curate) on Jul 18, 2001 at 21:12 UTC |