Professional Web Applications Themes

How to test a hash of arrays for defined/not defined - PERL Miscellaneous

"Steve" <ineverlookatthis> wrote in message news:f0d57f86.0306300420.403e83e7posting.google.c om... > Dear All > > I have a problem which I have reduced to the extreme case below. > I am populating a hash of arrays from a form (several scrolling > lists). > It may be completely undefined - nothing chosen from any list. I test > each for this as below. > > But an undefined hash fails with the message "Can't use an undefined > value as an ARRAY reference". > > Any thoughts ? > > Thanks > > Steve > > #!D:/Perl/bin/Perl.exe -w > > use strict; > ...

  1. #1

    Default Re: How to test a hash of arrays for defined/not defined


    "Steve" <ineverlookatthis> wrote in message
    news:f0d57f86.0306300420.403e83e7posting.google.c om...
    > Dear All
    >
    > I have a problem which I have reduced to the extreme case below.
    > I am populating a hash of arrays from a form (several scrolling
    > lists).
    > It may be completely undefined - nothing chosen from any list. I test
    > each for this as below.
    >
    > But an undefined hash fails with the message "Can't use an undefined
    > value as an ARRAY reference".
    >
    > Any thoughts ?
    >
    > Thanks
    >
    > Steve
    >
    > #!D:/Perl/bin/Perl.exe -w
    >
    > use strict;
    >
    > my (%myparams, $chosen);
    >
    > #$myparams{item}[0]='boo'; # with this line in, runs as expected
    > # without it crashes where I would hope
    > # $chosen to be set to zero
    >
    > $chosen = ({$myparams{item}} || 0); # want to set to number of items
    > selected
    >
    > print " chose: $chosen";
    I think you want:
    $chosen = (defined({$myparams{item}}) || 0);

    or perhaps you want:
    $chosen = ({$myparams{item}}[0] || 0);

    You can, if you like, delete the outside '()'s as they are not necessary.

    Hth.

    Cheers,
    Rob


    Sisyphus Guest

  2. #2

    Default Re: How to test a hash of arrays for defined/not defined

    Steve <ineverlookatthis> wrote:
    > But an undefined hash fails with the message "Can't use an undefined
    > value as an ARRAY reference".
    > $chosen = ({$myparams{item}} || 0); # want to set to number of items
    > selected

    $chosen = defined $myparams{item} ? {$myparams{item}} : 0;


    --
    Tad McClellan SGML consulting
    [email]tadmcaugustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

Similar Threads

  1. Why are some defined and some not???
    By Steve Grosz in forum Macromedia ColdFusion
    Replies: 3
    Last Post: June 16th, 06:51 PM
  2. Need info on how to test for roles defined in web.config.
    By Ken Varn in forum ASP.NET Security
    Replies: 1
    Last Post: August 6th, 12:43 PM
  3. Pre-defined styles
    By Mike Woods webforumsuser@macromedia.com in forum Macromedia Freehand
    Replies: 3
    Last Post: February 5th, 02:41 PM
  4. Sort a hash based on values in the hash stored as arrays of hashes
    By Tore Aursand in forum PERL Miscellaneous
    Replies: 3
    Last Post: September 16th, 10:14 AM
  5. How can I pack a hash in a well-defined key order
    By Steve D in forum PERL Miscellaneous
    Replies: 0
    Last Post: August 22nd, 07:28 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139