in reply to Re: problem with foreach statement
in thread problem with foreach statement
sugestions of how i can do better are welcome
OK, here goes...
Having said that, here is a "tidy" version of your code, which uses a single HoH and a single set of related arrays and file handles (instead of five distinct sets of everything), and applies most of the other suggestions listed above -- it's about 120 lines, down from the 252 of the original "tidy" version. I haven't tested it, of course, but it does pass "perl -cw", and I think it'll do the same thing as your original code. (I could be wrong about that, but this should at least give you an idea about how you can eliminate unnecessary duplication in your code.)
#!/usr/bin/perl -w use strict; use File::Spec; my $spec_text = qr/Smt22/; my $path_tmp = $0; my ( $volume, $directories, $file ) = File::Spec->splitpath($path_tmp) +; my $path = $volume . $directories; opendir( DIRREN, $path ) || die; my @RENDIR = readdir(DIRREN); closedir( DIRREN ); my $match_file = "MSISDN.txt"; my @matches = (); my @errorcodes = qw/22 24 50 52 79/; # open file and load into array chdir $path; open( my $match_in, '<', $match_file ) or die("$match_file: open faile +d: $!"); my @matches_in = <$match_in>; close($match_in); for ( @matches_in ) { $_ = (split /,/)[1] . '.*Returned error code (?:' . join('|', @err +orcodes) . ')'; } foreach my $file (@RENDIR) { if ( $file =~ $spec_text ) { open( my $fh_in, '<', $file ) or die("$file: open failed: $!") +; while ( my $line = <$fh_in> ) { foreach my $match_in (@matches_in) { if ( $line =~ /$match_in/ ) { push @matches, $line; last; } } } close($fh_in); } } my @newmatch = (); my @tmplist = (); #split logfile line into needs and put together everything ;D foreach my $match (@matches) { chomp $match; @newmatch = split( ';', $match ); my @tmpnewmatch = split( ',', $newmatch[6] ); #cuts the blanks @ beginning and end of each string element $newmatch[5] =~ s/^\s+//; $tmpnewmatch[0] =~ s/^\s+//; push @tmplist, $newmatch[5] . ";" . $tmpnewmatch[0]; } # removes duplicates and print out MD Del list my @list = do { my %seen; grep !$seen{$_}++, @tmplist }; open( INFILE, '<', $match_file ) or die("$match_file: open failed: $!" +); open( MDDELOUT, '>', "MD_delete_list.txt" ) or die("MD_delete_list.txt +: open failed: $!"); while (<INFILE>) { my @mdmsisdn = split( ',', $_ ); $mdmsisdn[1] =~ s/^\s+//; foreach my $mddel (@list) { my @newmddel = split( ';', $mddel ); if ( $newmddel[0] == $mdmsisdn[1] ) { print MDDELOUT "$mdmsisdn[0] ; $mddel \n"; } } } close(INFILE); close(MDDELOUT); #put the list into an array to easyly search for msisdn, will be put i +nto a hash of hashes # top-level hash keyed by errorcode, lower-level hash keyed by msisdn +value my %adv = (); my $errcode_regex = 'ADV: Returned error code (' . join('|',@errorcode +s) . ')'; foreach my $list (@list) { my ( $msisdn, $adverror ) = split( ';', $list ); if ( $adverror =~ /$errcode_regex/ ) { my $errcode = $1; $adv{$errcode}{$msisdn} = $adverror; } else { print "no ADV Errors ;D" } } #open errorfiles and check if msisdn is already known then sorting to +existing or not for my $errcode ( @errorcodes ) { my $advname = "adv$errcode.txt"; open( my $exmsisdn, '<', $advname ) or die("$advname: open failed: + $!"); my @exerrors = <$exmsisdn>; close($exmsisdn); my @tmpnew = (); my @tmpold = (); foreach my $advkey ( keys %{$adv{$errcode}} ) { if ( grep { $advkey == $_ } @exerrors ) { print "$advkey is an old error\n"; push @tmpold, $advkey . "\n"; } else { print "$advkey is a new error\n"; push @tmpnew, $advkey . "\n"; } } #put new errors in arry to existing errors dupe check it and write bac +k to file foreach my $tmpexout (@tmpnew) { push @exerrors, $tmpexout; } my @newexout = do { my %seen; grep !$seen{$_}++, @exerrors }; open( OUT, '>', $advname ) or die("$advname: open failed: $!"); foreach my $newout (@newexout) { print OUT $newout; } #printing out final records to mail ;D my $mailname = join '', 'adv', $errcode, '_mail.txt'; open( OUTMAIL, '>', $mailname ) or die("$mailname: open failed: $! +"); print OUTMAIL "Here goes the text needed befor MSISDN \n NEW MSISD +Ns\n\n"; foreach my $mail (@tmpnew) { print OUTMAIL "$mail"; } print OUTMAIL "\n EXISTING MSISDNs\n"; foreach my $mailold (@tmpold) { print OUTMAIL "$mailold"; } close(OUTMAIL); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: problem with foreach statement
by ultibuzz (Monk) on Jan 26, 2006 at 12:32 UTC | |
by graff (Chancellor) on Feb 01, 2006 at 06:35 UTC | |
by ultibuzz (Monk) on Feb 16, 2006 at 07:33 UTC | |
Re^3: problem with foreach statement
by ultibuzz (Monk) on Jan 03, 2006 at 19:01 UTC |