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

  • Home
  • SEARCH
  • 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 9257089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:07:03+00:00 2026-06-18T12:07:03+00:00

I am a perl noob still. I am getting a string which can be

  • 0

I am a perl noob still. I am getting a string which can be man_1, man_2, woman1, woman2 etc.. (no commas and there will be only one string as input to the function).

I need to check man_ or woman as substring in if statements to make sure appropriate number is extracted and some offset is added.

the number i can extract as below

$num =~ s/\D//g
if (<need the substring extracted> == "man_")
    $offset = 100;
else if (<need the substring extracted> == "woman")
    $offset = 10;

return $num + $offset;

Now how do I extract the substring. I looked at substr and it requires offsets and what not. Couldnt figure out. Appreciate the help

  • 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-06-18T12:07:04+00:00Added an answer on June 18, 2026 at 12:07 pm

    Solution:

    if ( $num =~ m{^man_(\d+)$} ) {
        return 100 + $1;
    } elsif ( $num =~ m{^woman(\d+)$} ) {
        return 10 + $1;
    } else {
        die "Bad input: $num\n";
    }
    

    In your example there is couple of problems:

    1. s/\D//g – will remove the characters, but one-by-one, and not as big block of all \D characters. So there is no variable that would be “man_”
    2. to get data from regexp, you should use grouping parens, like s/(\D)//
    3. to get all characters, you should use * or + operators, like: s/(\D+)//
    4. Its still better to match without modifying, as it gives you better handling of edge cases of malformed data.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Perl usually converts numeric to string values and vice versa transparently. Yet there must
I'm a bit of a noob with Perl, and can't to see what's wrong
I am a noob Perl user trying to get my work done ASAP so
Perl is really good for writing the kind of string/file parsing programs that I
Using Perl XML::Twig, how can I loop on each sibling until reaching the last
Perl is continuing to surprise me. I have a code which takes an input
Perl 5.14 gives us the extended each function which operates on arrays as well
In Perl, what does this error mean? Unresolved symbol: Perl_Gthr_key_ptr I am getting this
In perl, if I have a string $str and a pattern saved in $pat
Perl: $string =~ s/[áàâã]/a/gi; #This line always prepends an a $string =~ s/[éèêë]/e/gi; $string

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.