?
Solved

String comparison not working???

Posted on 2007-11-24
8
Medium Priority
?
303 Views
Last Modified: 2010-03-05
I'm trying to do a string comparison with the Perl Script below, but it's not working for some reason???

Basically I checking to see if string from the XML element: TODAY  in the Previous.xml file is in the Output.txt file. If it is don't write to the text file otherwise write to the file.

Previous.xml file
<?xml version="1.0" standalone="yes"?>
<MSR>
  <Info>
     <TODAY>Tuesday, November 20, 2007</TODAY>
        <numAutoSys>176</numAutoSys>
    <numMVS>35</numMVS>
  </Info>
</MSR>

Text file:
Thursday, November 01, 2007|5:07am|8:45am|4:55am
Friday, November 02, 2007|1:50am|3:25am|1:50am
Friday, November 16, 2007|2:38am|4:24am|3:02am
Monday, November 19, 2007|Running;|Bypassed;|Not
Tuesday, November 20, 2007|4:57am|6:26am|4:55am


So, with the status of the files above it shouldn't write to the text file because the date from the XML element: TODAY which is "Tuesday, November 20, 2007"  is already in the text file as you can see...


thanks


#!/opt/perl/bin/perl
 
use XML::Simple;
my $InputFile = "/MSR_XML_HTML/Previous.xml";
my $OutputFile = "/data/EBIS_Output.txt";
 
open (IN, "<$InputFile") or die "$InputFile $!";
open (OUT, ">>$OutputFile") or die "$OutputFile $!";
 
my $ref = XMLin($InputFile);
my $string = @{$ref->{Info}}{qw(TODAY)};
 
print $string;
while(<IN>) {
      $content=$_;
            }
      if ($content =~ /$string/ig)
         {
         close IN;
         close OUT;
          exit(0);
        } else {
        print OUT (@{$ref->{Info}}{qw(TODAY)}),'|', join('|',map/(\S+)/,@{$ref->{Info}}{qw(TNC RPI CMHM)}),"\n";
          }
close IN;
close OUT;

Open in new window

0
Comment
Question by:Richard Kreidl
[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
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 400 total points
ID: 20343933
I don't see where you read Text file:
It looks like $content will end up containing "</MSR>\n"
0
 
LVL 2

Author Comment

by:Richard Kreidl
ID: 20344008
You're right I changed this line:

while(<IN>) {
 
to this:

while(<OUT>) {


It still doens't work...
0
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 400 total points
ID: 20344235
1)  You have 2 input files (xml and csv), but your script is only reading the xml file.

2)  Your while loop should be reading/processing the csv file, not the xml or output file.

3)  Your while loop is saving the very last line of the file in $content which may or may not hold the data that you need.  You should be comparing each line, not just the very last one.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Author Comment

by:Richard Kreidl
ID: 20344423
Help with the coding please???
0
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 400 total points
ID: 20344449
It's unclear what you intended to send to the output file, so I'll just show you how to fix the comparison test.

use warnings;
use strict;
use XML::Simple;
 
my $InputFile = 'input.csv';
my $XMLFile = "Previous.xml";
 
open (IN, "<$InputFile") or die "$InputFile $!";
 
my $ref = XMLin($XMLFile);
my $string = $ref->{Info}{'TODAY'};
 
while(my $content = <IN>) {
   if ($content =~ /$string/ig) {
      print $content;
      last;
   }
}
close IN;

Open in new window

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 400 total points
ID: 20344474
OUT was opened for appending, not reading, so
while(<OUT>) {
does not work
You could open it for both read and append, and reset file position to the beginning

open (OUT, "+>>$OutputFile") or die "$OutputFile $!";
seek OUT,0,0;

But as Fishmonger points out, the loop would still save only the last line of rhe file.
Perhaps that is what you want.
=~ would also interpret $string as a regular expression,
There are no metacharacters in
Tuesday, November 20, 2007
 so perhaps you don't care.  but if  $string could ever contain metacharacters, you may want to think about exactly what you want he program to be doing
0
 
LVL 17

Assisted Solution

by:mjcoyne
mjcoyne earned 1200 total points
ID: 20344712
Is this what you need?

#!/opt/perl/bin/perl
use warnings;
use strict;

my ($previous_xml, $text_file);

open (XML, "/MSR_XML_HTML/Previous.xml") or die "Can't open Previous.xml: $!\n";
open (TXT, ">>/data/EBIS_Output.txt") or die "Can't open EBIS_Output.txt: $!\n";

{ local $/; $previous_xml = <XML>; $text_file = <TXT>; }

my ($today) = ($previous_xml =~ /<TODAY>(.+?)<\/TODAY>/);

print TXT "$today\n" unless ($text_file =~ /$today/);
0
 
LVL 17

Accepted Solution

by:
mjcoyne earned 1200 total points
ID: 20344730
Sorry, that won't work.  Perhaps something like:

#!/opt/perl/bin/perl
use warnings;
use strict;
use XML::Simple;

my $ref = XMLin("/MSR_XML_HTML/Previous.xml");
my $string = @{$ref->{Info}}{qw(TODAY)};

my ($previous_xml, $text_file);

open (XML, "/MSR_XML_HTML/Previous.xml") or die "Can't open Previous.xml: $!\n";
open (TXT, "/data/EBIS_Output.txt") or die "Can't open EBIS_Output.txt: $!\n";

{ local $/; $previous_xml = <XML>; $text_file = <TXT>; }
close (XML);
close (TXT);
my ($today) = ($previous_xml =~ /<TODAY>(.+?)<\/TODAY>/);

if ($text_file !~ /$today/i) {
    open (OUT, ">>/data/EBIS_Output.txt") or die "Can't open EBIS_Output.txt: $!\n";
    print OUT (@{$ref->{Info}}{qw(TODAY)}),'|', join('|',map/(\S+)/,@{$ref->{Info}}{qw(TNC RPI CMHM)}),"\n";
}

Assuming your output line is correct...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Six Sigma Control Plans

777 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