[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

String comparison not working???

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
Richard Kreidl
Asked:
Richard Kreidl
  • 2
  • 2
  • 2
  • +1
6 Solutions
 
ozoCommented:
I don't see where you read Text file:
It looks like $content will end up containing "</MSR>\n"
0
 
Richard KreidlSoftware DeveloperAuthor Commented:
You're right I changed this line:

while(<IN>) {
 
to this:

while(<OUT>) {


It still doens't work...
0
 
FishMongerCommented:
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
Independent Software Vendors: 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!

 
Richard KreidlSoftware DeveloperAuthor Commented:
Help with the coding please???
0
 
FishMongerCommented:
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
 
ozoCommented:
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
 
mjcoyneCommented:
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
 
mjcoyneCommented:
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now