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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:22:19+00:00 2026-06-14T15:22:19+00:00

I have the following program Extract.pl, which opens a file, finds the lines containing

  • 0

I have the following program “Extract.pl”, which opens a file, finds the lines containing “warning….”, “info…”, “disabling…” then counts and prints the value and number of them. It is working ok.

What I want to do is to create command line arguments for each of the 3 matches – warning, disabling and infos and then run either of them from the command prompt.

Here is the code:

#!/usr/bin/perl

use strict;
use warnings;

my %warnings = ();
my %infos = ();
my %disablings = ();    

open (my $file, '<', 'Warnings.txt') or die $!;                 

while (my $line = <$file>)  {                       

 if($line =~ /^warning ([a-zA-Z0-9]*):/i) {                     
     ++$warnings{$1};                           
}
 if($line =~ /^disabling ([a-zA-Z0-9]*):/i) {                       
     ++$disablings{$1};                         
}
 if($line =~ /^info ([a-zA-Z0-9]*):/i) {                        
     ++$infos{$1};                          
}
}
       close $file;                                     

       foreach my $w (sort {$warnings{$a} <=> $warnings{$b}} keys %warnings) {      
   print $w . ": " . $warnings{$w} . "\n";                  
}
       foreach my $d (sort {$disablings{$a} <=> $disablings{$b}} keys %disablings) {        
   print $d . ": " . $disablings{$d} . "\n";                    
}

       foreach my $i (sort {$infos{$a} <=> $infos{$b}} keys %infos) {   
       print $i . ": " . $infos{$i} . "\n"; 
}
  • 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-14T15:22:20+00:00Added an answer on June 14, 2026 at 3:22 pm

    The builtin special array @ARGV holds all command line arguments to the script, excluding the script file itself (and the interpreter, if called as perl script.pl). In the case of a call like perl script.pl foo bar warnings, @ARGV would contain the values ‘foo’, ‘bar’, and ‘warnings’. It’s a normal array, so you could write something like (assuming the first argument is one of your options):

    my ($warning, $info, $disabling);
    if    ($ARGV[0] =~ /warning/i)   { $warning = 1   }
    elsif ($ARGV[0] =~ /info/i)      { $info = 1      }
    elsif ($ARGV[0] =~ /disabling/i) { $disabling = 1 }
    
    # [...] (opening the file, starting the main loop etc...)
    
    if ( $warning and $line =~ /^warning ([a-zA-Z0-9]*)/i ) {
        ++$warnings{$1};
    }
    elsif ( $info and $line =~ /^info ([a-zA-Z0-9]*)/i ) {
        ++$infos{$1};
    }
    elsif ( $disabling and $line =~ /^disabling ([a-zA-Z0-9]*)/i ) {
        ++$disablings{$1};
    }
    

    I created flag variables for the three conditions before the main loop that goes through the file to avoid a regex compilation on every line of the file.

    You could also use the Getopt::Long or Getopt::Std modules. These provide easy and flexible handling of the command line arguments.

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

Sidebar

Related Questions

I have the following c program which launches a Gtk Program on ubuntu: #include
I have following program which crashed at upper bound call. I am not getting
I have the following program which I'd like to get it fixed. Not sure
I am cross compiling linux kernel. I have the following program which compiles perfectly
Supposedly I have this line in my text file which has the following format.
I have inherited the following db and following query which extracts program names where
I have the following program that writes a user's input to a file. Currently
i have following program to calculate size of file #include <iostream> #include <fstream> #include
I have following simple program: import std.stdio; int main(string[] argv) { writeln(Hello, world!); return
I have the following program. I wonder why it outputs -4 on the following

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.