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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:45:01+00:00 2026-06-16T09:45:01+00:00

I need to extract multiple substrings at fixed positions from a line and the

  • 0

I need to extract multiple substrings at fixed positions from a line and the same time replace whitespaces at another position.

For example, I have a string ‘01234567890 ‘. I want to extract characters at positions 1,2,6,7,8 and the same time if position 12, 13 are whitespaces, I want to replace them with 0101. It is all position based.

What is the best way to achieve this using perl ?

I can use substr and string comparison and then concatenate them together, but the code looked rather chuncky….

  • 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-16T09:45:02+00:00Added an answer on June 16, 2026 at 9:45 am

    I would probably split (or: explode) the string into an array of single chars:

    my @chars = split //, $string; # // is special with split
    

    Now we can do array slices: extracting multiple arguments at once.

    use List::MoreUtils qw(all);
    
    if (all {/\s/} @chars[12, 13]) {
       @chars[12, 13] = (0, 1);
       my @extracted_chars = @chars[1, 2, 6..8];
       # do something with extracted data.
    }
    

    We can then turn the @chars back into a string like

    $string = join "", @chars;
    

    If you want to remove certain chars instead of extracting them, you would have to use slices inside a loop, an ugly undertaking.

    Complete sub with nice interface to do this kind of thing

    sub extract (%) {
       my ($at, $ws, $ref) = @{{@_}}{qw(at if_whitespace from)};
       $ws //= [];
       my @chars = split //, $$ref;
       if (all {/\s/} @chars[@$ws]) {
          @chars[@$ws] = (0, 1) x int(@$ws / 2 + 1);
          $$ref = join "", @chars;
          return @chars[@$at];
       }
       return +();
    }
    
    my $string = "0123456789ab \tef";
    my @extracted = extract from => \$string, at => [1,2,6..8], if_whitespace => [12, 13];
    
    say "@extracted";
    say $string;
    

    Output:

    1 2 6 7 8
    0123456789ab01ef
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help to extract multiple substrings from a string. An example of
I need to extract data from my MySQL database into multiple text files. I
I need to extract multiple matches for a string on a single line. The
I need to extract multiple text/dropdown list fields from an asp.net form and format
Need to extract .co.uk urls from a file with lots of entries, some .com
I need to extract a value from a hidden HTML field, have somewhat figured
I need to extract the URL's from the php datas, how can i achieve
I need to extract this text: Line 1 text. Line 2 text. Line 2
I need to extract the arguments from a function Say, Function_1(arg1,agr2,....argn). Is it possible
I need to extract images from a FB fan page. I'm able to read

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.