CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to shrink code by reducing if conditions

 

First page Previous page 1 2 Next page Last page  View All


Tejas
User

Apr 26, 2014, 8:09 AM

Post #26 of 35 (2382 views)
Re: [FishMonger] How to shrink code by reducing if conditions [In reply to] Can't Post

my bad, iam sorry. I dint see the keys.
But, If the values are unqiue like below


Code
$hash{181}{304}{CardCreation}="\tTOC\tSTALLS\tAuto\tCCA \n";   
$hash{181}{304}{PostCreation}="\tTOC\tSTALLS\tAuto\tCCA";
$hash{181}{304}{CreationDay}="\tTOC\tSTALLS\tAuto\tCCA";

So This can jsut be

Code
 my %detail = (    
Creation =>"\tTOC\tSTALLS\tAuto\tCCA" )

Did i get it right ?


FishMonger
Veteran / Moderator

Apr 26, 2014, 8:18 AM

Post #27 of 35 (2376 views)
Re: [Tejas] How to shrink code by reducing if conditions [In reply to] Can't Post

Yes, that is correct as long as the values are all the same.


BillKSmith
Veteran

Apr 26, 2014, 8:19 AM

Post #28 of 35 (2375 views)
Re: [Tejas] How to shrink code by reducing if conditions [In reply to] Can't Post

Each approach has advantages. In a production environment, I would try to guess what kind of changes we should expect in the future. Adding an entry to the hash version would be easier than modifying the dispatch version if it required adding new functions to the dispatch table. On the other hand, adding details to an existing function would be easier (and less error prone) than creating a new entry in the hash version.

Now you tell me that your crystal ball is broken. How do you choose? Make your best guess. Its probably better than ignoring the issue.
Good Luck,
Bill


Tejas
User

Apr 26, 2014, 8:28 AM

Post #29 of 35 (2374 views)
Re: [BillKSmith] How to shrink code by reducing if conditions [In reply to] Can't Post

Hi Bill

I am going to implement both
1. Hash Version for making my mentor happy.And the new concept of using a wild card like keys in keys has cut my work a lot.
i will try it once i get the premissions to load the package Tie::Hash::Regex;

2.The dispatch table version for myself to learn .
Its been tremendous that you have been mentroing me constantly and providing me the solutions for almost every problem i have.

Will definitely get back once i start implementing hash key patterns in few hours .
Thanks a Ton Guys.


Tejas
User

Apr 26, 2014, 11:57 AM

Post #30 of 35 (2363 views)
Re: [FishMonger] How to shrink code by reducing if conditions [In reply to] Can't Post

Can You please tell me whether i have replaced this correctly
Here Iam Actually trying to find the line starting with 5S or 5W or 5D


Code
if (/\t182\t201\t/ && ((/^5S/ || /^5W/ || /^5D/ || /^5F/ || /^2I/ || /^6B/ || /^4Y/) && /\tnone\t/)) {$tagged = $line."\tSL\tBS\tAuto\t Freetime (none)\n";


Code
$hash{182}{201} {qr/^2I(\s+|-?("what shud be here for any string" )none?/i} =  "\tSL\tBS\tAuto\t Freetime (none)\n";


This is how input looks and if condition worls for this, This is actually edge case where i search for the system codes at the start of the lines..

Quote
2I 12110 0 DummyAccounts 182 201 SLA 19-MAR-14 800000084185 BS DOD none


Thanks
Tejas


FishMonger
Veteran / Moderator

Apr 26, 2014, 12:26 PM

Post #31 of 35 (2359 views)
Re: [Tejas] How to shrink code by reducing if conditions [In reply to] Can't Post

This applies the pattern matching to $_ not $line. Given your latest sample, I'm not sure which var actually holds the string.

Code
my $re = qr/^5[SWDF]|2I|6B|4Y/; 

if ( /\t182\t201\t/ && $re && /\tnone\b/ ) {


Personally, I think it's a mistake to apply the regex's to the entire string given that the items you're matching are in known fields. I'd split the line into it separate fields and test against those specific fields.


Tejas
User

Apr 26, 2014, 8:34 PM

Post #32 of 35 (2272 views)
Re: [FishMonger] How to shrink code by reducing if conditions [In reply to] Can't Post

Correct
The Code below

Code
my $re = qr/^5[SWDF]|2I|6B|4Y/;  

if ( /\t182\t201\t/ && $re && /\tnone\b/ ) {


was the old version and now as i am preparing hashes ,
i need a version to include these kind of edge cases in has itself.

And , I dint Save this first valueto any variable


Code
while(my $line = <IN>)  
{
chomp($line);
my ($Code,$Source,$Category,$Desc) = (split "\t",$line)[0,4,5,7];
if($hash{$Source}{$Category}{$Desc}){
print OUT "$line.\t$hash{$Source}{$Category}{$Desc}";
else if
if($hash{$Source}{$Category}{$Code}{$Desc}){
print OUT "$line.\t$hash{$Source}{$Category}{$Desc}";
}
}

Now we have 5[SW..] in $cc and NONE in $Desc

And Is the final hash like below

Code
 
my $re = qr/^5[SWDF]|2I|6B|4Y/;
$hash{181}{201}{$re}{none} = "\tSL\tBS\tAutomated\t Freetime (none)\n";



Quote
Yeah, I will also have to create a new hash with four keys in it , as my main has just has 3 keys in it.



Tejas
User

May 2, 2014, 4:18 AM

Post #33 of 35 (2108 views)
Re: [FishMonger] How to shrink code by reducing if conditions [In reply to] Can't Post

Hi

Why are Regexp not working for Multi Level Hashes.

Code
$hash1{qr/IGC/i} = "Kranthi";

This hash is working properly and printing the output .

But The multi level hash

Code
$hash1{qr/IGC/i}{qr/EFC/i}

is not working at all.

Is it teh case that we can use regexp only for one level hash , but nt multi level ?

Thanks
Tejas


FishMonger
Veteran / Moderator

May 2, 2014, 6:13 AM

Post #34 of 35 (2063 views)
Re: [Tejas] How to shrink code by reducing if conditions [In reply to] Can't Post


Quote
But The multi level hash

Code
	 
$hash1{qr/IGC/i}{qr/EFC/i}

is not working at all.


I have not used either of those Tie:: modules I pointed you to, but I would not expect that multi-level hash to work.


(This post was edited by FishMonger on May 2, 2014, 6:14 AM)


Tejas
User

May 2, 2014, 6:35 AM

Post #35 of 35 (2049 views)
Re: [FishMonger] How to shrink code by reducing if conditions [In reply to] Can't Post

Hi
Well, i dint really understand why i will nt work
Is there any specific reason why it ll not work
I have use tie module and it worked for a single level hash, but notnfor multilevel hash



Thanks

First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives