?
Solved

string comparison

Posted on 2007-03-28
6
Medium Priority
?
205 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Accepted Solution

by:
midrisi earned 2000 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 85

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

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!

Question has a verified solution.

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

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans
Suggested Courses

829 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