Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

The A and the Z are the file header and footer. Within the file header and footer there can be multiple transactions. The H and the S are the transaction header records for TL and LTL respectively. The C is the Carrier record, it occurs once per H or S, the L is the Location record, will have an L record for each pick-up and delivery location. The R, O, I and P records are the order records and can occur multiple times for each stop (L record). Below is an example of an ROI abc file.

1. The input file is abc.txt

2. Check file for TL header records (adds, deletes, modifies) identified by "HA", "HD" and "HM".

2a) Check Art file for the LTL header records (adds, deletes,modifies) identified by "SA" , "SD" and "SM".

3. For Adds, ("HA", "SA"), catch email value.(It should be in between "P" records (order R, O, I and P).

4. For Modifications, ("HM", "SM"), catch email value.(It should be in between "P" records (order R, O, I and P).

I would like to catch email address values before HATL or SALTL or HMTL or SMLTL or HDTL or SDLTL line starts...

I started writing like this ...

#!/usr/bin/perl open SEE ,"c:/abc.txt"; @ok=<SEE>; close(SEE); foreach(@ok) { if($_ =~ /HATL/) { ($o,$t) = split(/\s+/, $_); print "second value $t<br>"; ### I would like to catch email address values before HATL or SALTL li +ne starts... ### How can i do that? } if($_ =~ /SALTL/) { ($o,$t) = split(/\s+/, $_); print "second value $t<br>"; } }
Please try to provide a soln... Thanks

update (broquaint): added formatting + dropped customer data sample

Replies are listed 'Best First'.
Re: catch values from txt file
by Tomte (Priest) on Jun 23, 2003 at 10:12 UTC

    Update: thanks broquaint ;)

    Update II: Check if Mail::Address suits your needs, it extracts all found email-addresses from a given line of text and returns Mail::Address objects.

    Always Super Search on such questions, or have a deeper look into Categorized Questions and Answers, as parsing an email-address is a very common problem, and as dragonchild, I believe, once said: "90% of every perl-program are already written". And 99% of these 90% can be found on CPAN ;)

    And I thought the forced preview would make you think...
    silly me *sigh*

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

Re: catch values from txt file
by BrowserUk (Patriarch) on Jun 23, 2003 at 14:58 UTC
    As Tomte said, when you get to actually match your email addresses, use a canned solution from CPAN.

    With respect to using such a canned solution at the right time within your file structure, at a cursory glance, breaking your file into lines isn't necessarialy helpful and could well be making your life more difficult. It is possible that some creative use of $\ would greatly simplify your logic of detecting only the email addys your interested in.

    However, it's very difficult to speculate further based upon your--probably very accurate, but extremely abstract--description of your file format. A (few) concrete examples of what the file structures look like would make the description much easier to follow and might draw a simpler solution to the overall problem.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller