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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:02:20+00:00 2026-06-13T03:02:20+00:00

I have a string, say: 5 3*3 4*1 6*3 14*4 3*2 2*3 4 I

  • 0

I have a string, say: “5 3*3 4*1 6*3 14*4 3*2 2*3 4”

I want this string to become an array with elements:

@array = ( “5”, “3” x 3, “1” x 4, “3” x 6, “4” x 14, “2” x 3, “3” x 2, “4” );

I’m guessing there’s a better (shorter) way than this:

$string = "5 3*3 4*1 6*3 14*4 3*2 2*3 4";
@array = split (/\s+/,$string);
foreach(@array) {
   if ( /\*/ ) {
      s/^(\d+)\*(\d+)/"$2" x $1/g;
   } else {
      print "$_\n";
      $_ = '"'. $_ .'"';
   }
}

EDIT: This code actually does what I say it should do above in the original question. But it’s not actually what I want. I want the final array to look like:

@array = (5, 3, 3, 3, …)

  • 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-13T03:02:22+00:00Added an answer on June 13, 2026 at 3:02 am

    Algorithm:

    1. Take the string and split it at spaces
    2. If the /(\d+)*(\d+)/ regex matches an element, we do the list element repetition. Else, we pass the element through unchanged.

    Implementation (oneliner)

    perl -MData::Dumper -E'$string = "5 3*3 4*1 6*3 14*4 3*2 2*3 4"; @array = map{/(\d+)\*(\d+)/ ? ($2)x$1 : $_} split /\s+/, $string; say Dumper \@array'
    

    Implementation (normal)

    my $string = "5 3*3 4*1 6*3 14*4 3*2 2*3 4";
    my @array = map {/(\d+)\*(\d+)/ ? ($2)x$1 : $_} split /\s+/, $string;
    # do something with @array:
    say "@array";
    

    Do note that the repetition operator used on strings repeats and concatenates the strings:

    "a"x3 ~~ "aaa"
    

    But used on a list, it returns a list with repeated elements:

    [(1)x3] ~~ [1,1,1]
    [(1,2)x2] ~~ [1,2,1,2]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have the string 1,2,3,4,5 and I want to convert this to an
Let's say I have this string: 3+5+6+2^5+9+1+2+32^2+10^10 I want to replace all occurrences of
Say you have this string: ABCDEFGH And you want to reverse it so that
Say, I have a string hello is it me you're looking for I want
I have a HashSet<string> DirectoryHolding = new HashSet<string>(); How do I say I want
I have a string say string s =C:\\Data , I have an array which
I have a String variable say strHTML = <div class='abc'> This is a test
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I have a string like this: String test = hfikoebndolahsdHEL123LOkjahhsdqhuihs; And then
Let's say I have a string This $0 is $2 $1. Still, \$$3 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.