Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I won't give you the whole solution, just tell you what went wrong (at least what I saw) :

Reading through you file

while (<INPUt>) { push @array, $_; }
This does the same thing as : @array = <INPUT>, but since you are processing your file line by line, you don't have to use an array. So in your case you can do:
while (<INPUT>) { if (/$pattern/) # matches work by default on $_ { do something with $_; } }

Your $pattern variable

Your $pattern variable is the empty string, because $pattern = /regex/ means "process /regex/ on the default variable $_ then put the result in $pattern". Since $_ is still empty, the result has to be false. This will work if you just make $pattern a string (or a qred expression in more advanced perl).

While programming, if you need to paste 4 times the same thing in a row, you did it wrong. In a regexp, to repeat some part you can use the {n,m} syntax. Something like /([0-9][0-5]\.){3}/ # three times a two-digit number followed by a dot. Or you can interpolate an inner pattern into the bigger one with something like:

$inner = "([0-5][0-9]|[0-9][1-8])"; $outer = "$inner.$inner.$inner/[0-6]"; # Can also be written $outer = +join('.', $inner x 3) . "[0-6]"
This is not the best solution, but it's probably both simple and effective enough for your needs. And that's still writing several times the same thing (unless you use the alternative syntax), but it's far easier to read, and if a change is required you don't have to copy it 3 times.

Getting the parts (here : numbers) of your string

Then to get the content of your match, you can either use captures (read perlquickre) or keep the split. But here you are splitting on spaces (that's what \s means in a regex), when your pattern doesn't allow spaces (which means the lines you want to split can't have any space). You can split on non-digit characters instead with : split /\D/, $_;


using Data::Dumper is a good thing, but here you're not doing anything with it. Do print the content of your variables when in doubt, this is how you'll be able to solve your problem by yourself, faster than you would by waiting for people to read your code.

And last advice: you can use \d instead of [0-9]

In reply to Re: Section of $string into new array by Eily
in thread Section of $string into new array by GeorgMN

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (6)
    As of 2018-03-24 20:11 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (299 votes). Check out past polls.