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

i need to open the file containing the data below, and put it in a form that will keep everything between the ";"'s as separate records, and each line within the ";"'s as items i can call up. is a hash the best way to store it, and how would i do it? THANK YOU Data__________
; Begin Product GPY01A2 GPY Angel 2 Print 8.5x11 140.00 0.00 1.00 3.00 No ; Tracking Inventory? http://www.monkey-n-around.com/GPY/gallery/images/sang2.JPG|http:/ +/www.monkey-n-around.com/GPY/gallery/images/sang2.JPG ProdText\GPY01A2.txt SoftGoodControl: ::0:0 PriceCategory: Wholesale:114.00:114.00 Retail:114.00:114.00 Reside +ntial:114.00:114.00 Commercial:114.00:114.00 End Product ; Begin Product ZAF01 Zen Again Fire on Water Fountain 190.00 10.00 16.00 3.00 No ; Tracking Inventory? http://www.monkey-n-around.com/mall/images/fireonwaterfountains.JP +G|http://www.monkey-n-around.com/mall/images/fireonwaterfountains.JPG ProdText\ZAF01.txt Keywords: Zen Again Fire on Water Fountain SoftGoodControl: ::0:0 UnitOfMeasure: Lbs. End Product ;

Replies are listed 'Best First'.
Re: which data structure and how?
by Masem (Monsignor) on Mar 31, 2001 at 02:24 UTC
    Use an array of hashes, for example.

    my @array; my %hash = ( name => "Me", address => "Right here" ); push @array, \%hash; # etc etc etc print $array[2]->{ 'name' }; # prints name from the 3rd hash in the a +rray.

    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      I couldn't make this work with my data. Where am I going wrong? Nothing prints out.
      $value = "GPY"; $lfilename = "products.pdg"; open(FILE, "$lfilename") or &dienice; while(<FILE>) { @row = split(/\;/); if ($row eq $value){ %data; @data{@fields} = @row; push @records, \%data; } } close (FILE); foreach my $ref ( @records ) { print $ref->{Begin}; }
        You are not using -w or strict. In particular, there is nothing in $row, because it's only used once. $row[0] would be a more likely choice.

        Let Perl help you debug.

        Update: Additionally, @fields is not defined, as far as I can see. Even if your if loop executed, you'd be throwing away the data you just read in. This is *exactly* the time to use strict and -w! If the error messages confuse you, use diagnostics as well. Perl will tell you when you throw away your data and that is most of your problem here. The other part is not setting $/ to "\n;\n".

Re: which data structure and how?
by malaga (Pilgrim) on Mar 31, 2001 at 03:14 UTC
    I tried that, but since I don't always have labels i want to say - give me the row that starts with "whatever" in the record that contains ZAF01. Also, some records will have more lines than others.