Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Hash problem.

by Anonymous Monk
on May 16, 2001 at 15:46 UTC ( [id://80852]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Hash problem.
by azatoth (Curate) on May 16, 2001 at 15:48 UTC
Re: Hash problem.
by DeaconBlues (Monk) on May 16, 2001 at 17:21 UTC

    It is difficult for me to reproduce your problem because your code is not generalized in any way. I do not have Lotus Notes. However I have run into this error many times. I recently read a wonderful suggestion to safequard against this error.

    First suggestion is to turn on warnings and diagnostics for testing. This shows you as much information about your error as possible.{leave the strict pragma on!}

    use warnings; use diagnostics;

    So here is an simple example of the error:

    use strict; use warnings; use diagnostics; my $href; my %hash; %hash = %{$href}; --Error-- Can't use an undefined value as a HASH reference at line 8 (#1) (F) A value used as either a hard reference or a symbolic referenc +e must be a defined value. This helps to delurk some insidious errors. Uncaught exception from user code: Can't use an undefined value as a HASH reference at line 8.

    You can not deference a reference that is not defined. But you can deference a reference to an empty object. Like this:

    use strict; use warnings; use diagnostics; my $href = {}; my %hash; %hash = %{$href}; --Error-- This caused no error

    And here is the wonderful suggestion I read in the Effective Perl Programming Book p.125 by Hall with merlyn. If you can't intialize your hash reference, have the expression default to an empty anonymous hash reference. Like this:

    use strict; use warnings; use diagnostics; my $href; my %hash; %hash = %{$href || {}}; --Error-- This caused no error also

    I apologize if this comment was too general and was not helpful in solving your problem

    Man I love that book!

    -DeaconBlues
Re: Hash problem.
by davorg (Chancellor) on May 16, 2001 at 15:52 UTC

    When you "preview" a node you get the message:

    If something looked unlike you expected it to you might need to check out Writeup Formatting Tips

    Which part of that didn't you understand?

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: Hash problem.
by t'mo (Pilgrim) on May 16, 2001 at 19:21 UTC

    As far as the original error goes ("Can't use an undefined value as a HASH reference at line 49"), it looks like your problem is this:

    $FullName = $Document->GetFirstItem('FullName')->{Text};

    The call to $Document->GetFirstItem() must be returning an undefined value. Why it is doing that is for you to figure out...

    Aside from that, I've just got a few general comments. Give the code below, which is a modified copy of what you originally had:

    use strict; use Win32::OLE; use Data::Dumper; ### Skipped everything that worked... my ($FullName, $Extension); my $Notes = Win32::OLE->new('Notes.NotesSession)' or die "Can't start Notes session: $!"; ### (1) ### my $Database = $Notes->GetDatabase('GLOSPK2', 'iddnames.nsf'); my $AllDocuments = $Database->AllDocuments(); ### (2) ### my $Count = $AllDocuments->Count; ### (3) ### print "There are $DocCount documents in the database.\n"; ### (4) ### for ( my $Index = 1; $Index < $Count; $Index++ ) { # do stuff... }

    I had these observations, suggestions, etc. All of them should be taken with a grain of salt, however, given that I've never done any Lotus Notes programming via Perl.

    1. Will GetDatabase() return an error? Why aren't you checking? (I am not familar with the Win32::OLE module myself, so I briefly scanned this, and did notice that there is a Win32::OLE->LastError() function; however, I'm not sure if you can use that. Just an idea...
    2. Why do you need to maintain a separate count? If $AllDocuments is a list, a hash, a list ref, or a hash ref, you can access the count anytime you need it. E.g., the following expression will return a 'count' if it is a hash ref:
      scalar keys %{ $AllDocuments }
    3. Did you mean $Count instead of $DocCount?
    4. The built-in list/hash iteration capabilities of Perl are nice...I would use them. I notice later in your loop you use GetNthDocument. Why not use GetNextDocument if you can't do something like this:

      foreach my $Document ( each %{ $AllDocuments } ) { ... }

      (I cheated and looked at the Notes docs to find that)

    Finally, I'd recommend a closer look at your Notes documentation. I found an example in my Notes docs that "shows how you can use a While loop and GetNextDocument to access every document in a view". You may find that example enlightening.

    p.s. -- get a PerlMonks account, log in, stick around... you'll like it here. :-)

Re: Hash problem.
by Anonymous Monk on May 16, 2001 at 15:53 UTC
    Many apologies... have corrected with the CODE tags... I'm having problems with a hash problem and am getting the response - "Can't use an undefined value as a HASH reference at line 49" from my procedure. I am accessing a Notes database (holds telephone directory information) with this procedure included below. The commented out section (used against my mailbox) works fine - but the second section (used against the telephone directory database) gives the error... If anyone has any hints/can give any help i would be grateful! Thanks, Lewis Bishop
    use strict; use Win32::OLE; use Data::Dumper; my %count; my $Buffer; my $Document; my $DocCount; my $FullName; my $Extension; my $cnt = 1; my $Notes = Win32::OLE->new('Notes.NotesSession') or die "Cannot start + Lotus Notes Session object.\n"; my ($Version) = ($Notes->{NotesVersion} =~ /\s*(.*\S)\s*$/); print "The current user is $Notes->{UserName}.\n"; print "Running Notes \"$Version\" on \"$Notes->{Platform}\".\n"; $cnt = 0; #------------------this section does work correctly------------------- +---------- #my $Database = $Notes->GetDatabase('GBLOSPK01', 'mail\\lbishop.nsf'); #my $AllDocuments = $Database->AllDocuments; #my $Count = $AllDocuments->Count; #print "There are $DocCount documents in the database.\n"; #for (my $Index = 1 ; $Index <= $Count ; ++$Index) { # my $Document = $AllDocuments->GetNthDocument($Index); # $Subject = $Document->GetFirstItem('Subject')->{Text}; # $From = $Document->GetFirstItem('From')->{Text}; # #$Body = $Document->Body(); # if (index($From,"CN=") > -1) { # $From = substr($From,3); # if (index($From,"/OU=") > -1) { # $From = substr($From,0,index($From,"/OU=")); # } # } # if ($Index < 415) { # print "$Index - From: $From - Subject: $Subject\n"; # #print "BODY: $Body\n"; # } #} #------------------this section does not work correctly--------------- +-------------- my $Database = $Notes->GetDatabase('GBLOSPK02', 'iddnames.nsf'); my $AllDocuments = $Database->AllDocuments; my $Count = $AllDocuments->Count; print "There are $DocCount documents in the database.\n"; for (my $Index = 1 ; $Index <= $Count ; ++$Index) { my $Document = $AllDocuments->GetNthDocument($Index); $FullName = $Document->GetFirstItem('FullName')->{Text}; $Extension = $Document->GetFirstItem('Extension')->{Text}; if ($Index < 145) { print "$FullName - $Extension\n"; } }
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-05-20 17:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found