Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^7: RFC: beginner level script improvement

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


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

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;


Comment on Re^7: RFC: beginner level script improvement
Select or Download Code
Re^8: RFC: beginner level script improvement
by georgecarlin (Acolyte) on Sep 27, 2013 at 09:10 UTC

    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://1055895]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2014-09-22 08:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (184 votes), past polls