Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: Please help...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 8 Topic 11052 of 11531
Post > Topic >>

Re: Please help critcize and shorten my sub code

by Jenda@[EMAIL PROTECTED] (Jenda Krynicky) May 11, 2008 at 03:02 AM

From: Richard Lee <rich.japh@[EMAIL PROTECTED]
>
> Jenda Krynicky wrote:
> > You may do something like this:
> >
> >   for my $wanted ( 
> >       [outsideroute_group_m => route_name => \$routename, 
> > route_group_id => \$routegroupid], 
> >       [outsideroute_trunk_m => route_group_id => \$routegroupid, 
> > trunkgroup_id => \$trunkgroupid], 
> >       [outsideotrunkgroup_m => trunkgroup_id => \$trunkgroupid, 
> > carrier_id => \$carrierid], 
> >       [outsidecarrier_m => carrier_id => \$carrierid, carrier_name => 
> > \$carriername], 
> >       [outsidecarrier_m => carrier_id => \$carrierid, active => 
> > \$carrier_active], 
> >   ) {
> >       if (exists $_->{$wanted->[0]}{$wanted->[1]} 
> >         and $_->{$wanted->[0]}{$wanted->[1]} eq ${$wanted->[2]}) {
> >           ${$wanted->[4]} = $_->{$wanted->[0]}{$wanted->[3]};
> >           last;
> >       }
> >   }
> >
> >   return($trunkgroupid,$carriername,$carrier_active);
> > }
> >   
> thanks.. I am looking at your solution(looks very complicated) and also 
> will alos look into DBD::SQLite as well

It's not terribly complicated once you get used to references. I just 
extracted the differing parts from your loops so that I could replace 
them by a single one. 

And I made a mistake doing so, the code should have been like this:

for my $wanted ( 
      [outsideroute_group_m => route_name => \$routename, 
route_group_id => \$routegroupid], 
      [outsideroute_trunk_m => route_group_id => \$routegroupid, 
trunkgroup_id => \$trunkgroupid], 
      [outsideotrunkgroup_m => trunkgroup_id => \$trunkgroupid, 
carrier_id => \$carrierid], 
      [outsidecarrier_m => carrier_id => \$carrierid, carrier_name => 
\$carriername], 
      [outsidecarrier_m => carrier_id => \$carrierid, active => 
\$carrier_active], 
  ) {
    for (@[EMAIL PROTECTED]
) {
      if (exists $_->{$wanted->[0]}{$wanted->[1]} 
        and $_->{$wanted->[0]}{$wanted->[1]} eq ${$wanted->[2]}) {
          ${$wanted->[4]} = $_->{$wanted->[0]}{$wanted->[3]};
          last;
      }
    }
  }

 return($trunkgroupid,$carriername,$carrier_active);
}

I've forgotten the inner loop.

Maybe you'd like it better if I would have hidden the inner loop in a 
subroutine:

  my $routegroupid = findValue( 'outsideroute_group_m', 'route_name', 
$routename, 'route_group_id'); 
  my $trunkgroupid = findValue( 'outsideroute_trunk_m', 
'route_group_id', $routegroupid, 'trunkgroup_id'); 
  my $carrierid = findValue( 'outsideotrunkgroup_m', 'trunkgroup_id', 
$trunkgroupid, 'carrier_id'); 
  my $carriername = findValue( 'outsidecarrier_m', 'carrier_id', 
$carrierid, 'carrier_name'); 
  my $carrier_active = findValue( 'outsidecarrier_m', 'carrier_id', 
$carrierid, 'active'); 

 return($trunkgroupid,$carriername,$carrier_active);
}

sub findValue {
  my ($dat, $section, $lookup_key, $lookup_value, $value_key) = @[EMAIL PROTECTED]
  for (@[EMAIL PROTECTED]
) {
    if (exists $_->{$section}{$lookup_key}
     and $_->{$section}{$lookup_key} eq $lookup_value) {
      return $_->{$section}{$value_key};
    }
  }
}

Looking at it again, I would like it better as well.

Jenda
P.S.: The => (sometimes called "fat comma") is almost equivalent to 
an ordinary comma, the only difference is that if the thing to the 
left of the => looks like a word it's automaticaly quoted. So
  [one => two => $three]
is equivalent to
  ['one', 'two', $three]

It's just syntactic sugar.
===== Jenda@[EMAIL PROTECTED]
 === http://Jenda.Krynicky.cz
=====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
	-- Terry Pratchett in Sourcery
 




 8 Posts in Topic:
Please help critcize and shorten my sub code
rich.japh@[EMAIL PROTECTE  2008-05-10 19:21:05 
Re: Please help critcize and shorten my sub code
Jenda@[EMAIL PROTECTED]   2008-05-11 02:05:06 
Re: Please help critcize and shorten my sub code
rich.japh@[EMAIL PROTECTE  2008-05-10 20:20:13 
Re: Please help critcize and shorten my sub code
Jenda@[EMAIL PROTECTED]   2008-05-11 03:02:04 
Re: Please help critcize and shorten my sub code
rich.japh@[EMAIL PROTECTE  2008-05-10 23:03:58 
Re: Please help critcize and shorten my sub code
Peter@[EMAIL PROTECTED]   2008-05-11 05:56:15 
Re: Please help critcize and shorten my sub code
Jenda@[EMAIL PROTECTED]   2008-05-11 15:44:09 
Re: Please help critcize and shorten my sub code
rob.dixon@[EMAIL PROTECTE  2008-05-11 20:57:56 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 0:59:38 CDT 2008.