http://qs321.pair.com?node_id=130537


in reply to Re: Re: Re: Problem with subroutine
in thread Problem with subroutine

A rewrite (yes, I've changed the coding style slightly...): (Be forewarned that this is very much untested.)

#!/usr/bin/perl -w use strict; my $home = $ENV{HOME} || (getpwuid($<))[7]; my $mail_to = 'me@employer'; # XXX when is this used? my $outfile = "$home/store_errors"; my $delivery_dir = "$home/delivery"; my $no_curropt_dir = "$home/no_curropt_stores"; my $error_pat = "$delivery_dir/error*"; my $no_error_pat = "$delivery_dir/no_error*"; my $corrupt_pat = "$no_curropt_dir/corrupt*"; my $no_corrupt_pat = "$no_curropt_dir/no_corrupt*"; my $store_list = "/store_list.full_listing"; my $date = scalar localtime; exit unless `netstat -i` =~ /^P01$/mi; sub store_name_map { map { (split /\./, $_, 2)[1] } @_ } sub uniques { my %seen; $seen{$_}++ for @_; return sort grep { $seen{$_} == 1 } keys %seen; } open STORES, "<$store_list" or die "opening $store_list for reading: $ +!\n"; my @stores = <STORES>; close STORES; my @errors = store_name_map(glob($error_pat)); my @no_errors = store_name_map(glob($no_error_pat)); my @error_union= uniques(@stores,@errors,@no_errors) my @corrupt = store_name_map(glob($curropt_pat)); my @no_curropt= store_name_map(glob($no_curropt_pat)); my @curropt_union= unqiues(@stores,@corrupt,@no_curropt); my $store_count = scalar(@stores); my $error_count = @errors + @no_errors; # - $store_count; my $curropt_count = @curropt + @no_curropt; # - $store_count; # XXX what were the - $NUM_STORES for? my $no_error_count = $store_count - $error_count; my $no_curropt_count = $store_count - $curropt_count; open OUTFILE, ">$outfile" or die "opening $outfile (for writing): $!\n +"; local $" = "\n\t\t"; # proper array output format print OUTFILE <<"END"; <snip> listing for $date Error Section There were a total of $store_count stores. \tThere were $error_count stores that reported in. \tThere were $no_error_count that did not report in. \t\t@error_union \tThere were $error_count that reported errors. \t\t@errors Curroption Section There were a total of $store_count stores. \tThere were $curropt_count stores that reported in. \tThere were $no_curropt_count stores that did not report in. \t\t@corrupt_union \tThere were $curropt_count that reported errors. \t\t@corrupt END

I guessed in a few places what you wanted to do.

This is probably a lot more wasteful then yours would be if it worked, due to not using temperorary files; that could probably be remidied with a bit of work. I hope that this helps you learn how to code better...