Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^4: A Beginner Needs Homework help

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


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

I can't begin to express how much I appreciate your response. Thank you so much, that was SUPER helpful. When you said

"So do you think the program should stop or keep going if the file can't be opened? Maybe try open second ARGument ?"

did you mean an "while" loop or a if/or statement? or another ARGV?

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

    did you mean an "while" loop or a if/or statement? or another ARGV?

    I meant in english :)

    See print, see return, see exit, see die

    Main( @ARGV ); exit( 0 ); sub Main { for my $file ( @_ ){ TryAndDoStuffOrDieAndEndProgram( $file ); } } sub TryAndDoStuffOrDieAndEndProgram { open ... or die "Can't open ($file): $! "; ... close ... return; }

    There are other moves than AndEndProgram, like move on to next file, see die/eval

      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)

        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://1081893]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 17:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found