Your problem is that the precedence of
== (numeric equal) is
higher than that of
, (comma). This means that the
numeric comparison happens
before the call to
index, so every one of your
index
calls is returning the position of 1 in the string (numeric comparison
of any string with 0 yields true or 1), and unless the string
actually contains 1, your
if( index... lines will
all return -1, which
if interprets as true. Unfortunately
if the string
does contain 1,
index will return the postion of the
digit, which will also be seen as true by
if unless the digit
happens to be at position 0 (first in the string).
To make a long story short, all your data match all your conditionals
and so all your variables are set to each line. Since your "gold" line
is last in your data, all the variables have been set to that
line by the time your script produces output. To change this, you
can force the precedence you want using brackets in the appropriate locations.
All of that however is evading many handy Perl features. One easier way to
do it which is still similar to yours would be to use
perlfunc:split to extract your keywords and values from your
data lines, along the lines of:
my($keyword,$value)= split ':', $line;
as the first line in your loop, then just test the possible
values of $keyword. There are many "more Perlish" ways to do this, but
this should suffice without getting you too confused for now.
Finally, you should always use strict, and check the return value
when you open a file. I hope this helps!
--
I'd like to be able to assign to an luser
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.