Loading ...
Sorry, an error occurred while loading the content.
 

Re: [PBML] correct way to test a hash value... for undef or not.

Expand Messages
  • Erik Tank
    That may not be such a good idea. This is from perldoc -f defined : When used on a hash element, it tells you whether the value is defined, not whether the
    Message 1 of 4 , Mar 25, 2004
      That may not be such a good idea. This is from 'perldoc -f defined':
      When used on a hash element, it tells you whether the value is
      defined, not whether the key exists in the hash. Use the
      exists entry elsewhere in this document for the latter purpose.

      'exists' is specifically designed for determining elements of arrays and
      hashes. Truthfully, I don't know the why you can't use 'defined' for
      hashes and array elements but the documentation states it quiet clearly.

      Erik

      You are either alive and proud or you are dead,
      and when you are dead, you can't care anyway.
      - Steve Biko


      Paul Archer wrote:
      > In addition to 'exists' (does the element exist at all), there is also
      > 'defined' (is the element set to a value, or 'undef').
      >
      > Paul
      >
      > 6:15pm, Erik Tank wrote:
      >
      >
      >>I believe you are looking for 'exists'. Of course this only indicates
      >>if the element of the hash has been initialize. For a full explanation
      >>see (this is assuming that you are on a UNIX like system):
      >>perldoc -f exists
      >>
      >>I would do something like this:
      >>
      >>if (exists($hash->{location}) && -e $hash->{location}) {
      >>...
      >>
      >>Hope that helps,
      >>
      >>Erik
      >>
      >>You are either alive and proud or you are dead,
      >>and when you are dead, you can't care anyway.
      >> - Steve Biko
      >>
      >>
      >>franki wrote:
      >>
      >>>Hi guys,
      >>>
      >>>I have a hash of hashes...
      >>>
      >>>I need to test if the child hash key 'location' has a value or not..
      >>>if the locaton key has a value, then the value is the full server path
      >>>of a file to be attached to an email.
      >>>
      >>>If it doesn't have a value,( ie 'location => '' ) then the email is
      >>>sent, but without the attachment.
      >>>
      >>>This code works,
      >>> if (($files_hashref->{$query->param('item_name')}->{'location'} ne '')
      >>>&& ($attach ne 'none'))
      >>> {
      >>> # send email with location file attachment.
      >>> }
      >>>
      >>> elsif (($files_hashref->{$query->param('item_name')}->{'location'} eq
      >>>'') && ($attach ne 'none'))
      >>> {
      >>> # send email without attachment cos there isn't one.
      >>> }
      >>>else {
      >>> # Fall back condition..
      >>> }
      >>>
      >>>but I don't like it because if the location key has no value, then I get
      >>>an "use of uninitialised value in string......."
      >>>message in the log when it tries to see if the location key is ne ''.
      >>>
      >>>Maybe its because its 8.20am in the morning here and I've not yet gotten
      >>>to sleep, but I have been messing for about an
      >>>hour using if (defined xxxx) and exists and anything else I can think
      >>>of.. and I just can't get it to work properly in any other
      >>>way except for the above. (which has the error log problem because of
      >>>the conditional test on
      >>>$files_hashref->{$query->param('item_name')}->{'location'} when it may
      >>>not have a value.....
      >>>
      >>>Can anyone please point me in the right direction??
      >>>I really wanted to sort this out so I can sleep in peace (its the last
      >>>error I needed to sort out).
      >>>If I can't get it sorted, I'll have to go to bed disappointed. :-(
      >>>(I'm going for the sympathy vote here. :-) )
      >>>
      >>>(Oh, I should mention that param('item_name') is quiet safe, its being
      >>>fed to CGI.pm from a file.
      >>>just thought I should mention that in light of our recent security
      >>>discussions.)
      >>>
      >>>rgds
      >>>
      >>>Franki
      >>>
      >>>
      >>>
      >>>Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html
      >>>Yahoo! Groups Links
      >>>
      >>>
      >>>
      >>>
      >>>
      >>>
      >>>
      >>>
      >>
      >>
      >>Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html
      >>Yahoo! Groups Links
      >>
      >>
      >>
      >>
      >>
      >
      >
      > -------------------------------------------------------------
      > "The lawgiver, of all beings, most owes the law allegiance.
      > He of all men should behave as though the law compelled him.
      > But it is the universal weakness of mankind that what we are
      > given to administer we presently imagine we own."
      > ---------------------- H.G. Wells ---------------------------
      >
      >
      >
      > Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html
      > Yahoo! Groups Links
      >
      >
      >
      >
      >
      >
      >
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.