1

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"; 
}
7
  • 1
    You forgot to ask a question. What have you tried to read command line arguments? How does that fail? With a simple google search I found millions of good reads even on SO, including getopt. Commented Nov 20, 2012 at 16:35
  • Yes, I will give it a try. I have been reading about command line arguments a lot today, found some examples, but still can't implement them in my code. I have been learning programming and perl in the last dats and I was just seeking help.Thanks. Commented Nov 20, 2012 at 16:47
  • "run either of them from the command prompt." makes no sense. Either what? Commented Nov 20, 2012 at 17:10
  • well i mean that for example if i type in the command prompt Perl Extract.pl Warning it will display only the "Warning" values and not the "disabling" and "infos" and vice-versa. Commented Nov 20, 2012 at 17:15
  • So you "found some examples, but still can't implement them in my code". Why? What's happening? This isn't very helpful. Commented Nov 20, 2012 at 17:42

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @mpe. This is very useful. It is working and I also tried the Getopt::Long module and worked as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.