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

comment on

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

You can make your code rather more simpler and hence, easier to read:

Initialise $count to zero

my $count = 0;

...or, even more simply

my $count;

Then you don't need that perverse

$count = $count - 1;

statement. And even if you did want it, what's wrong with


(always bearing in mind the old TIMTOWTDI acronym of course!)

The next simplification, if the file really does consist of just one line, is to slurp it in, in one hit

@array = <$fh>; # Reads all lines into array $singleLine = <$fh>; # reads one line only

Finally, and this is where I think your question might get answered, you can capture regex hits to an array and then simply count the array...

my @count = $w =~ /$x/g; my $total = @count;

With no loops making it look more complicated than it is, you can then start to pin down what is going on here. Perhaps use some test data/test regex to give a simpler starting block. I would also be suspicious of perl being greedy in that there regex. But first, create some test data and make sure you can "prove" your code before hitting the real data.

In reply to Re: Regex shows only last match multiple times? by zeltus
in thread Regex shows only last match multiple times? by GertMT

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 avoiding work at the Monastery: (4)
As of 2022-01-22 05:59 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (61 votes). Check out past polls.