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

"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.

Replies are listed 'Best First'.
Re: (bbfu) Re(5): how to hash this
by malaga (Pilgrim) on Apr 01, 2001 at 08:59 UTC
    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.

        i'll have to start fresh tomorrow i think. the way i'm doin it seems ok, but maybe there's a better way. i'll look at them all again. right now i'm stuck because i need to print a file that is named with ref->{'prodtext'}, and i can't seem to open the file within the current block (because it only hits the first instant and stops) and i can't make it work in a sub routine because i'm using a hash and not an array and i don't know if there's a way to pass ref->{'prodtext'} to the subroutine.

        <code> foreach my $ref (@products) { if ($ref->{prodname} =~ ($value)){ print $ref->{prodcode}; print "
        "; print $ref->{prodname}; print "
        "; print $ref->{prodprice}; print "
        "; print $ref->{prodtext}; print "
        "; print "
        "; #here is where i need to open the file named in $ref->{prodtext}, and just print out the contents. i tried to put it in a subroutine, but don't know how to pass the hash element to it. </code.