Having answered a number of your questions from last week, I suspect that there's a conceptual issue going on. You seem to be coming from a language which doesn't really handle variable scoping very well, and for whom variables get modified by actions that Perl doesn't modify them in. Note that I can't think of such a language, but your questions seem to be coming from that slant.

Your basic question is "How can I: do X to Y if Y has characteristic Z." You phrased it as "How can I print the 9th thing in X if I know that the 9th thing should have characteristic Y, but it might not be the 9th thing?"

As I reccommended before, think your problems through. Instead of rushing into an editor and typing away, write your solutions out on paper first. In fact, you might try writing out your problem there, too. Oftentimes, it's not that the solution eludes us, but the problem does.

I know it sounds like a waste of time, but think of it this way - if you manage to get it working without paper, great! But, you have been stopping into PM a lot. That's all well and good, but you're spending a lot of your time on understanding your problem vs. solving your problem. That time doesn't have to be spent like that.

If you write our your solution in pseudo-code, then you'll start to immediately notice parts you don't understand very well. Heck, write it out in English, as if you were going to explain your proposed solution to someone who doesn't program at all. An example could be as follow:

Problem: Given a paragraph, print out each line that contains the word "the".

Solution (in English):

  1. Put all the lines into a list.
  2. Go through the list, starting with the first line.
  3. For each line, check to see if it contains the word "the".
  4. If it does, then write it to the screen.
  5. If it doesn't, skip the line.
my @list = <LINES>; LINE: foreach my $line (@list) { if ($line =~ /(^|\s)the(\s|$)/i) { print "$line"; } else { next LINE; } }
(Before anyone smarts off, yes, I know the next LINE; isn't strictly necessary.)

See how each numbered part in English corresponds very nicely with the code? Try doing that once or twice for very simple examples and you'll see an immediate improvement in your understand of programming as a discipline.

------
We are the carpenters and bricklayers of the Information Age.

Vote paco for President!


In reply to Re: Isn't there a print line function? by dragonchild
in thread Isn't there a print line function? by brassmon_k

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.