<div dir="ltr"><br><div>   With the for-each walking through the dispatch list, that seems to just be about the equivalent of the chain of if then statements.  I started a simple tester and came up with this:</div><div><br></div><div>Input Lines:</div><div>   This is Line1.</div><div>   This is Line3.<br></div><div>   This is Line2.<br></div><div><br></div><div>And this script:</div><div><br></div><div><div>#!/usr/bin/perl -w</div><div>$| = 1;</div><div><br></div><div>use strict;</div><div>use diagnostics;</div><div><br></div><div>my $User=$ENV{"LOGNAME"};</div><div><br></div><div># GetOpt</div><div>use vars qw( $opt_f );</div><div>use Getopt::Mixed;</div><div>Getopt::Mixed::getOptions("f=s ");</div><div><br></div><div><br></div><div>my $Line;</div><div>my @Lines;</div><div>my %DispatchHash = (</div><div>  qr/Line1/ => \&Line1($Line),</div><div>  qr/Line2/ => \&Line2($Line),</div><div>  qr/Line3/ => \&Line3($Line),</div><div>);</div><div><br></div><div><br></div><div>open(INPUT, "<$opt_f") || die "Can't open $opt_f $?\n";</div><div>while(<INPUT>) {</div><div>  chomp;</div><div>  $Line=$_;</div><div>  print "\$Line :$Line:\n";</div><div>#  print "\$_ :$_:\n";</div><div><br></div><div>#  push(@Lines, $_);</div><div><br></div><div>  exit 0;</div><div>}</div><div><br></div><div>exit 0;</div><div><br></div><div>#</div><div># Subs</div><div>#  </div><div>sub Line1 {</div><div>  my $InputLine=$_[0] ||= "Undefined1";</div><div>  print "  Sub 1 :$InputLine:\n";</div><div>}  </div><div><br></div><div>sub Line2 {</div><div>  my $InputLine=$_[0] ||= "Undefined2";</div><div>  print "  Sub 2 :$InputLine:\n";</div><div>}  </div><div>sub Line3 {</div><div>  my $InputLine=$_[0] ||= "Undefined3";</div><div>  print "  Sub 3 :$InputLine:\n";</div><div>}</div></div><div><br></div><div><br></div><div>Unfortunately, my output looks like this:</div><div><div>Sub 1 :Undefined1:</div><div>Sub 2 :Undefined1:</div><div>Sub 3 :Undefined1:</div><div>$Line :Line1:</div></div><div><br></div><div>Where it should be:</div><div><div><br></div><div>This is Line1.</div><div>   Sub 1 :This is Line1:  </div><div>This is Line3.<br></div><div><span style="line-height:1.5">   Sub 3 :This is Line3:</span></div><div>This is Line2.</div></div><div>   Sub 2 :This is Line2:<br></div><div><br></div><div>Robert</div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Apr 19, 2016 at 5:25 PM Stuart A Johnston <<a href="mailto:saj@thecommune.net">saj@thecommune.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Here's a simple example. I've just used $ARGV[0] as the input but you<br>
could add another loop for your multi-line input.<br>
<br>
#!/usr/bin/perl<br>
<br>
use strict;<br>
use v5.10;<br>
<br>
sub sub_foo {<br>
     say "foo: $_[0]";<br>
}<br>
<br>
sub sub_bar {<br>
     say "bar: $_[0]";<br>
}<br>
<br>
my @dispatch = (<br>
     [ qr/foo/, \&sub_foo ],<br>
     [ qr/bar/, \&sub_bar ],<br>
);<br>
<br>
foreach my $d (@dispatch) {<br>
     my ($r, $sub) = @$d;<br>
<br>
     if ($ARGV[0] =~ $r) {<br>
         $sub->($ARGV[0]);<br>
         next;<br>
     }<br>
}<br>
<br>
<br>
<br>
On 04/19/2016 02:28 PM, Robert L. Harris wrote:<br>
><br>
> Anyone have a straight forward script using dispatch lists?  I have one<br>
> ( 4500 lines by now ) which effectively does this:<br>
><br>
>  1. Open input file<br>
>  2. while<input> {<br>
>  3.      $Line=<INPUT><br>
>  4.      &Sub1("Line") if ( $Line =~ /<regex pattern 1>/ );<br>
>  5.      &Sub2("Line") if ( $Line =~ /<regex pattern 2>/ );<br>
>  6.      &Sub3("Line") if ( $Line =~ /<regex pattern 3>/ );<br>
>  7.      ... about 25 patterns now ...<br>
>  8. }<br>
><br>
> Yeah, it's ugly, it started out as a 30 line data munger about 2.5 years<br>
> ago and I'm looking to speed and clean it up.   Each Sub performs<br>
> various actions based which can't be simplified or condensed more than<br>
> they have.<br>
><br>
>     I've created my DispatchHash for subs/patterns but I think I'm<br>
> confusing myself on actually doing the match and dispatch including<br>
> passing $Line to the sub.<br>
><br>
> Any examples would be very welcome.<br>
><br>
> Robert<br>
<br>
_______________________________________________<br>
Denver-pm mailing list<br>
<a href="mailto:Denver-pm@pm.org" target="_blank">Denver-pm@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/denver-pm" rel="noreferrer" target="_blank">http://mail.pm.org/mailman/listinfo/denver-pm</a><br>
</blockquote></div>