Solved

String comparison not working???

Posted on 2007-11-24
8
296 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
 
LVL 2

Author Comment

by:rkckjk
ID: 20344423
Help with the coding please???
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Regular expression 9 198
Search in text file in column and compare 4 46
add a syntax to a csv file 8 98
.properties file to call function/method 9 59
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 …
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

911 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now