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

The Archive Base Latest Questions

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

I am new to Perl and have a question about the syntax. I received

  • 0

I am new to Perl and have a question about the syntax. I received this code for parsing a file containing specific information. I was wondering what the if (/DID/) part of the subroutine get_number is doing? Is this leveraging regular expressions? I’m not quite sure because regular-expression matches look like $_ =~ /some expression/. Finally, is the while loop in the get_number subroutine necessary?

#!/usr/bin/env perl

use Scalar::Util qw/ looks_like_number /;
use WWW::Mechanize;

# store the name of all the OCR file names in an array
my @file_list=qw{
   blah.txt
};

# set the scalar index to zero
my $file_index=0;

# open the file titled 'outputfile.txt' and write to it
# (or indicate that the file can't be opened)
open(OUT_FILE, '>', 'outputfile.txt')
    or die "Can't open output file\n";

while($file_index < 1){
    # open the OCR file and store it in the filehandle IN_FILE
    open(IN_FILE, '<', "$file_list[$file_index]")
        or die "Can't read source file!\n";

    print "Processing file $file_list[$file_index]\n";
    while(<IN_FILE>){
            my $citing_pat=get_number();
            get_country($citing_pat);
    }
    $file_index=$file_index+1;
}
close IN_FILE;
close OUT_FILE;

The definition of get_number is below.

sub get_number {
    while(<IN_FILE>){
        if(/DID/){
            my @fields=split / /;
            chomp($fields[3]);
            if($fields[3] !~ /\D/){
                return $fields[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-05-23T11:35:48+00:00Added an answer on May 23, 2026 at 11:35 am

    In that case, if (/DID/) by default searches the $_ variable, so it is correct. However, it is a rather loose regex, IMO.

    The while loop in the sub may be necessary, it depends on what your input looks like. You should be aware that the two while loops will cause some lines to get completely skipped.

    The while loop in the main program will take one line, and do nothing with it. Basically, this means that the first line in the file, and every line directly following a matching line (e.g. a line that contains “DID” and the 4th field is a number), will also be discarded.

    In order to answer that question properly, we’d need to see the input file.

    There are a number of issues with this code, and if it works as intended, it’s probably due to a healthy amount of luck.

    Below is a cleaned up version of the code. I kept the modules in, since I do not know if they are used elsewhere. I also kept the output file, since it might be used somewhere you have not shown. This code will not attempt to use undefined values for get_country, and will simply do nothing if it does not find a suitable number.

    use warnings;
    use strict;
    use Scalar::Util qw/ looks_like_number /;
    use WWW::Mechanize;
    
    my @file_list=qw{ blah.txt };
    
    open(my $outfile, '>', 'outputfile.txt') or die "Can't open output file: $!";
    
    for my $file (@file_list) {
        open(my $in_file, '<', $file) or die "Can't read source file: $!";
        print "Processing file $file\n";
        while (my $citing_pat = get_number($in_file)) {
            get_country($citing_pat);
        }
    }
    close $out_file;
    
    sub get_number {
        my $fh = shift;
         while(<$fh>) {
                if (/DID/) {
                        my $field = (split)[3];
                        if($field =~ /^\d+$/){
                    return $field;
                        }
                }
         }
        return undef;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: $coder = JSON::XS->new->utf8->pretty->allow_nonref; %perl = $coder->decode ($json); When I write
Apologies for a simple question, but I'm very new to Perl! I have an
I have a question about Perl from the script below. If the input for
Hey all, I have a question about perl objects and threading. My program is
I'm fairly new to perl so sorry if this is a newbie question. As
This question is not Perl-specific, (although the unpack function will most probably figure into
I very new to Python, and fairly new to regex. (I have no Perl
In my new job more people are using Python than Perl, and I have
Im new to perl, so sorry if this is obvious, but i looked up
I am very new to Perl, so please bear with my simple question: Here

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.