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

Re^6: A Beginner Needs Homework help

by Swizzlestix617 (Novice)
on Apr 11, 2014 at 01:40 UTC ( [id://1081900]=note: print w/replies, xml ) Need Help??


in reply to Re^5: A Beginner Needs Homework help
in thread A Beginner Needs Homework help

lol.

I realized what you meant and changed the program so it would die if not read. what I'm trying to work on now is the pattern matching. Upon reading my textbook I don't really think I need substitution. What I need my program to do is to recognize similar words with their numbers and add them together, otherwise look over the word (and associate number) but print them anyway.

($info=~ /(\w+ (-?\d+ )+)/)

That should match any word with its number, since \w is for word charters and \d is for numbers. + is for one or more of the same to match.. the and ? is so that the last number can still be counted. the dash in front of it is undefined to me by my text book.. (elements of programming with perl)

Replies are listed 'Best First'.
Re^7: A Beginner Needs Homework help
by Anonymous Monk on Apr 11, 2014 at 21:37 UTC

    lol... I don't really think I need substitution...

    :) sounds good

    ... That should ... the dash in front of it is undefined to me by my text book.. (elements of programming with perl)

    What does it say a regular expression is composed of? What is the special characters like "?" called? So if its not the same kind of character as the special "?" character, its what kind of character?

    :) Here is what rxrx says about the pattern

    (The start of a capturing block ($1)
      \w+Match an identifier character, one-or-more times (as many as possible)
      (The start of a capturing block ($2)
        -?Match a literal '-' character, one-or-zero times (as many as possible)
        \d+Match a digit, one-or-more times (as many as possible)
      )+The end of $2 (matching one-or-more times (as many as possible))
    )The end of $1

    Its pretty close except it doesn't explain the literal space character ... it does show that in the next step

    If you give rxrx a string, it will go a step further and show you how the regex matches

    To do it run rxrx

    then paste 'red:27  yellow:102  green:311  yellow:12  blue:45'

    then paste /(\w+ (-?\d+ )+)/

    then type m then enter and keep hitting space to see the next step

    Here is what a step looks like

    Trying literal whitespace ('\N{SPACE}') | V /(\w+ (-?\d+ )+)/ | V 'red:27 yellow:102 green:311 yellow:12 blue:45' [Visual of regex at 'rxrx' line 0] [step: 7]

    So hopefully you can see what you're missing, its the thing you had in split

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-04-24 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found