zarath has asked for the wisdom of the Perl Monks concerning the following question:
Hello
Still a bit new to Perl here, but learning pretty quickly.
Please consider the following piece of code.
The context and what I'm supposed to do with this:
I have several .xml-files (here FILE) and 1 .txt-file (here QR).
Every FILE is a .xml-file about a person, but these files lack the date of birth, it is my assignment to complete these files by adding this date of birth.
This is where the file QR steps in, this contains FirstName, LastName, Email and BirthDate of every one of these people.
So with the code underneath I am trying to match FirstName, LastName and Email in both FILE and QR to extract the BirthDate that way and paste it into FILE.
This question would not be here if it worked... Apparently $firstname, $lastname and $email need to be defined in the sub fetchBD. But if you look at the main piece of code, these 3 are defined by the time this sub gets called, so I don't understand why this is an issue.
I am not used to using subroutines, so it just might be a bad choice to throw them in, it just seemed to me that is would be the easiest way of doing what I'm supposed to do. Definitely feel free to disagree here and all tips are welcome.
2 notes that are less important:
1. use of index() and substr() are to my knowledge the safest way to fetch a specific string if it is surrounded by a known constant, any tips on easier / cleaner-looking ways are of course welcome.
2. the use of a triple 'if'-block seems to me the safest way to make sure all 3 strings are matched from the same FILE and from the same QR-line. Again, any tips on easier / cleaner-looking ways are of course welcome.
use strict; # because we should use warnings; # because we should use autodie; # die if problem reading or writing a file my $base = 'D:/Some/Specific/Folder'; open (QR, '<', $oldqr); for my $file (glob qq($base/ToUpload/Staging/*)) { open FILE,'+<',$file; binmode (FILE); while (my $line = <FILE>){ my $firstname = getFN(); my $lastname = getLN(); my $email = getEM(); if ($line =~ /<birthdate>/) { my $bdate = fetchBD(); print FILE ' <birthdate>'.$bdate.'</birthdate>'; } } } close QR; sub fetchBD { while (my $line = <QR>){ my $fns = index($line,'FirstName'); $fns += 10; my $fne = index($line,' LastName'); my $fnl = $fne - $fns; my $fn = substr($line,$fns,$fnl); my $lns = index($line,'LastName'); $lns += 10; my $lne = index($line,' Email'); my $lnl = $lne - $lns; my $ln = substr($line,$lns,$lnl); my $ems = index($line,' Email '); $ems += 7; my $eme = index($line,' BirthDate'); my $eml = $eme - $ems; my $em = substr($line,$ems,$eml); if ($fn = $firstname) { if ($ln = $lastname) { if ($em = $email) { my $bdates = index($line,' BirthDate '); $bdates += 11; my $bdate = substr($line,$bdates,10); return $bdate; } } } } } sub getFN{ while (my $line = <FILE>){ if ($line =~ /<firstname>/) { my $fnse = index($line,'<firstname>'); $fnse += 11; my $fnen = index($line,'</firstname>'); my $fnlo = $fnen - $fnse; my $firstname = substr($line,$fnse,$fnlo); return $firstname; } } } sub getLN{ while (my $line = <FILE>){ if ($line =~ /<name>/) { my $lnse = index($line,'<name>'); $lnse += 6; my $lnen = index($line,'</name>'); my $lnlo = $lnen - $lnse; my $lastname = substr($line,$lnse,$lnlo); return $lastname; } } } sub getEM{ while (my $line = <FILE>){ if ($line =~ /<email>/) { my $emse = index($line,'<email>'); $emse += 7; my $emen = index($line,'</email>'); my $emlo = $emen - $emse; my $email = substr($line,$emse,$emlo); return $email; } } }
I am sure I'm missing something crucial here, but I'm having a hard time to keeping track of what I'm actually doing with this code.
Thank you for your help anyways!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Matching specific strings (are subs a good idea?)
by Athanasius (Archbishop) on Sep 07, 2017 at 17:00 UTC | |
|
Re: Matching specific strings (are subs a good idea?)
by karlgoethebier (Abbot) on Sep 07, 2017 at 16:59 UTC | |
|
Re: Matching specific strings (are subs a good idea?)
by Laurent_R (Canon) on Sep 07, 2017 at 20:21 UTC | |
|
Re: Matching specific strings (are subs a good idea?)
by zarath (Beadle) on Sep 08, 2017 at 07:56 UTC | |
by choroba (Cardinal) on Sep 08, 2017 at 08:23 UTC | |
by poj (Abbot) on Sep 08, 2017 at 08:11 UTC |