Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Contrary to what jcwren says, I would hardly label myself a regex guru, but thanks for the compliment :)

As far as I can tell, you can't use the match operator to return the length. The closest I came up with is a variation of Ozymandias' response:

$string = "This is a test\nMore Test"; $len++ for ($string =~ /./sg); print $len;
In the Perl Cookbook, it has a recipe for finding the nth occurence of a match, but that also uses a loop. If you could enumerate matches with a straight regex, you could construct a well-defined regex and skip the loop in the Cookbook.

Believe me, I tried :) If any monks would like to tackle this, I'd love to be proven wrong (and I'm sure it would be something ridiculously simple).

Update: And in my quest to come up with horribly unoptimized code:

$_ = "This is a test\nMore Test"; $len = (split //); print $len;
Or you can use this beauty (no, I'm not serious):
$string = "This is a test\nMore Test"; $len = (grep /./s, (@chars = split //, $string));
No, I don't suggest using them. I just had to toss it out because no one else had mentioned it :)

In reply to RE: RE: Counting Characters by Ovid
in thread Counting Characters by NeverMore

Title:
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 How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 12:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found