Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Using Hashes

by Anonymous Monk
on Jun 19, 2003 at 23:25 UTC ( #267386=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I'm getting an error which I believe I'm trying to use a hash which consist of a number and compare to see if that number is given. If it is, then I want to print out a string. Unfortunately, I get this error:

Argument "2EN" isn't numeric in eq at Calsdiscrete.pl line 71

This is my code:
if (($cals_info {$cals_type} {low_count_no} == $cals_info {$cals_type} {high_count_no}) == 0){<br> print ($cals_info {$cals_type} {$cals_state_0});<br> }<br> else<br> { <br> if (($cals_info {$cals_type} {low_count_no} == $cals_info {$cals_type} {high_count_no}) == 1){ <br> print ($cals_info {$cals_type} {$cals_state_1});<br> }<br> }<br>
Can you tell me what I'm doing wrong?
My text file is this:
"A1","BL1CHILDB0","0","0","OFF" "A1","BL1CHILDB0","1","1","ON" "A1","BL1CHILDB1","0","0","DIS" "A1","BL1CHILDB1","1","1","ENA" "A1","BL1CHILDB2","0","0","ZERO" "A1","BL1CHILDB2","1","1","ONE" "A1","BL1CHILDB3","0","0","DAN" "A1","BL1CHILDB3","1","1","JOHN" "A1","BL1CHILDB4","0","0","RICH" "A1","BL1CHILDB4","1","1","MARK" "A1","BL2CHILDB5","0","0","OFF" "A1","BL2CHILDB5","1","1","ON" "A1","BL2CHILDB6","0","0","OFF" "A1","BL2CHILDB6","1","1","ON" "A1","D1CHILDB0","0","0","OFF" "A1","D1CHILDB0","1","1","ON" "A1","D2CHILDB1T3","0","3","0T3" "A1","D2CHILDB1T3","4","5","4T5" "A1","D2CHILDB1T3","6","7","6T7" "A1","D3CHILDB4T5","0","0","DIS" "A1","D3CHILDB4T5","1","1","1EN" "A1","D3CHILDB4T5","2","2","2EN" "A1","D3CHILDB4T5","3","3","12EN" "A1","D4CHILDB6","0","0","OFF" "A1","D4CHILDB6","1","1","ON"
I'm reading the two columns of numbers and I want to print the last column. ex. (on,off)

203-06-21 edit ybiC: <tt>, <code> and <p> tags

Comment on Using Hashes
Select or Download Code
Re: Using Hashes
by Anonymous Monk on Jun 19, 2003 at 23:45 UTC
    your problem may be two things: first, if both $cals_info{$cals_type}{low_count_no} and $cals_info{$cals_type}{high_count_no} are supposed to be from the columns of just numbers, then one of the two is being misassigned to the last column. second, if one of the two is *supposed* to be the last column, then you're using the incorrect operator. "eq" is the string comparison op. "==" is the numerical comparison op.
Re: Using Hashes
by graff (Chancellor) on Jun 20, 2003 at 06:55 UTC
    For one thing, you want to learn about using the <code> tag when posting code or other stuff that should look like code.

    For another thing, your "if" expressions are much more verbose than they should be. Assuming that "low_count_no" and "high_count_no" are supposed to refer to the values of the third and fourth columns in your data file (e.g. numeric values 0 and 3 respectively, for this line: "A1","D2CHILDB1T3","0","3","0T3") -- and assuming that you are parsing the file correctly when you load it into your data structure -- your logic would normally look like this:

    if ($cals_info{$cals_type}{low_count_no} != $cals_info{$cals_type}{hig +h_count_no}) { print $cals_info{$cals_type}{$cals_state_0}; } else { print $cals_info{$cals_type}{$cals_state_1}; }
    Though for cases like this, I personally prefer to use the ternary operator:
    print ($cals_info{$cals_type}{low_count_no} != $cals_info{$cals_type}{ +high_count_no}) ? $cals_info{$cals_type}{$cals_state_0} : $cals_info{$cals_type}{$cals_state_1};
    But, as suggested in the earlier reply, the error report you're getting suggests that you are not parsing the data file correctly, and for at least the line that has "2EN" in it (if not other lines as well), one of those "count_no" elements is holding that last field, instead of the intended digit value.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (14)
As of 2014-09-30 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (367 votes), past polls