Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Seaching for text in string and comparing to xml, if not found print text

by nagalenoj (Friar)
on Feb 14, 2013 at 07:30 UTC ( [id://1018691]=note: print w/replies, xml ) Need Help??


in reply to Seaching for text in string and comparing to xml, if not found print text

This could solve your purpose.
my $file = 'Events.txt'; open $info, "<", $file or die "Couldn't open events file: $!"; open $xml, "<", 'XML_FILE.xml' or die "Couldn't open xml file: $!"; open $output, ">", 'OUTPUT.txt' or die "Coudn't open output file: $!"; my $found = 0; #Flag for indication while(my $line = <$info>) { @get_data = split ',' ,$line; $alert_ID = $get_data[5]; seek $xml, 0, 0; # Seek to the beginning, to scan from beginning f +or each ID. while(my $xml_line = <$xml>) { if ($xml_line =~ m|<ALERT_ID>$alert_ID</ALERT_ID>|) { $found = 1; last; } } if ( $found == 1 ) { print "$alert_ID Found.\n"; $found = 0; } else { print $output "@get_data"; } } close $info; close $xml; close $output;

If you don't want anything else to be considered from the XML file. You can remove the other lines(either using sed or grep) before executing the script. It would reduce the processing time significantly.

  • Comment on Re: Seaching for text in string and comparing to xml, if not found print text
  • Download Code

Replies are listed 'Best First'.
Re^2: Seaching for text in string and comparing to xml, if not found print text
by tobyink (Canon) on Feb 14, 2013 at 08:18 UTC

    Argh, no! Don't do this! There's no need to re-read the whole XML file for every line of the text file (unless you expect the XML file to be constantly changing). Read it into a hash once at the start of the script.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1018691]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found