Solved

string comparison

Posted on 2007-03-28
6
193 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 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