1,105,546 Community Members

PERL custom diff

Member Avatar
iacosar
Newbie Poster
3 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hello, Im new to PERL and I'm writing some code to compare two files lines:

file 1 gets random password from a server
file 2 is a test case to check that I'm getting the password with the required characteristics ( length, alnum or num)

the problem that I have is in the case that the password generated by the server supouse to be numeric
and the password in my test case have a char but, it doesnt throw the error message.

anyways here is some code: for that specific section

elsif (($tact_line =~ m/[0-9]^\w/)&($texp_line =~ m/\w/)){
            print "\nnumeric password can't have alpha characters\n";
            print "$ARGV[0] $tact_line  |   $ARGV[1] $texp_line\n";
            exit 1;
        }
Member Avatar
2teez
Posting Whiz in Training
224 posts since Apr 2012
Reputation Points: 40 [?]
Q&As Helped to Solve: 38 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,
It will be better if you can share more codes here, so that we can see what you are really comparing.
However, there are several things wrong with the code you have shown so far.
Do you intend to use && as against & and || as against |. What then is the purpose of exit 1.
The in your match string in perl REs, ^ out the [] represents the beginning of a string, but inside means exception, so what do you mean by ~ m/[0-9]^\w/.
Sometimes you might what to place at the beginning of your script

use warnings;
use strict;
use diagnostics; ## only use when you want verbose explanations of error

Hope this helps.

Member Avatar
d5e5
Practically a Posting Shark
827 posts since Sep 2009
Reputation Points: 109 [?]
Q&As Helped to Solve: 163 [?]
Skill Endorsements: 2 [?]
 
0
 

The following may not exactly correspond to what you mean by 'numeric' but it will serve as an example of what the Scalar::Util module can do.

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

use Scalar::Util qw(looks_like_number);
while (my $teststring = <DATA>){
    chomp $teststring;#Remove newline character from end of string
    if (looks_like_number($teststring)){
        print $teststring, ' looks like a number to me', "\n";
    }
    else{
        print $teststring, ' does NOT look like a number to me', "\n";
    }
}

__DATA__
hello
5
2.3
-2.3
7t7
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: