Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Ensuring Each If Statement Is Evaluated

by Anonymous Monk
on Oct 17, 2002 at 08:13 UTC ( #205953=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I currently have a script that reads the contents of an excel file into a hash, keyed on column titles. Each record (approx 20 column titles and one respective value for each) is then looped over and printed to a new file.

The problem I am having is before I print each record to the new file, I want to execute a series of if statements over the record, so that if it matches *any* of the statements, the function is executed accordingly. Here is a code snippet:

my $rec = $G::result[0]; $fh->print( join( "\t", sort keys %$rec), "\n" ); foreach my $rec ( @G::result ) { LogGeneral("Here's the record : $rec"); #B/L = B & f/r p type = RR if (($rec->{"borrow_lend_indicator"} == "B") && ($rec->{"f +ee_rebate_payable_type"} == "RR")) { $rec->{"accrued_fee_rebate_value"} = "+" . $rec->{"acc +rued_fee_rebate_value"}; #B/L = L & f/r p type = RR } elsif (($rec->{"borrow_lend_indicator"} == "L") && ($rec +->{"fee_rebate_payable_type"} == "RR")) { $rec->{"accrued_fee_rebate_value"} = "-" . $rec->{"acc +rued_fee_rebate_value"}; #f/r type = FR } elsif ($rec->{"fee_rebate_payable_type"} == "FP") { $rec->{"accrued_fee_rebate_value"} = "-" . $rec->{"acc +rued_fee_rebate_value"}; #f/r type = FP } elsif ($rec->{"fee_rebate_payable_type"} == "FR") { $rec->{"accrued_fee_rebate_value"} = "+" . $rec->{"acc +rued_fee_rebate_value"}; #B/L = B } elsif ($rec->{"borrow_lend_indicator"} == "B") { $rec->{"actual_cost_of_funds"} = "-" . $rec->{"actual_ +cost_of_funds"}; #B/L = L } elsif ($rec->{"borrow_lend_indicator"} == "L") { $rec->{"actual_cost_of_funds"} = "+" . $rec->{"actual_ +cost_of_funds"}; } foreach my $field ( sort keys %$rec ) { LogGeneral("Here's the field : $rec->{$field}"); $fh->print($rec->{$field},"\t"); } $fh->print("\n"); } $fh->close();

The problem is, it never really checks the record against every single if statement. I can't work out how to fix this. Any pointers / advice is most welcome.

Thanks

Comment on Ensuring Each If Statement Is Evaluated
Download Code
Re: Ensuring Each If Statement Is Evaluated
by ignatz (Vicar) on Oct 17, 2002 at 08:31 UTC
    else and elsifs only get checked if every if and elsif before it was false:
    #!/usr/bin/perl -w use strict; my $bool = 1; if ($bool) { print "/\$bool: I'm true, so this get's printed.\n"; } elsif ($bool) { print "/\$bool: You'll never see me if I'm true.\n"; } else { print "/\$bool: Nobody loves me, everybody hates me.\n"; }
    If you want to check every condition, use a serious of ifs:
    if ($bool) { print "/\$bool I'm true, so this get's printed.\n"; } if ($bool == 1) { print "/\$bool You love me! You really love me!\n"; } if ($bool != 2) { print "/\$bool First, I want to think my manager...\n"; }
    ()-()
     \"/
      `                                                     
    
Re: Ensuring Each If Statement Is Evaluated
by BrowserUk (Pope) on Oct 17, 2002 at 08:32 UTC

    Just remove the "els" parts of the statements! eg. Instead of this

    ... if ( test1 ) { ... } elsif ( test2 ) { ... } elsif (test3) { ... } .... #etc

    do this..

    ... if (test1) {...} if (test2) {...} if (test3) {...} ...

    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
Re: Ensuring Each If Statement Is Evaluated
by Anonymous Monk on Oct 17, 2002 at 09:04 UTC

    Thanks for your input.

    I changed as per your comments above (and had in fact already tried this). The problem now is that each $rec is now printed to the file with changes made by *all* statements. For example, the first four statements affect "accrued_fee_rebate_value". This is now printed to the file for each record with "+--+" at the beginning. The last two statements deal with "actual_cost_of_funds", which is now printed with "+-" at the beginning of each record.

    So either I've got a problem in what I'm trying to match, or the logic is still flawed...

Re: Ensuring Each If Statement Is Evaluated
by azatoth (Curate) on Oct 17, 2002 at 09:13 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-10-25 22:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls