Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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/, $_;


Misc

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

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (10)
    As of 2014-09-16 10:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

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











      Results (10 votes), past polls