Solved

String comparison not working???

Posted on 2007-11-24
8
300 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
[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 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
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!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

731 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