Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

how to to find the "/" in a string

by robertw (Sexton)
on Aug 31, 2012 at 18:11 UTC ( #991062=perlquestion: print w/replies, xml ) Need Help??
robertw has asked for the wisdom of the Perl Monks concerning the following question:

I want to recognize the pattern after the "while" but because the "/" is used to open and close the string to be searched for I have no idea how to make the program count how many times "aug</td>" is in the string. Thank you so much in advance.

$html = get("$aandeel/historisch +e-koersen.aspx?maand=0") or die "could not get +sche-koersen.aspx"; while ($html =~ /aug</td>/g) { $count++ } print $count;

Replies are listed 'Best First'.
Re: how to to find the "/" in a string
by davido (Archbishop) on Aug 31, 2012 at 18:49 UTC

    The answer to the specific question you ask can be found in the first section of perlrequick:

    Finally, the // default delimiters for a match can be changed to arbitrary delimiters by putting an 'm' out front:

    "Hello World" =~ m!World!; # matches, delimited by '!' "Hello World" =~ m{World}; # matches, note the matching '{}' "/usr/bin/perl" =~ m"/perl"; # matches after '/usr/bin', # '/' becomes an ordinary char

    But so you do less work up front, and endure less pain in the longrun, you might benefit from one of the many HTML parsers on CPAN.


Re: how to to find the "/" in a string
by ww (Bishop) on Aug 31, 2012 at 18:39 UTC
    Homebrewed html parsers are rarely as good as homebrewed beer... even after considerable aging.

    Suggest you check the available modules with "parse" and/or "html" in their names; read their docs or at least each precis; and then pick one.

    Update: just checked. In less than 2 months, you've posted nearly 50 questions. Perhaps you've seen this advice before in reply to one or more of them: study a good introductory text ("Learning Perl" for example); read the tuts here and also look around the Monastery for highly regarded, free tuts available at some (emphasis, "some;" as not all are worth the powder to blow them to hell) on-line college level tuts or courses; and study (hint: google terms terms terms... or Super Search here) nodes that show how others dealt with your problem(s).

Re: how to to find the "/" in a string
by AnomalousMonk (Canon) on Aug 31, 2012 at 19:23 UTC

    robertw: Please use  <c> ... </c> or  <code> ... </code> tags to enclose things like  "aug</td>" or  aug</td> that will otherwise render as HTML (since they include valid HTML tags!). (Didn't your post appear a bit strange to you when you previewed it?) Please see Markup in the Monastery and Writeup Formatting Tips.

      Thanks guys:), the ironic statements are well deserved yes I knowxD, and I did not notice the missing tags, my apologies for that

Re: how to to find the "/" in a string
by philiprbrenan (Monk) on Aug 31, 2012 at 21:39 UTC

    Here is a start. The regular expression gets the size of the array returned by the regular expression, which uses () to enclose the text to be searched for and /g to find all the occurences.

    use feature ":5.14"; use warnings FATAL => qw(all); use strict; my $h = "aaa bbb ccc aaa aaa"; say scalar (() = $h=~m(aaa)g);


Re: how to to find the "/" in a string
by Kenosis (Priest) on Aug 31, 2012 at 19:32 UTC

    Have you tried feeding the goat(se)?

    $count =()= $html =~ m{aug</td>}g; print $count;

    Update: Didn't see AnomalousMonk's posting already mentioning this...

Re: how to to find the "/" in a string
by BrowserUk (Pope) on Aug 31, 2012 at 18:18 UTC

    Pull hard. When it breaks, where the slash in the string was will be obvious :)

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Re: how to to find the "/" in a string
by cheekuperl (Monk) on Sep 03, 2012 at 03:28 UTC
    how to to find the "/" in a string
    perl -e '$str=q (usr/bin); print index($str,q(/))."\n";'
    The output is
Re: how to to find the "/" in a string
by pvaldes (Chaplain) on Aug 31, 2012 at 18:30 UTC

    Not sure if I'm understanding the problem. You can search for a special character putting a "\" before "my_special_character"

    count=0, while ($html =~ [aug<\/td>]g) { $count++ }
      count=0, while ($html =~ [aug<\/td>]g) { $count++ }

      Please don't post code that won't work – or, in this case, even compile. (Or if the code is untested, please flag the fact prominently.) A beginning Perler, even one as reluctant as robertw to read basic documentation, deserves better of us.

      >perl -wMstrict -le "my $html = 'xxx aug</td> yyy aug</td> zzz'; ;; my $count = 0; while ($html =~ m[aug</td>]g) { $count++ } print $count; ;; my $count2 =()= $html =~ m{ aug</td> }xmsg; print $count2; " 2 2

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://991062]
Approved by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2017-01-19 00:56 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (166 votes). Check out past polls.