Solved

string comparison

Posted on 2007-03-28
6
198 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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