Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6723417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:33:58+00:00 2026-05-26T09:33:58+00:00

I am trying to code a Perl Script which will take the date in

  • 0

I am trying to code a Perl Script which will take the date in Pattern, October 24, 2011 and convert this to 10,24,2011.

In order to do this I have prepared a Hash which will have the Month Name as a Key and a Numerical value representing Month’s position as a Value.

I will read the input string, use a regular expression to extract the month name from above format.

Replace this month name with a value which corresponds to the month as a key.

Here’s the script I have coded so far, but it’s not working for me.

@dates array will have every element in this format -> October 24, 2011.

%days=("January",01,"February",02,"March",03,"April",04,"May",05,"June",06,"July",07,"August",08,"September",09,"October",10,"November",11,"December",12);

@output = map{
$pattern=$_;
$pattern =~ s/(.*)\s/$days{$1};
} @dates;

foreach $output (@output)
{
print $output."\n";
}

Here’s a little explanation of what I am trying to do with this code.

@output will have the new formatted array with the Month Name replaced by the corresponding Numerical representing it as defined in the Hash.

map function is used to transform the elements of the array on the fly.

a sequence of characters followed by space is the regular expression used to extract the Month Name from pattern, October 24, 2011.

This will be referenced by $1.

I look up the corresponding value for $1 in the hash using, $days{$1}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:33:59+00:00Added an answer on May 26, 2026 at 9:33 am

    I see a few problems here. The first is that there is no use strict;.

    A number with a leading zero is assumed to be in octal format (i.e. base 8) so 08 is invalid. You want one of these:

    %days = ("January",     1,  "February",     2,  ...
    %days = ("January",   "01", "February",   "02", ...
    %days = ("January" =>   1,  "February" =>   2,  ...
    %days = ("January" => "01", "February" => "02", ...
    

    You should also be declaring your variables with my:

    my %days = ...
    my @output = ...
    

    You’re missing the final slash on your substitution, you probably want a comma in there to match your desired output format, and .* will eat up more than you want:

    $pattern =~ s/(\S*)\s/$days{$1}, /;
    

    The block for your map needs to return the value you want in @output but it currently returns 1 (see perldoc perlop to learn why); something like this will serve you better:

    my @output = map {
        my $pattern=$_; # You don't need this, operating on $_ is fine here
        $pattern =~ s/(\S*)\s/$days{$1}, /;
        $pattern
    } @dates;
    

    If you really want the spaces removed from the output, then this should do the trick:

    my @output = map {
        my $pattern=$_; # You don't need this, operating on $_ is fine here
        $pattern =~ s/(\S*)\s/$days{$1}, /;
        $pattern =~ s/\s//g;
        $pattern
    } @dates;
    

    There are more compact ways to do this map but I don’t want to change too much and confuse you.

    And, as mentioned in the comments, you might want to save yourself some trouble and have a look at DateTime and related packages.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to code a Perl script to substitute some text on all
I have some text files which I am trying to transform with a Perl
I'm trying to execute a script from within my java code which looks like:
How can I convert this Perl code to Groovy? How to bypass confirmation prompts
I'm trying to write a perl script which takes the output of colorgcc (or
I'm trying to write a perl script that determines which users are currently logged
I am trying to have my perl script get an Xxml file from online
I'm trying to develop a script which will, in part, generate a list of
I have a Perl script, the relevant bits of which are posted below. #
Have some Perl code which is using the DBI module - (the code is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.