in reply to how to hash this

It looks like the file format you are working with is lacking in the best of structure, so you're going to have to apply some explicit rules do handle this.

Here's how I would approach this, not necessarily knowing everything about the file format:

my @products; while ( <FILE> ) { if ( /^Begin Product (.*)/i ) { # We have a product starting line my %hash; $hash{ 'name' } = $1; # get the product name. # I'm guessing on the next 6 lines as to their functions. $hash{ 'description' } = <FILE>; $hash{ 'numberline1' } = <FILE>; $hash{ 'numberline2' } = <FILE>; my $track = <FILE>; $hash{ 'tracking' } = ( $track =~ /(Yes|No)/i ); $hash{ 'images' } = <FILE>; $hash{ 'producttext' } = <FILE>; # Now you have some weirdness to your stuff. Some of # your items have an XML-like structure, some don't. # I'll assuming that if the lines starts with Begin, # it indicates the start of a list of items. Of course, # End Product will end all this. my $line = <FILE>; last if ( $line ~= /End Product/ ); if ( $line ~= /^\s*Begin Option (.*)$/ ) { my $name = $1; my @option_list; while ( <FILE> ) { last if ( /End Option/ ); push @option_list, $_; } $hash{ $name } = \@option_list; } else { # if there is no option, then stick what's in front of the : as +the hash name, the rest as it's value my ( $name, $value ) = ( $line ~= /^\s*(.*?):(.*)$/ ); $hash{ $name } = $value; } push @products, \%hash; }
But again, your file format is very awkward, and without more details, there's not much more we can do with it. However, what we've given you should give you a good start of how to set up a data structure to use with this file.
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

Replies are listed 'Best First'.
Re: Re: how to hash this
by malaga (Pilgrim) on Mar 31, 2001 at 23:08 UTC
    thanks, this gave me some good clues.
Re: Re: how to hash this
by malaga (Pilgrim) on Apr 01, 2001 at 00:36 UTC
    after 2 hours, i can't figure out where the errors are coming from.
    open (FILE, $lfilename) or &dienice; my $hash; my $line; my %hash; my @products; while ( <FILE> ) { if ( /^Begin Product (.*)/i ) { my %hash; $hash{ 'name' } = $1; $hash{ 'description' } = <FILE>; $hash{ 'numberline1' } = <FILE>; $hash{ 'numberline2' } = <FILE>; my $track = <FILE>; $hash{ 'tracking' } = ( $track =~ /(Yes|No)/i ); $hash{ 'images' } = <FILE>; $hash{ 'producttext' } = <FILE>; my $line = <FILE>; last if ( $line ~= /End Product/ ); ##i'm getting a syntax error o +n this line: line 27, near "$line ~". if i take out the line, i get: + Can't use an undefined value as a HASH reference at c:\PROGRA~1\APA +CHE~1\APACHE\CGI-BIN\GETPRO~2.CGI line 31, <FILE> line 2134. push @products, \%hash; }} print "<B>", my $ref->{name}, "</B>";
      Your error is in the exact line that Perl complains about. It is in the exact spot that Perl complains about. Why do you not believe Perl?

      Simply change your code to read:

      last if ($line =~ /End Product/);

      There is no ~= operator in Perl, which is what the first error means.

      As for the second error, you're declaring $ref in the same line in which you try to access it. Why would there be anything in $ref, much less a hash reference that contains a key named 'name'?

      I suggest rereading perldoc perlop, perlvar, and maybe perlref.

        ok, sorry. i believe, i believe. the first mistake was stupid. the second, i still don't get. i thought this:
        while ( <FILE> ) { if ( /^Begin Product (.*)/i ) { my %hash; $hash{ 'name' } = $1; $hash{ 'description' } = <FILE>; $hash{ 'numberline1' } = <FILE>;

        was putting each line of the file into the hash with the key names being 'name', 'description', 'numberline1', etc.