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

[SOLVED]substitution using hash issue

by jaffinito34 (Acolyte)
on Nov 12, 2012 at 15:01 UTC ( #1003454=perlquestion: print w/ replies, xml ) Need Help??
jaffinito34 has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I'm writing a script that will simply take in a given date in the format of (DD/Mon/YYYY) where the month is entered as a 3 letters. I'm trying to convert the letters into its corresponding number using a hash but I'm having some difficulty and I know this is a simple fix but I can't seem to figure it out. Check out the code below.

#! /usr/bin/perl print "Enter date: "; chomp($date = <STDIN>); ($day,$month,$year) = split '/', $date; print "$day $month $year\n"; %dates = ( 'Jan' => 01,'Feb' => 02,'Mar' => 03,'Apr' => 04,'May' => 05,'Jun' +=> 06, 'Jul' => 07,'Aug' => 08,'Sep' => 09,'Oct' => 10,'Nov' => 11,'Dec' +=> 12, ); foreach $char ($month){ $char =~ s/.../$dates{$month}/; }

I keep getting compilation errors, not sure why though. Any help is appreciated.

Comment on [SOLVED]substitution using hash issue
Download Code
Re: substitution using hash issue
by Anonymous Monk on Nov 12, 2012 at 15:10 UTC

      When the script is run in the terminal, I get this.

      syntax error at ./dateWork line 13, near "+=>" Illegal octal digit '8' at ./dateWork line 14, at end of line Illegal octal digit '9' at ./dateWork line 14, at end of line Execution of ./dateWork aborted due to compilation errors.
Re: substitution using hash issue
by brap (Pilgrim) on Nov 12, 2012 at 15:16 UTC

    Hi jaffinito34,

    Looks like the month numbers are being interpreted as octal. Drop the leading zeroes, especially for 'Aug' and 'Sep'.

    'Jan' => 1, ...

    That will at least get your program running.

      Got compilation errors.

      syntax error at ./dateWork line 13, near "+=>" Execution of ./dateWork aborted due to compilation errors.

        Sorry it works, but when I try to print the updated $month, it just prints a blank line.

Re: substitution using hash issue
by choroba (Canon) on Nov 12, 2012 at 15:17 UTC
    No substitution is required, a hash is all you need:
    #!/usr/bin/perl use warnings; use strict; print "Enter date: "; chomp(my $date = <STDIN>); my ($day, $month, $year) = split '/', $date; print "$day $month $year\n"; my $m; my %dates = map { $_, sprintf '%02d', ++$m } qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/; my $month_n = $dates{$month}; print "$day $month_n $year\n";
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      The program seemed to run but got some errors after entering the date, not sure what they mean though.

      Enter date: 23 Oct 2222 Use of uninitialized value $month in concatenation (.) or string at ./ +dateWork line 9, <STDIN> line 1. Use of uninitialized value $year in concatenation (.) or string at ./d +ateWork line 9, <STDIN> line 1. 23 Oct 2222 Use of uninitialized value $month in hash element at ./dateWork line 1 +5, <STDIN> line 1. Use of uninitialized value $month_n in concatenation (.) or string at +./dateWork line 16, <STDIN> line 1. Use of uninitialized value $year in concatenation (.) or string at ./d +ateWork line 16, <STDIN> line 1. 23 Oct 2222
        You should enter the date in the specified format.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: substitution using hash issue
by brx (Pilgrim) on Nov 12, 2012 at 15:50 UTC

    1. use strict; This is a good friend.
    2. Declare your variables:  my %dates = ...
    3. 01 is an octal number (one). 08 is an illegal number representation. What you want is text, so write text with quotes: my %dates = ( 'Jan' => '01', #...
    4. $dates{'Dec'} contains "12". So, forget regex substitution: print "$day $dates{$month} $year\n";
    5. There is a lot of caveats with dates. Consider well known CPAN modules like DateTime...

    #!/usr/bin/perl use strict; my %dates = ( 'Jan' => '01', 'Feb' => '02', 'Mar' => '03', 'Apr' => '04', 'May' => '05', 'Jun' => '06', 'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' => '12', ); my $date = "30/Feb/2013"; my ($day,$month,$year) = split '/', $date; print "$day $dates{$month} $year\n";

    English is not my mother tongue.
    Les tongues de ma mère sont "made in France".

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2015-07-04 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls