Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: a Couple of questions!

by bart (Canon)
on Sep 20, 2003 at 14:11 UTC ( #292857=note: print w/replies, xml ) Need Help??

in reply to a Couple of questions!

a) There's a recent thread here (I think in meditations, "Perl idioms explained", but I'll look it up for you) that explains what happens with // and //g. In short: you need to use //g in list context, and extract the count from there.

$count = () = $data =~ m/and/g;

Update: Got it: Perl Idioms Explained - @ary = $str =~ m/(stuff)/g

b) Even if it's another "and"? Use lookahead.

my($next_word) = /and(?=\W*(\w+))/;
You can use list context again, in which case you'll get a list of list of matches, but flattened.
my @pairs = /(and)(?=\W*(\w+))/g;
Test script:
$_ = 'foo and bar and baz went to the sea.'; my @pairs = /(and)(?=\W*(\w+))/g; print join "#", @pairs;

c) That's just his point of view. Tastes vary. I happen to disagree. But that's just me. :)

d) Dunno. Maybe you should try out one of those template modules. That way you can separate code (script) from design (template). There's a nice list in the intro chapter of the Mason book: chapter 1

There's also a comparative list in this article on <>: Choosing a Templating System

Replies are listed 'Best First'.
Re: Re: a Couple of questions!
by Nik (Initiate) on Sep 20, 2003 at 14:49 UTC
    Can you please explain a) and b) more detailed please? i tried out yous answer of a) which is
    $count = @count = $data =~ m/and/g; and it works ok
    also this works as well
    $count = () = $data =~ m/and/g;
    The problem is that i don't seem to understand why they are working!!! can u please expalin a liitle bit more? as far as for b) is this
    $_ = 'foo and bar and baz went to the sea.'; @pairs = /(and)(?=\W*(\w+))/g; print join "#", @pairs;
    the same as this?
    $data = 'foo and bar and baz went to the sea.'; @pairs = $data =~ /(and)(?=\W*(\w+))/g; print "@pairs";
    i dont understand this line of yours!
    @pairs = /(and)(?=\W*(\w+))/g;
    do you mean this?
    @pairs = $data =~ /(and)(?=\W*(\w+))/g;
    is $data implied on this? and what about the w the * and the +?

    edited: Sat Sep 20 15:00:09 2003 by jeffa - code tags

      First off Nik, please start using code tags and making your posts look nice in general. When you post something to this site, you are suppose to click the PREVIEW button first - don't click SUBMIT until the posts looks good. How do you make a post look good? By using our Perl Monks Approved HTML tags and reading over Writeup Formatting Tips first. I have looked at your previous posts and see that you have never learned this simple, important step for getting good information and respect from this community.

      If you want to understand the code you have been given, please try to do a little research on your own. A good perldoc page to read is perlre, this contains every answer to all of your regex questions.

      What is hard to understand, however, is this:

      $count = () = $data =~ m/and/g;
      so let's break it down. Hopefully, you realize that $data =~ m/and/ is a boolean test. It just asks "does $data contain the word 'and'?". If it does, then the answer to the question is true. If it does not, then the answer to the question is false. However, we don't want to know if 'and' appears or not, we want to count how many times it does appear.

      Hopefully you also know about the 'g' modifier. This allows us to match the pattern more than once. Of course, in our simple version above, 'g' is not needed because we only ask if 'and' appears or not - the number of times is irrelevant.

      This finally brings us to "scalar context" versus "list context" (see perldata). As you found out in your original question, this does not work:
      $count = $data =~ m/and/g;
      Because we ask the question in "scalar context", we get back ... true (assuming $data does contain 'and'). In order to actually count the occurances, we have to explicitly tell Perl that we want "list context". This is why we have the bare parens (an empty list) in between $count and $data. If you still don't understand, don't worry -- this took me a few months to understand. :)

      The rest of your questions are all addressed in perlre. I would answer them now, but since you didn't use code tags ... i will instead instruct you to go do some reading. And next time, please for the love of God use code tags man.

      Oh, and as for Python versus Perl ... what do you think we are going to say? This is a Perl site! :P


      (the triplet paradiddle with high-hat)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://292857]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2022-12-03 22:22 GMT
Find Nodes?
    Voting Booth?

    No recent polls found