Re: regexp wizardry needed
by Kenosis (Priest) on Jan 25, 2013 at 17:11 UTC
|
use strict;
use warnings;
my $lines2 = " PERIOD 13 OCT 06 OCT";
my @date = map ucfirst lc, split ' ', $lines2;
print "$date[2] $date[3] and $date[2] $date[1]";
Output:
Oct 06 and Oct 13
Update: Removed an unnecessary map. Thanks, johngg! | [reply] [Watch: Dir/Any] [d/l] [select] |
|
$ perl -E '
> $line = q{PERIOD 13 OCT 06 OCT};
> say for map ucfirst lc, split q{ }, $line;'
Period
13
Oct
06
Oct
$
I hope this is of interest.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
I'm not sure why you are going with two maps...
I didn't think of it, so this is certainly of interest. Greatly appreciate you sharing this, JohnGG!
| [reply] [Watch: Dir/Any] |
Re: regexp wizardry needed
by LanX (Saint) on Jan 25, 2013 at 17:03 UTC
|
Please use <c> and <p>-tags and phrase a question.
You'll be surprised how fast you'll get useful answers! =)
see Writeup Formatting Tips
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: regexp wizardry needed
by Anonymous Monk on Jan 25, 2013 at 17:06 UTC
|
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
| [reply] [Watch: Dir/Any] [d/l] |
|
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
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
/r is only availabe in 5.14 I am using 5.10.
| [reply] [Watch: Dir/Any] |
|
|
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"
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: regexp wizardry needed
by AnomalousMonk (Archbishop) on Jan 26, 2013 at 05:31 UTC
|
>perl -wMstrict -le
"my $currentyear = '2013';
my $lines2 = ' PERIOD 13 OCT 06 OCT';
;;
$lines2 =~ s{ (\d\d) \s ([[:upper:]]{3} (?! \S)) }
{\u\L$2 $1, $currentyear}xmsg;
print qq{$lines2};
"
PERIOD Oct 13, 2013 Oct 06, 2013
| [reply] [Watch: Dir/Any] [d/l] |