Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Re: how to hash this

by malaga (Pilgrim)
on Apr 01, 2001 at 00:36 UTC ( [id://68711]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re: Re: Re: how to hash this
by chromatic (Archbishop) on Apr 01, 2001 at 02:01 UTC
    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.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://68711]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-20 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found