Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Regular expression

by Rexcel (Initiate)
on Jun 05, 2006 at 06:35 UTC ( #553539=perlquestion: print w/replies, xml ) Need Help??

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

Hi... Can anyone help me with my problem...
Im trying to write a code and im wondering if there is a way to make my code more shorter...

my $maxdate = "2006-06-06"; my $m_year = $maxdate; my $m_month = $maxdate; $m_year =~ s/(^.*)\-.*\-.*/$1/; $m_month =~ s/^.*\-(.*)\-.*/$1/;
thanks alot!

Edited by planetscape - added code tags

( keep:0 edit:11 reap:0 )

Replies are listed 'Best First'.
Re: Regular expression
by McDarren (Abbot) on Jun 05, 2006 at 06:44 UTC
    It would help if you said exactly what it is that you are trying to do. But I guess that you are trying to split a date into its components using a regular expression.

    Well, you can capture (and assign) multiple parts of your expression in one go. So something like this:

    my ($year, $month, $day) = $maxdate =~ m/(\d+)-(\d+)-(\d+)/;

    Of course, with your sample you could do the same thing using split, eg:

    my ($year, $month, $day) = split /-/, $maxdate;

    But if you want something more robust and reliable, that can deal with different date formats - then you should look at one of the Date parsing modules available on CPAN.

    Darren :)

Re: Regular expression
by davido (Cardinal) on Jun 05, 2006 at 06:45 UTC

    How about this?

    my $maxdate = "2006-06-06"; my( $m_year, $m_month ) = split /-/, $maxdate;

    Sometimes it's about using the right tool for the task. split comes in handy.


Re: Regular expression
by GrandFather (Saint) on Jun 05, 2006 at 06:55 UTC

    Things clean up somwhat when you change the substitute to a match and know that the match returns the list of captured strings in list context:

    use strict; use warnings; my $maxdate = "2006-06-05"; my ($m_year, $m_month, $d_day) = $maxdate =~ m/(^\d+)\-(\d+)-(\d+)/; print "year: $m_year, Month: $m_month, Day: $d_day\n";


    year: 2006, Month: 06, Day: 05

    DWIM is Perl's answer to Gödel
Re: Regular expression
by ioannis (Abbot) on Jun 05, 2006 at 08:28 UTC
    Assuming the date is in fixed-length format, you could also use unpack():
    my $maxdate = "2006-06-06"; my ($m_year, $m_month) = unpack 'A4 x A2', $maxdate;
Re: Regular expression
by ambrus (Abbot) on Jun 05, 2006 at 09:43 UTC

    If you just want to get the year and month part of the date, try this:

    use Date::Manip; my $maxdate = "2006-05-11"; my($m_year, $m_month) = UnixDate($maxdate, "%Y", "%m");

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2023-06-05 16:26 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (25 votes). Check out past polls.