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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:09:26+00:00 2026-06-05T08:09:26+00:00

There is a similarly worded question, but I think this is slightly different. Basically,

  • 0

There is a similarly worded question, but I think this is slightly different.

Basically, say I have this string:

“aa{bb{dccd“

Here I would like to split the string at the last brace {; and have the parts returned as an array. I can easily find the position (0-based index) of this character using rindex:

perl -e '
$aa="aa{bb{dccd" ;
$ri = rindex($aa, "{") ;
print "$ri\n"; '

5

… and given that I’m not a Perl coder, first thing I think of is to use something like $str = split($aa, 3). Unfortunately, that is not correct syntax – split takes a regex as first argument (what to match for), and string as second – and it doesn’t take an integer position index as argument.

I found posts like Perl Guru Forums: Perl Programming Help: Intermediate: split or splice string on char count?, which recommend using substr in a similar context; however, I’d have to write two substrs to populate the list as per the example above, and so I’d rather hear about alternatives to substr.

Basically, if the problem of matching the position of N-th character can be expressed as a regex match, the split could work just as well – so that would be my primary question. However, I’d also be interested in hearing if there are Perl built-in functions that could accept a list/array of integers specifying character positions, and return an array containing the split sections.

EDIT:

To summarize the above – I’d like to have the character indexes, because I’d like to print them out for debugging; and at the same time, use them for splitting a string into array – but without using substrs.

EDIT2: I just realized that I left something out from the OP — and that is, that in the problem that I’m working on, I have to first retrieve character indexes (by rindex or otherwise); then I have to do calculations on them (so they may increase, or decrease) – and only then am I supposed to split the string (based on the new index values). It may have been that my original example was too simple, and didn’t express this focus on indexes/character positions much (and not to mention that my first thought of split implies character indexes anyways – but I really cannot remember which programming language it came from :))

  • 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-05T08:09:27+00:00Added an answer on June 5, 2026 at 8:09 am

    You wrote:

    I’d also be interested in hearing if there are Perl built-in functions that could accept a list/array of integers specifying character positions, and return an array containing the split sections.

    To create a function that takes a list of offsets and produces a list of substrings with those split positions, convert the offsets to lengths and pass these as an argument to unpack.

    There’s a &cut2fmt function in Chapter 1 of the Perl Cookbook that does this very thing. Here is an excerpt, reproduced here by kind permission of the author:

    Sometimes you prefer to think of your data as being cut up at specific columns. For example, you might want to place
    cuts right before positions 8, 14, 20, 26, and 30. Those are the column numbers where each field begins. Although you could calculate that the proper unpack format is "A7 A6 A6 A6 A4 A*", this is too much mental strain for the virtuously lazy Perl programmer. Let Perl figure it out for you. Use the cut2fmt function below:

    sub cut2fmt {
          my(@positions) = @_;
          my $template   = '';
          my $lastpos    = 1;
          foreach $place (@positions) {
              $template .= "A" . ($place - $lastpos) . " ";
              $lastpos   = $place;
          }
          $template .= "A*";
          return $template;
      }
    
      $fmt = cut2fmt(8, 14, 20, 26, 30);
      print "$fmt\n";
    
      A7 A6 A6 A6 A4 A*
    

    So the way you would use that is like this:

    $fmt = cut2fmt(8, 14, 20, 26, 30);
    @list = unpack($fmt, $string);
    

    or directly as

    @list = unpack(cut2fmt(8, 14, 20, 26, 30), $string);
    

    I believe this is what you were asking for.

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

Sidebar

Related Questions

I have asked a similar question to this one already but I think it
There are similar questions to this, but I don't think anyone has asked this
OK, this may sound weird, but here goes. There are 2 computers, A (Pentium
I know there is plenty of question answered over here https://stackoverflow.com/questions/tagged/youtube+regex , but not
Pardon me if there's a similar question somewhere on here already, but I couldn't
I have asked a similar question to this but for GMail and I was
My apologies if my question is not well worded but I seem to have
This one's been tricky to quantify, so I may not have this question worded
This may seem like a basic/stupid/obviously-answered question, but I wanted to check: why use
I asked a similar question earlier but I'll ask it again in a different

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.