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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:11:13+00:00 2026-05-12T14:11:13+00:00

I have a DOS command which outputs as follows (just an example containing 3

  • 0

I have a DOS command which outputs as follows (just an example containing 3 results):

The Scheme GUID: 123-abc (Scheme1) *

The Scheme GUID: 456-def (Scheme2) 

The Scheme GUID: 789-ghi (Scheme3) 

I am invoking the command line program from a Perl script and I want to store two results in a structure:

**123-abc** (alphanumeric value) & 
**Scheme1**(name of the scheme)
*(values obtained from the results mentioned above in the eg)* 
  1. I want to know how to store the above 3 results (the alphanumeric value and Scheme name) and put in an array of 3 structures.

  2. I need to get the Scheme which is starred (as shown above Scheme1 is starred) and assign it to a global variable.

  • 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-05-12T14:11:14+00:00Added an answer on May 12, 2026 at 2:11 pm

    This sounds like a job for a regex and an array of hashes.

    First, let’s create a pattern that can find the information. You are looking for a constant string "The Scheme GUID: " that is followed by a contiguous string of alpha-numeric and hyphen characters followed by a space and then a contiguous string of alpha-numeric characters surrounded by parentheses. In regex, this is /The Scheme GUID: [a-zA-Z0-9-]+ \([a-zA-Z0-9]+\)/. Now, that will only match the string, and we want to pull out pieces of it, so we need to add captures to the regex and catch its return:

    my ($guid, $scheme) = /The Scheme GUID: ([a-zA-Z0-9-]+) \(([a-zA-Z0-9]+)\)/;
    

    The () are used to denote the parts we want to save from the string and are called captures.

    Now that we have the values, you want to create a record-like structure. In Perl, you commonly use a hash for this purpose:

    my %record = (
        guid   => $guid,
        scheme => $scheme
    );
    

    You can now access the guid by saying $record{guid}. To build an array of these records, just push the record onto an array:

    my @records;
    while (<>) {
        my ($guid, $scheme) = /The Scheme GUID: ([a-zA-Z0-9-]+) \(([a-zA-Z0-9])\)/;
        my %record = (
            guid   => $guid,
            scheme => $scheme
        );
        push @records, \%record;
    }
    

    You can now access the third record’s scheme like this: $records[2]{scheme}.

    Your last requirement requires a change to the regex. You need to look for that star and do somehthing special if you see it. Unfortunately star means something to regexes, so you will need to escape it like you did with parentheses. And the star is not always present, so you will need to use non-grouping parentheses (?:) and the ? quantifier to tell the regex that not matching that part of the string is okay:

    my ($guid, $scheme, $star) = /The Scheme GUID: ([a-zA-Z0-9-]+) \(([a-zA-Z0-9]+)\)(?: (\*))?/;
    

    The regex has gotten very long and hard to read at this point, so it is probably a good idea to use the /x flag and add some whitespace and comments to the regex:

    my ($guid, $scheme, $star) = m{
        The [ ] Scheme [ ] GUID: 
        ([a-zA-Z0-9-]+)          #capture the guid
        [ ]
        \(  ([a-zA-Z0-9]+)  \)  #capture the scheme 
        (?:
            [ ]
            (\*)                #capture the star if it exists
        )?
    }x;
    

    They way I would write a program like this is:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $primary_record;
    my @records;
    while (<DATA>) {
        next unless my ($guid, $scheme, $star) = m{
            The [ ] Scheme [ ] GUID: [ ]
            ([a-zA-Z0-9-]+)          #capture the guid
            [ ]
            \(  ([a-zA-Z0-9]+)  \)   #capture the scheme 
            (?:
                [ ]
                ([*])                #capture the star if it exists
            )?
        }x;
        my %record = (
            guid    => $guid,
            scheme  => $scheme,
            starred => defined $star ? 1 : 0
        );
    
        if ($record{starred}) {
            $primary_record = \%record;
        }
    
        push @records, \%record;
    }
    
    print "records:\n";
    for my $record (@records) {
        print "\tguid: $record->{guid} scheme: $record->{scheme}\n";
    }
    print "primary record is $primary_record->{guid}\n";
    
    __DATA__
    The Scheme GUID: 123-abc (Scheme1) *
    The Scheme GUID: 456-def (Scheme2) 
    The Scheme GUID: 789-ghi (Scheme3) 
    

    If you have the data in an array, for you can replace the while loop with a for loop:

    for my $line (@lines) {
        next unless my ($guid, $scheme, $star) = $line =~ m{
            The [ ] Scheme [ ] GUID: [ ]
            ([a-zA-Z0-9-]+)          #capture the guid
            [ ]
            \(  ([a-zA-Z0-9]+)  \)   #capture the scheme 
            (?:
                [ ]
                ([*])                #capture the star if it exists
            )?
        }x;
    

    The next unless match idiom says that to get a different line if this one doesn’t match the regex. The m{regex} is the generalized form of /regex/. I tend to use the generalized form when I stretch a regex across multiple lines because it makes matching the beginning and ending of the regex easier in my editor.

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

Sidebar

Ask A Question

Stats

  • Questions 368k
  • Answers 368k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer =C2=A0 represents the bytes C2 A0. Since this is UTF-8,… May 14, 2026 at 6:09 pm
  • Editorial Team
    Editorial Team added an answer You can use <rich:suggestionBox> and define the custom autocomplete algorithm.… May 14, 2026 at 6:09 pm
  • Editorial Team
    Editorial Team added an answer As Damien already mentioned, if the only thing you'd like… May 14, 2026 at 6:09 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.