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

I have the following data in a file:

ABC corp. 1 1002003 1002007 some text here 2 1011999 1012020 other text here XYZ Ltd. 1 2031994 2032071 some text here 2 2021996 2022030 other text here 1 1871995 1872031 some text here 2 1772004 1772021 other text here ...

and I need to match individual records in another file that start with a give id+year (e.g. 2032002) to its corresponding range per the data show above.

I have thought to load the data above into a record and then use that record to identify matches as I parse through the other data file. As matches are found I write the results to a results page that includes counts, company name, and the data (or some unique id that can be used to refer to the data).

I am fairly new to Perl and am finding it complicated to extract data out of my record... (which would look something like below):

@corps = ( { name => "ABC Corp.", ranges => [ { bid = "1022002", eid = "1022002", prefix = "102" }, { ... } ] }, { name => "XYZ Ltd.", ranges => [ { ... } ] } );

Reading the data file (sample in the top of email) and dynamically creating this record seems somewhat tricky but I was able to do it (I think). What I am having trouble with is printing the data (so I can check it is assigned correctly)! Help would be appreciated...

Cheers,
David

Replies are listed 'Best First'.
Re: Records question
by BrowserUk (Patriarch) on Jan 11, 2003 at 01:19 UTC

    This seems to achieve your requirements. You may need to tighten the regexs depending on how faithful the real data is to the sample provided.

    Code

    Output


    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

      oh boy! This is not fair! ;) I have been humiliated ;p It took me all afternoon to work on this and in a few minutes you cooked it up! Needless to say I need alot of practice! Thanks for the help!!!
Re: Records question
by rbc (Curate) on Jan 11, 2003 at 00:45 UTC
    The easiest way to print out @corps would be to use Data::Dumper
    like so ...
    ... use Data::Dumper; ... print Dumper( @corps );
      print Dumper( \@corps );    # Will probably be neater.

      If you pass an array, it'll treat it as a bunch of scalars. Passing a reference is cleaner.

      -sauoq
      "My two cents aren't worth a dime.";
      

      Great! That is definatelly quick (and simpler). :) Nonetheless to complete my code I will eventually have to access that data... and since it is nested and requires a long trail to get to the inner data is it considered easier or more common practice to simply store the data in an array and separate field values and records with some delimeter? Or am I on a good track (records is good for this and it won't be to difficult to get at the data)?

      thanks!
        Delimited data organization might seem tempting, especially if the concept of references is a little foreign. But I think you will find that in general, manipulating data internally using records is good, and internally using just delimited data in an array is bad, unless your data is very simple. In a complex record structure, each atom of data has a location, which can't be said for the delimited data; it requires you to create temporary variables and use split every time you need access to something. There is also the problem of quoting/escaping data so it doesn't interfere with the delimiter. Given that your record structure is intelligently organized (it seems that way so far), you really can't go wrong. Once you really come to understand Perl's way of handling references, you will find very little data that you can't model inside your program with complex structures. The same can't be said for a delimited array.

        See perlreftut for more info.

        blokhead