Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: regexp wizardry needed

by Anonymous Monk
on Jan 25, 2013 at 17:06 UTC ( #1015373=note: print w/ replies, xml ) Need Help??


in reply to regexp wizardry needed

I added the correct HTML tags to the post to make it easier to get help. I am using Perl version 5.10

$lines2 = " PERIOD 13 OCT 06 OCT"; $lines2 =~ /\d\d \w\w\w/g; $lines2 =~ s/(\d\d) (\w\w\w)/"\u\L$2 " . ($1 + 0)/eg; $lines2 =~ /(\w\w\w \d\d) \s+ (\w\w\w \d\d)/; print "dates: $1 and $2 \n"; #this line only works with 5.14. #push (@date, map { s/(\d\d) (\w\w\w)/"\u\L$2 " . ($1 + 0)/er } $lines +2 =~ /\d\d \w\w\w/g); push (@date, $1); push (@date, $2); $date[0] = $date[0]. ", ". $currentyear; $date1 = $date1. ", ". $currentyear;

Output: dates: 06 and OCT
I need it to be dates: Oct 06 and Oct 13


Comment on Re: regexp wizardry needed
Download Code
Re^2: regexp wizardry needed
by Corion (Pope) on Jan 25, 2013 at 17:13 UTC

    perlop says that /r is for non-destructive operation of a regular expression. You can simply replace that by introducing a temporary variable:

    my $result = $example =~ s/foo/bar/r;

    ... can be replaced by

    (my $result=$example) =~ s/foo/bar/;

    In your case, you will have to make sure your map block actually returns the intended value:

    ... map { (my $result=$_) =~ s/foo/bar/; $result } @elements
      /r is only availabe in 5.14 I am using 5.10.

        Please read Corion's reply: "In your case, you will have to..."

Re^2: regexp wizardry needed
by LanX (Canon) on Jan 25, 2013 at 18:50 UTC
    Still no clear question, only a bunch of weird code! :(

    That's what you want?

    DB<159> @date = (" PERIOD 13 OCT 06 OCT" =~ m/ PERIOD (\d\d) (\w\w\w +) (\d\d) (\w\w\w)/) => (13, "OCT", "06", "OCT")

    If you only need the first letter of the month to be capitalized, try only matching the first letter and the rest, and process them accordingly with lc() or '\U' and '\L' in s///ubstitutions.

    UPDATE
    another way to do it, see ucfirst() (a command I've never used before =)...
    DB<164> ucfirst lc $date[1] => "Oct"

    Cheers Rolf

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2014-09-21 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (167 votes), past polls