Hang in there my friend. It looks like you are really trying. It will come...

Next time you might try posting in smaller gulps. There is a lot here to comment on. Just post your immediate problem, demonstrate your best attempt, tell what the input and output are, mention any error messages, and tell us where you looked in the docs or on Perl Monks for enlightenment (so we know you made an honest try to dope it out).

I strongly encourage you to indent your code. It will show you things you are not seeing and it makes it much easier for both you and us to quickly grasp the flow and logic of what you are trying to do. Many sources recommend a 2 to 4 space indent. (So of course I use 3.) Like this:

while(<FILE>) { flock(FILE, 2); @line = split(/ /, $_); $itemDB = $line[0]; if ($itemDB eq $item) { $found++ } }

Regarding comments: Try not to comment in a way that simply duplicates what your code says. This becomes a crutch and actually makes it harder for you to learn to look at the code and "see" what it is doing just by reading it. On the other hand, not duplicating the meaning of your code in a comment, helps you to code in a way that is more clear in the first place. Comments are for explaining why you did something non-standard, marking chunks of code, summarizing the purpose of a block of code, etc.

And in at least two places, your comments (which appear to be simply a restatement of the code) incorrectly describe what the code is doing. So if you are reading the comments to see what is happening, you will be misled.

And if you use indenting of blocks, you don't need the 'end if' or 'end while' comments unless the block is more than a screen long. The relationship of the end curly and the block it ends is clear visually.

Next, to your questions.

1. The default pattern for split is whitespace (i.e. \s) which includes \t. And note that it is back-slash and not forward-slash as you had it.

2. I will not comment the need for flock but I will point out that in both examples you are locking the file many times but only unlocking it once. This is the kind of thing that becomes more clear when you indent.

3. Writing to a file with a tab delimiter is just like writing anything else:      print OUTFILE "$var1\t$var2\t$var3\n"; or perhaps      print OUTFILE $ary[0] . "\t" . $ary[1] . "\t" . $ary[2] . "\n"; There are ways to get fancier than that, but they can come later when you are more confident. Be careful not to use single quotes around the \t. That will just give you a literal back-slash and a 't'. Not a tab.

4. I'm not sure exactly what you mean here so I'll leave that for someone else or a follow-up by you.

Second Example: (again this is something that indenting would make clear) near the end you use a curly bracket to close the 'else' and then you close the 'if'. But you already closed the if block right before the word 'else'. This kind of error can sometimes cause errors at some distance from the source of the problem. In this example the curly labeled '#end if' actually closes the 'while' block. Not what you intended at all. And if there were lots of code between the end of the if block and the real end of the while block, the resulting error msg might be hard to make sense of.

Yes you can escape from the loop early. Here's an example that also includes a split on tabs:

while(<FILE>) { @line = split(/\t/, $_); $itemDB = $line[0]; if ($itemDB eq $item) { &Sub1(); last; # <---- } else { &Sub2(); } }
My instinct is for you to clean up this much and then see where you are. Some of the other issues may resolve themselves in the process.

Good luck. Breath easy... Small gulps.... And keep in touch. ;-)

------------------------------------------------------------
"Perl is a mess and that's good because the
problem space is also a mess.
" - Larry Wall


In reply to Re: Breaking a loop after a valid match + proper use of /t delimiter. by dvergin
in thread Breaking a loop after a valid match + proper use of /t delimiter. by cobes

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.