Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Determine if a value is numeric in perl script

My perl script takes the first command line argument and sets ‘$numlines’ to it. Then I have an if statement that is supposed to determine if numlines is numeric (I want to use this value later if it is numeric) but even when I input a numeric value as my first command line argument, my script always skips the if statement anyways.

My code:

my $line;
my $counter = 0;
my @list;
my $numlines = shift @ARGV;
if ( $numlines !~ (/^-?\d+$/) ) {
    print "First argument is a number\n";
    while ($line=<>) {
        push(@list, $line);
    }
    @list = reverse @list;
    foreach (@list) {
       if ( $counter == $numlines ) {
          last;
       }
       print "$_";
       $counter++;
    }
    exit 0;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The condition should be the other way round.

if ($numlines =~ /^-?\d+$/)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading