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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:16:11+00:00 2026-05-16T22:16:11+00:00

I have a file that contains parameters using this syntax RANGE {<value> | <value>-<value>}

  • 0

I have a file that contains parameters using this syntax

RANGE {<value> | <value>-<value>} [ , ...]

where value s are numbers.

for example, all these are valid syntax

RANGE 34
RANGE 45, 234
RANGE 2-99
RANGE 3-7, 15, 16, 2, 54

How can I parse the values to an array in Perl?

For example for the last example, I want my array to have 3, 4, 5, 6, 7, 15, 16, 2, 54. The ordering of elements does not matter.


The most basic way is to check for a - symbol to determine whether there is a range or not, parse the range using a loop and then parse the rest of the elements

my @arr;
my $fh, "<", "file.txt" or die (...);
while (<$fh>) {
    if ($_ =~ /RANGE/) {
        if ($_ =~ /-/) { # parse the range
            < how do I parse the lower and upper limits? >
            for($lower..$upper) {
                $arr[++$#arr] = $_;
            }
        } else { # parse the first value
            < how do I parse the first value? >
        }

        # parse the rest of the values after the comma
        < how do I parse the values after the comma? >
    }
}
  • I need help parsing the numbers. For parsing, one way I can think of is to use successive splits (on -, , and ). Is there some better (clean and elegant, using regex maybe?) way?

  • Also, comments/suggestions on the overall program structure are welcome.

  • 1 1 Answer
  • 1 View
  • 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-16T22:16:11+00:00Added an answer on May 16, 2026 at 10:16 pm

    I would suggest parsing the line into a separate variable, as $_ tends to get clobbered by other function calls. You can remove the trailing newline at the same time, with chomp.

    while (<$fh)>
    {
        chomp (my $line = $_);
        # ...
    }
    

    Next, you need to detect the ‘RANGE’ indicator, and extract the numbers that follow. If there is no such indicator, you can just skip to the next line:

    next if $line !~ /^RANGE (.*)$/;
    

    Now, you can start extracting the numbers, splitting on the comma delimiter:

    my @ranges = split /, /, $1;
    

    Now you can extract the dashes and translate those into ranges. This is the tricky part — if the value has a dash in it, get the first and second numbers, and turn them into a range with the .. operator; otherwise, leave the number alone:

    @ranges = map { /(\d+)-(\d+)/ ? ($1 .. $2) : $_ } @ranges;
    

    Putting all that together, and combining expressions, gives us:

    my @numbers;
    while (<$fh)>
    {
        chomp (my $line = $_);
        next if $line !~ /^RANGE (.*)$/;
    
        push @numbers, map { /(\d+)-(\d+)/ ? ($1 .. $2) : $_ } (split /, /, $1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a File that COntains Strings in This Format: ACHMU)][2:s,161,(ACH Payment Sys Menus
I have a file that contains this <!-- CordovaVersion --> I want to replace
I have a file that I set using PowerShell that contains the version number
I have a .txt file that contains a list of names and some numbers.
I have a string that contains multiple parameters delimited by #, like this :
I have a file that contains: foo1 = 1 foo2 = 2 foo3 =
I have a file that contains the following in a csv file; country,region,city,postalCode,metroCode,areaCode I
I have Text file that contains data separated with a comma , . How
I have a file that contains around thousands of lines. Format of every line
I have a PHP file that contains a large array (about 90KB). I'm considering

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.