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!


In reply to Matching specific strings (are subs a good idea?) by zarath

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.