sprkling has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I am a newbie to perl world. I am teaching myself by watching VTC perl funamentals video tutorial from our local online library. but it does not include workfiles for each "Now you try" section.
In the chapter 6 of "String and pattern match", it asks me to solve the following problem:
"Replace all instances of the numerals 0-9 in a string of text with their full word versions ('one', 'two'...). Don't worry about higher numbers ('eleven', 'thirty-two')".
So far I have learned loops, conditions, regular expression match, search and replace functions. I am thinking of use s///g but not sure how can replace each number with their word version by using s///g.
This is my first post on monks. please forgive me if I am not following exact posting rules.
Thanks

Replies are listed 'Best First'.
Re: Regular Expression: search and replace
by toolic (Bishop) on Oct 11, 2013 at 14:31 UTC
      Thanks. I ended up just using an array as the index stars from 0 to 9 for this simple problem.
Re: Regular Expression: search and replace
by davido (Cardinal) on Oct 11, 2013 at 15:26 UTC

    Does your library have the OReilly book, Learning Perl? I'm not familiar with the tutorial you mentioned, but I would guess in any room full of Perl programmers a minimum of 50% will say that book contributed to getting through their own early learning curve with Perl. At least that has been my experience when the topic has come up at various Perl Mongers meetings.


    Dave

Re: Regular Expression: search and replace
by kcott (Archbishop) on Oct 11, 2013 at 16:16 UTC

    G'day sprkling,

    Welcome to the monastery.

    While there would be many ways to solve this, given a chapter title of "String and pattern match", choosing s///g sounds like you're right on target.

    I see you subsequently replied with "I ended up just using an array ...". In essence, that also seems like a good move; however, without any code, it's impossible to know whether you've used that with a regular expression substitution or in some other way. The implementation of that idea might range from excellent to abysmal.

    In general, a post along the lines of "I'm learning - here's an exercise - here's my solution - comments welcome", will get you constructive criticism on your code. If you can't find a solution, or what you do code produces warnings or errors, you can still post that but ensure you include the full text of the messages. Also, keep your code short, to the point and written in such a way that we can run it (and, where necessary, reproduce any problems you're encountering). Here's an example of what you might have posted for this specific question:

    #!/usr/bin/env perl use strict; use warnings; my @number_names = qw{zero one two three four five six seven eight nin +e}; my $pattern = qr{ ( [0-9] ) }x; my @strings = ('8, 9, 5', '3-2-1-0', 'Testing: 1 2 3'); for my $string (@strings) { print "DIGITS: $string\n"; $string =~ s/$pattern/$number_names[$1]/g; print "NAMES: $string\n"; }

    Output:

    DIGITS: 8, 9, 5 NAMES: eight, nine, five DIGITS: 3-2-1-0 NAMES: three-two-one-zero DIGITS: Testing: 1 2 3 NAMES: Testing: one two three
    "This is my first post on monks. please forgive me if I am not following exact posting rules."

    Beyond the comments I've already made about not posting any code, this generally looks fine. Whenever you post, right below the textarea, you'll find useful tips and links to guidelines and further help. You can see these, at any time, by looking at the end of the Seekers of Perl Wisdom page.

    -- Ken

Re: Regular Expression: search and replace
by RichardK (Parson) on Oct 11, 2013 at 14:53 UTC

    Perl ships with lots of documentation accessed via perldoc or online, a good place to start is the regular expression tutorial perlretut.

Re: Regular Expression: search and replace
by yateeshMN (Initiate) on Oct 11, 2013 at 19:42 UTC

    This is my first answer after joining perlmonks. Answer is already posted, but here is perl one liner for doing same thing.

    'num' is a file containing numbers.

    $ cat num Hello 12345 world 23456 777381 89651

    perl one liner which you can execute in command line for your answer.

    $ perl -lpne 's/\d/qw(zero one two three four five six seven eight nin +e)[$&]/ge' num
    A minor correction. Same can be achieved through.
    $ perl -lpe 's/\d/qw(zero one two three four five six seven eight nine +)[$&]/ge'

    Once you finish regexes, you can have a look into perl command line options to know what magic is going on in above command.

        Thanks for correction. I will update.
Re: Regular Expression: search and replace
by Lennotoecom (Pilgrim) on Oct 11, 2013 at 15:08 UTC
    @numbers = qw(zero one two three four five six seven eight nine); ($data = <<End_Line) =~ s/\d/$numbers[$&]/g; Sarah has 1 apples, 5 plumes and 6 pineapple, and George has 2 apples, 4 plumes and 1 pineapple. Together they have 3 apples, 9 plumes and 7 pineapples. No one has 8 anything. End_Line print "$data";
Re: Regular Expression: search and replace
by sprkling (Initiate) on Oct 11, 2013 at 18:36 UTC

    Thank you everyone!!

    @kcott: Special thank you to you. I will keep your points in mind. My code is very similar to Lennotoecom's.