Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^5: RFC: beginner level script improvement

by jwkrahn (Monsignor)
on Sep 26, 2013 at 03:14 UTC ( #1055776=note: print w/ replies, xml ) Need Help??


in reply to Re^4: RFC: beginner level script improvement
in thread RFC: beginner level script improvement

my ($c,$mailcc,$mailto) =(0,'',''); foreach ( @{ $mailref } ){ #put first element in array as mail_to +and all others as CC if ($c == 0){ $mailto .= $_; }else{ $mailcc .= $_; } }

You don't change $c inside the loop so $mailto gets everything and $mailcc gets nothing.    What you want is:

# put first element in array as mail_to and all others as CC my $mailto = shift @$mailref; my $mailcc = join '', @$mailref;


Comment on Re^5: RFC: beginner level script improvement
Select or Download Code
Re^6: RFC: beginner level script improvement
by georgecarlin (Acolyte) on Sep 26, 2013 at 09:17 UTC
    ah I forgot the increment there (should have tried -s with multiple args before posting the code here, apologies for the sloppyness). thank you for noticing as well as providing this shorter and more elegant solution. I suspect there are multiple places where shift/join might be better than what I am doing, I'll check out the join documentation and see if it's a better fit. Thanks again.

      Perhaps you should also look at using operators like push, for example in your code:

      my (@commands,@matches,$ip); for (my $i=0; $i < @temp; $i++){ #process each subset if ($i == 0){ my @temp1 = split(/:/, $temp[0]); #this is the fir +st subset, it contains the IP $ip = $temp1[0]; $count++; $temp[0] =~ s/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9 +][0-9]?)\.){3}(?:25[0-5]|2[0-4 +][0-9]|[01]?[0-9][0-9]?)://; #remove IP from subset } my @arr; if ($temp[$i] =~ /\=/ ){ #this subset contains pattern +-matching instructions my @temp2 = split(/\=/, $temp[$i]); #split subset +into further subsets, first co +ntaining the command, the rest are patterns $temp2[0] = substitute_placeholders($temp2[0],\@me +tachars,\@translations); #all +splitting is done on this string, revert it back to its original form $commands[$i] = $temp2[0]; #this is the command for (my $c=1; $c < @temp2; $c++){ #these are all t +he patterns $temp2[$c] = substitute_placeholders($temp2[$c +],\@metachars,\@translations); + ##undo prior substitutions $arr[$c] = $temp2[$c]; } $matches[$i] = \@arr; }else{ #this subset does not contain pattern matching +instructions $temp[$i] = substitute_placeholders($temp[$i],\@me +tachars,\@translations); ##und +o prior substitutions $commands[$i] = $temp[$i]; $arr[$i] = "no-match-hook"; ##set flag for easy id +entification later on in the s +cript $matches[$i] = \@arr; } } $$DATA{$ip}->{commands} = \@commands; $$DATA{$ip}->{matches} = \@matches;

      It looks like you could use push instead of array indexes.    Something like this:

      my ( @commands, @matches, $ip ); for my $i ( 0 .. $#temp ) { #process each subset if ( $i == 0 ) { my @temp1 = split /:/, $temp[ 0 ]; #this is the fi +rst subset, it contains the IP $ip = $temp1[ 0 ]; $count++; $temp[ 0 ] =~ s/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0 +-9][0-9]?)\.){3}(?:25[0-5]|2[0-4 +][0-9]|[01]?[0-9][0-9]?)://; #remove IP from subset } my @arr; if ( $temp[ $i ] =~ /=/ ) { #this subset contains patt +ern-matching instructions my @temp2 = split /=/, $temp[ $i ]; #split subset +into further subsets, first co +ntaining the command, the rest are patterns push @commands, substitute_placeholders( $temp2[ 0 + ], \@metachars, \@translations ); #all +splitting is done on this string, revert it back to its original form for my $c ( 1 .. $#temp2 ) { #these are all the pa +tterns push @arr, substitute_placeholders( $temp2[ $c + ], \@metachars, \@translations ); + ##undo prior substitutions } push @matches, \@arr; } else { #this subset does not contain pattern matchin +g instructions push @commands, substitute_placeholders( $temp[ $i + ], \@metachars, \@translations ); ##und +o prior substitutions push @arr, "no-match-hook"; ##set flag for easy id +entification later on in the s +cript push @matches, \@arr; } } $$DATA->{ $ip }{ commands } = \@commands; $$DATA->{ $ip }{ matches } = \@matches;

        I thought about using push and foreach instead of all the indexing, counting and such, but decided against it, because I wasn't sure it would work.

        The reason being that I need 100% certainty that the command=match relationship is deducable from the data structure, in all of the following cases:
        - command X and NO matches
        - command X and match y
        - command X and match y + ...match n

        if I use push in all cases won't this logic collapse? or maybe I just have to push undef, if that's possible. Again, Thank you for taking the time.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1055776]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (15)
As of 2014-07-10 20:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (216 votes), past polls