Solved

string comparison

Posted on 2007-03-28
6
196 Views
Last Modified: 2010-03-05
$c = `$command`;
$name = "john";
if ($c !~ /Name: $name/gm) {
 print "fail \n";
}

The above command is not printing "fail"

the output of $command is:

Name: Jack
ID: 123
City: San Jose

0
Comment
Question by:perlperl
6 Comments
 
LVL 10

Expert Comment

by:xanius
ID: 18808276
Hi perlperl,

I've just tested your script. on my window box it works. Are you shure $command is doing what you want it to? Try:

$c = `$command`;
print "$command\n";
$name = "john";
if ($c !~ /Name: $name/gm) {
 print "fail \n";
}

to see if it in deed does whart you're expecting.

Cheers
Xanius
0
 

Author Comment

by:perlperl
ID: 18808305
Strange, when i try on different host...it works....unbelievable..both the host has same version of perl 5.6.0
0
 
LVL 10

Expert Comment

by:xanius
ID: 18809488
Have you checked the output of $command on both hosts?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Accepted Solution

by:
midrisi earned 500 total points
ID: 18810770
I assume your command returns output with "\n".So script will be as below:

@output=`command`;
$c=join("\n",@output); # this will glue your output as a single string
$name = "john";
if ($c !~ /Name: $name/gm) {
 print "fail \n";
}

I think this may solve your problem.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 18811696
You should *always*, *always* check the status of opening files and running system commands.

my $c = `command` or die "Can not run command because $!\n";
0
 
LVL 84

Expert Comment

by:ozo
ID: 18817586
$c = `$command`;
is the same as
@output=`command`;
$c=join("",@output);

$c = `command` or die  doesn't test whether the command can be run, it tests whether the command had any output, (and not an output of "0")
you can test whether the command was run by checking $?

Unless $command had Name: john somewhere, in addition to
Name: Jack
ID: 123
City: San Jose
the snippet shold have printed
fail
Could there be something else in your program that is causing the problem?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now