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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:19:42+00:00 2026-05-23T11:19:42+00:00

I’m trying to parse out specific values from a text file, and output those

  • 0

I’m trying to parse out specific values from a text file, and output those to a different file.

I’m using regular expressions stored in a hash (matched up with their descriptive name) to search through a string (scalar), and then storing the discovered values in an array, which is then written out to a file.

I’ve got everything working, except for the searching/extracting part.
(I’ve only just learned Perl in the past couple days, so I wouldn’t be surprised if I was making some really simple mistakes.)

$inputstring = 'Lorem ipsum dolor Date: 20110131 quis semper egestas.';

%myregexhash = ( Date => '/([12][09][0-9][0-9][0-1][0-2][0-9][0-9])/' );

@foundvaluesarray=();

while ( ($thefieldname, $theregex) = each (%myregexhash))
{
    if ($inputstring =~ $theregex) 
    {
        push(@foundvaluesarray, "$thefieldname: $&\n");
        $inputstring = $';
    }
}

print "@foundvaluesarray";

The array fills up with the field names (“Date:”), but not the values I’m looking for (“20110131”).

Any idea what I’m doing wrong?

  • 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-23T11:19:43+00:00Added an answer on May 23, 2026 at 11:19 am

    Make one small change:

    %myregexhash = ( Date => qr/([12][09][0-9][0-9][0-1][0-2][0-9][0-9])/ );
    

    Note the use of qr//, which compiles a regex.

    You’re new, so I’d recommend a few other changes.

    Any non-trivial program should begin with the following front matter:

    #! /usr/bin/env perl
    
    use strict;
    use warnings;
    

    The strict pragma has nice benefits such as catching misspelled variable names at compile time and checking your use of references. The warnings pragma turns on extra warning diagnostics that can alert you to questionable cases in your code.

    Now must predeclare:

    my $inputstring = 'Lorem ipsum dolor Date: 20110131 quis semper egestas.';
    
    my %myregexhash = ( Date => qr/([12][09][0-9][0-9][0-1][0-2][0-9][0-9])/ );
    
    my @foundvaluesarray=();
    

    The = () is implied in an array or hash declaration, so you don’t see it in idiomatic Perl.

    You don’t want to use $& if you can help it because it slows down your entire program.

    WARNING: Once Perl sees that you need one of $&, $`, or $' anywhere in the program, it has to provide them for every pattern match. This may substantially slow your program. Perl uses the same mechanism to produce $1, $2, etc., so you also pay a price for each pattern that contains capturing parentheses. (To avoid this cost while retaining the grouping behaviour, use the extended regular expression (?: ... ) instead.) But if you never use $&, $` or $', then patterns without capturing parentheses will not be penalized. So avoid $&, $' , and $` if you can, but if you can’t (and some algorithms really appreciate them), once you’ve used them once, use them at will, because you’ve already paid the price. As of 5.005, $& is not so costly as the other two.

    Because you surrounded your pattern with parentheses, the substring that matched is captured in $1, so grab it from there.

    Also, the way you chopped off the front of $inputstring is much more naturally expressed in Perl with s///.

    while (my ($thefieldname, $theregex) = each (%myregexhash))
    {
        if ($inputstring =~ s/$theregex//) 
        {
            push(@foundvaluesarray, "$thefieldname: $1\n");
        }
    }
    
    print "@foundvaluesarray";
    

    Output:

    Date: 20110131
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
For some reason, after submitting a string like this Jack’s Spindle from a text
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
i want to parse a xhtml file and display in UITableView. what is the
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.