Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Move the match with /g into a condition of a while loop.

As there are no capture groups in the regex (i.e. no parentheses), using any other index than 0 makes no sense. Under warnings, you'd even get lots of Use of uninitialized value in print.

Moreover, when there's no match, the @- and @+ arrays are empty, there's no -1 (that's what index does). You have to handle that case yourself.

This should work directly as shown and output the expected output:

#!/usr/bin/perl use strict; use warnings; my $string = << "__EOS__"; foo bar baz foo bar foo\tbaz ccc ddd foo bar foo\tbaz ccc ddd\tfoo\tbar\tbaz foo bar foo\t\tbaz ccc ddd\t\t\tfoo __EOS__ open my $F0, '<', \$string or die $!; while (<$F0>) { my $match; while (/\t+/g) { $match = 1; print $-[0], ' ', $+[0], ' '; } print '-1 -1' unless $match; print "\n"; }

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Re: printing LAST_MATCH_START/@- LAST_MATCH_END/@+ array where regex match begin/end by choroba
in thread printing LAST_MATCH_START/@- LAST_MATCH_END/@+ array where regex match begin/end by perl_boy

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2022-07-06 09:58 GMT
Find Nodes?
    Voting Booth?

    No recent polls found