Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

String manipulation

by Anonymous Monk
on Dec 15, 2012 at 02:43 UTC ( [id://1008925]=perlquestion: print w/replies, xml ) Need Help??

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

Hiya Monks,

I have 3 strings $a = "December 14th 2012"; , $b = "December 21st 2012"; , $c = "December 22nd 2012"; I want to change it to a common format, means I want to delete the 'st', 'nd' & 'th'.

Dear Monks, I am not a seasoned Perl Programmer, so am asking your wisdom. Be Kind Monks.

Regards,

Whitefield.

Replies are listed 'Best First'.
Re: String manipulation
by toolic (Bishop) on Dec 15, 2012 at 03:28 UTC
    Another way, borrowing Athanasius' alternation:
    use warnings; use strict; while (<DATA>) { s/(\d+)(th|st|nd|rd)/$1/; print; } __DATA__ December 14th 2012 December 21st 2012 December 22nd 2012

    See also:

      Following will solve December 23rd 2012 to December 23 2012. It will work for all 1st, 2nd and 3rd to 1, 2, 3 respectively.
      use warnings; use strict; while (<DATA>) { s/(\d+)([a-z]{2})/$1/; print; } __DATA__ December 14th 2012 December 21st 2012 December 22nd 2012 December 23rd 2012
Re: String manipulation
by Athanasius (Archbishop) on Dec 15, 2012 at 03:01 UTC

    Here is one way:

    #! perl use Modern::Perl; my $a = "December 14th 2012"; my $b = "December 21st 2012"; my $c = "December 22nd 2012"; for ($a, $b, $c) { my @fields = split; $fields[1] =~ s/st|nd|rd|th//; $_ = join(' ', @fields); say; }

    Output:

    12:58 >perl 433_SoPW.pl December 14 2012 December 21 2012 December 22 2012 12:58 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: String manipulation
by CountZero (Bishop) on Dec 15, 2012 at 13:02 UTC
    Assuming this is homework and you really want to surprise your teacher:
    use Modern::Perl; use Date::Parse; use DateTime; while (<DATA>) { my $dt = DateTime->from_epoch( epoch => str2time($_, '+0000')); say join ' ', $dt->month_name(), $dt->day(), $dt->year(); } __DATA__ December 14th 2012 December 21st 2012 December 22nd 2012
    Output:
    December 14 2012 December 21 2012 December 22 2012

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      Treating the data as a date rather than a generic string can have a huge advantage in avoiding unwanted changes.
      Bill
Re: String manipulation
by LanX (Saint) on Dec 15, 2012 at 13:54 UTC
    lets golf! =)
    DB<101> s/(\d)\w\w /$1 / for @a=("December 14th 2012", "December 21s +t 2012" , "December 22nd 2012") => "" DB<102> @a => ("December 14 2012", "December 21 2012", "December 22 2012")

    UPDATE:

    a bit more fail safe!

    DB<126> map {/^(\w+ \d\d?)\w\w( \d\d\d\d)$/; "$1$2"} "December 1th 2 +012", "December 21st 2012" , "December 22nd 2012" => ("December 1 2012", "December 21 2012", "December 22 2012")

    Cheers Rolf

Re: String manipulation
by AnomalousMonk (Archbishop) on Dec 15, 2012 at 13:42 UTC

    What the heck. Another approach:

    >perl -wMstrict -le "my @dates = ( 'December 1st 2012', 'December 2nd 2012', 'December 23rd 2012', 'December 24th 2012', 'December 765th 2012', 'an 8th note', ); ;; for my $date (@dates) { printf qq{'$date' -> }; $date =~ s{ \d \K (?: st | nd | rd | th) \b }{}xms; print qq{'$date'} } " 'December 1st 2012' -> 'December 1 2012' 'December 2nd 2012' -> 'December 2 2012' 'December 23rd 2012' -> 'December 23 2012' 'December 24th 2012' -> 'December 24 2012' 'December 765th 2012' -> 'December 765 2012' 'an 8th note' -> 'an 8 note'
Re: String manipulation
by Anonymous Monk on Dec 15, 2012 at 04:22 UTC
    Homework?
Re: String manipulation
by Anonymous Monk on Dec 15, 2012 at 04:37 UTC

Log In?
Username:
Password:

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

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

    No recent polls found