Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Substring Syntax Check

by drodinthe559 (Monk)
on Sep 26, 2008 at 22:14 UTC ( [id://713956]=perlquestion: print w/replies, xml ) Need Help??

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

Here's a date I'm having the user enter in to get the month and the year, so I can create a folder with the month name and year. I wasn't sure how to handle the splitting of the date. I tried using split, but it didn't like the / as a delimitter. Any input would be appreciated. Thanks, Dave:)
print "Enter EOM date (MM/DD/YY)?"; chomp(my $eomdate = <STDIN>); my ($month,$year) = (substr($eomdate,0,2), substr($eomdate,-2)); my $month_t = (qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/)[$m +onth -1]; print $month_t . " " . $year . "\n";

Replies are listed 'Best First'.
Re: Substring Syntax Check
by JavaFan (Canon) on Sep 26, 2008 at 22:38 UTC
    I tried using split, but it didn't like the / as a delimitter.

    I've no problem using / to split:

    $ perl -wE '$_ = "12/13/14"; @x = split "/"; say for @x' 12 13 14 $
      That worked. Thank you.
Re: Substring Syntax Check
by dragonchild (Archbishop) on Sep 27, 2008 at 01:44 UTC
    When parsing user input, I first look for a parser that someone else wrote, look at writing my own parser, and then I reach for regexes. I would never use split to handle user input.

    Luckily, dates and times are something that have a lot of work put into them already. DateTime is always what I first reach for, but Date::Calc has several parsing routines that would work perfectly here. Date::Manip is the other date module on CPAN that is pretty good.


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Substring Syntax Check
by ikegami (Patriarch) on Sep 26, 2008 at 22:19 UTC

    Here's a date I'm having the user enter in to get the month and the year, so I can create a folder with the month name and year.

    Is it the current month and year?

    my ($y,$m) = (localtime)[5,4]; $m+=1; $y+=1900;

    I tried using split, but it didn't like the / as a delimitter.

    split first argument is a regular expression. / has a special meaning in regular expressions, so you need to escape it.
    split /\//

    Update: Said | where I meant /. Fixed.

      / has a special meaning in regular expressions

      No, it doesn't.

      $ perl -le' $_ = "one/two/three/four"; print m,/, ? "true" : "false"; $re = "/"; print $_ =~ $re ? "true" : "false"; ' true true

      Only ?, *, +, [, {, (, ), | and . have special meaning in a regular expression.

        ack, the post originally said | was special, cause I thought the OP said |. I fixed the post rather blindly. True, / is only special if / is the delimiter.
      Unfortunely, it isn't the the current month. I always have to take the last month to create the directory. For example, if I had to run it today, it would make a folder in the users directory labeled Aug08. However, I could take the current month and subtract one from it, but I'm thinking the results may potentially be incaccurate when I have to run this in January. It seems like your helping me again. I appreciate your help. I'll give the split a shot. Dave
        the results may potentially be incaccurate when I have to run this in January
        I agree. So, your choice is to add code to handle the corner case for January, or to use one of the CPAN modules that can probably do this cleanly for you. It looks like you can subtract a month using the Add_Delta_YM function from Date::Calc.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-25 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found