Solved

String comparison not working???

Posted on 2007-11-24
8
298 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:rkckjk
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 100 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:rkckjk
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 100 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 2

Author Comment

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

Assisted Solution

by:FishMonger
FishMonger earned 100 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 100 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 300 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 300 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

773 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