in reply to Re: Re: Re: how to hash this
in thread how to hash this

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.

Replies are listed 'Best First'.
(bbfu) Re(5): how to hash this
by bbfu (Curate) on Apr 01, 2001 at 07:14 UTC

    "i thought this: ... was putting each line of the file into the hash"

    It is. The problem is that you need to declare $ref before the print line. You're also declaring $hash and never using it and you're declaring %hash and $line twice.

    Plus, @products is being populated for you (or, rather, it would be if you weren't lasting right before it all the time) but you're never using it. I say, ditch $ref and use @products.

    open (FILE, $lfilename) or &dienice; # my $hash; # Never used. ??? # my $line; # Declared my inside the while(). ??? # my %hash; # Declared my inside the while(). ??? 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>; # Why are you doing this, anyway? # last if ( $line =~ /End Product/ ); push @products, \%hash; }} # @products contains all of your hashes. # Lookup the one you want in it. print "<B>$products[0]{name}</B>"; # Where was $ref supposed to've come from? # print "<B>", my $ref->{name}, "</B>";

    Well, I hope that helps some. You should definately read perlref.

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.

      ok, i've had 3 beers at my sons wrestling awards, but let me give it a try: this is what i think i'm doing:

      1. i can't just use the array, because the records will not always have the same number of lines.
      2. i think i have to use last because the lines between  Begin Record and  End Recordneed to go into separate records, since i need to search through and pull specific items from them later.
      am i way off base?

      cheers!

        Forgive me if I'm not much help. I'm pretty tired and I have work to do so I can't spend long on it...

        I didn't think about the fact that the records would have different number of lines in them. Am I ok to assume that the first part of the records will all be the same and just have an optional number of sub-recrods (Options)? If so, then are you wanting to save the Option records? If not, then just remove the last like I said and you should be ok (since the while(<FILE>) will slurp up all the Option records as well as the "End Product" and ";" lines; ie: the if block won't match). If you did want to save the Option records, you'll have to put a sub-while loop in just before the push to read in all the Option records. It should have similar logic to the outter while loop.

        That said, I'm not sure this is the best way to go about this. I don't really like reads inside the if block because you're not doing any EOF checking on them and you're usurping control from the while. I think you might be better off going with Trimbach's suggestion or, perhaps even better (depending on how you implement it), DeaconBlues' method, both of which use $/, though DeaconBlues' use of it actually make more use of it. (Okay, that last bit didn't make much sense but, like I said, I'm sleepy.)

        Anyway, sorry I couldn't help more. Hope you get it. Gotta work. =(

        bbfu
        Seasons don't fear The Reaper.
        Nor do the wind, the sun, and the rain.
        We can be like they are.