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 227595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:36:42+00:00 2026-05-11T19:36:42+00:00

I was wondering if anyone had any suggestions on improving the following code (if

  • 0

I was wondering if anyone had any suggestions on improving the following code (if possible) so that it didn’t need the repeated (my @a = $time =~ …), possibly using case/switch or given/when or some other idea that i’m missing?

my $time = '12:59pm';

if( my @a = $time =~ m/^(\d\d?)(am|pm)$/ )        { tell_time( $a[0], 0, $a[1] ) }
if( my @a = $time =~ m/^(\d\d?):(\d\d)(am|pm)$/ ) { tell_time( @a ) }
if( my @a = $time =~ m/^(\d\d?):(\d\d)$/ )        { tell_time( @a ) }

sub tell_time
{
    my $hour    = shift;
    my $minute  = shift || '00';
    my $ampm    = shift || ( $hour > 12 ) ? 'pm' : 'am';

    print "Hour: $hour, Minute: $minute, AMPM: $ampm\n";
}

I’ve tried playing around with Switch and the 5.10 given/when but can’t seem to be able to do something like:

given( $time )
{
    when( /^(\d\d?)(am|pm)$/ )        { tell_time( $_[0], 0, $_[1] ) }
    when( /^(\d\d?):(\d\d)(am|pm)$/ ) { tell_time( @_ ) }
    when( /^(\d\d?):(\d\d)$/ )        { tell_time( @_ ) }
}

That doesn’t fly because @_ appears to be storing $time.

also note I’m more interested in the syntax of the problem than the problem the code solves. I’m well aware that I could use Time::ParseDate to figure out the various parts of a string formatted like a time or date.

  • 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-11T19:36:42+00:00Added an answer on May 11, 2026 at 7:36 pm

    Your regex uses () to extract matches, but you don’t have to store these in an array. If you want, they’re stored in $1, $2, $3, and so on. Lookie:

    given( $time )
    {
        when( /^(\d\d?)(am|pm)$/ )        { tell_time( $1, 0, $2 ) }
        when( /^(\d\d?):(\d\d)(am|pm)$/ ) { tell_time( $1, $2, $3 ) }
        when( /^(\d\d?):(\d\d)$/ )        { tell_time( $1, $2 ) }
    }
    

    Does exactly what I think you want to do.

    If you want to add to the syntax, I would write tell_time() to simply take the time as a string, and have the function parse the result itself, rather than make the user of your code parse it himself. Alternatively, you could use this given() block as the start of a new function that does exactly that – parses a time string and passes it correctly to tell_time(). But that’s just me. I don’t know what you need your code to do, so by all means go for it.

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

Sidebar

Related Questions

I'm wondering if anyone has had any experience with the API that gives you
I was wondering if anyone had any suggestions for minimizing a function, f(x,y), where
Hey guys! Was wondering is anyone had any suggestions on books or other resources
I was wondering if anyone had any luck with the following senario in flex.
I was wondering if anyone had any suggestions on how to throuougly test the
Being relatively new to the .net game, I was wondering, has anyone had any
I was wondering if anyone had some resources that describe the binary protocol used
Was wondering if anyone had any favourite methods/ useful libraries for processing a tab-delimited
I'm wondering if anyone has had any experience modifying print settings via a web
I was wondering if anyone has had any luck getting a DynamicObject to serialize

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.