Solved

string comparison

Posted on 2007-03-28
6
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

733 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