How to interrogate array cell?

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default How to interrogate array cell?

    I have an array of hashes. What function should I be
    using to interrogate each array cell when I want to
    know if it is occupied?

    "exists" seemed to do the job nicely. What about
    "defined"?

    Now I am curious: how would I implement a switch
    statement (er, I mean, set of if-elsif statements) for
    a hetrogeneous array where some array cells contain
    arrays, others integers, other hashes?

    I tried saying
    my @x;
    if(@x && $x[$ii] && %{$x[$ii]} && exists
    $x[$ii]{"xyz"}){
    my $z = $x[$ii]{"xyz"};
    ...}

    But that did not work. There must be some function
    that will tell me "this array cell contains a hash,
    this other array cell contains another array...".

    Thanks,
    Sieg

    __________________________________
    Do you Yahoo!?
    Yahoo! Finance: Get your refund fast by filing online.
    [url]http://taxes.yahoo.com/filing.html[/url]
    Richard Heintze Guest

  2. Similar Questions and Discussions

    1. cfgrid - Update a cell based on the value of another cell
      I would like to update a hidden cell based on the value of a visible cell. Is this possible? Thanks
    2. How to set cell background based on cell value when datagrid is displayed
      I would like to check a datagrid cell value, and change the color of the cell background, when a datagrid is displayed. I want to do this as early...
    3. I need to have a tooltip appear over a cell showing data from another cell in the same row.
      I have a datagrid with locations from around the world. In hidden cells, I have the Lat and Long of that location. I need to be able to mouseover...
    4. RadioButtonList In A DataGrid Cell - Can I find the selected button without editing the cell?
      I have an ASP.NET form with a DataGrid and Button. I want to put a RadioButtonList in a DataGrid cell. I bind it to an ArrayList which has a...
    5. make array cell = array?
      I want to make a 2D array quickly $item = an array with the associative names , , etc... I want to do: $items_array = $item $i++
  3. #2

    Default Re: How to interrogate array cell?

    Hi,

    Richard Heintze wrote:
    >I have an array of hashes. What function should I be
    >using to interrogate each array cell when I want to
    >know if it is occupied?
    >
    > "exists" seemed to do the job nicely. What about
    >"defined"?
    >
    exists only works with hashes. Please check the recent conversation about "Array containment", where Rob Dixon advised me to turn my array into a hashto use the exists function.

    - Jan
    --
    There are two major products that come out of Berkeley: LSD and UNIX. We don't believe this to be a coincidence. - Jeremy S. Anderson
    Jan Eden Guest

  4. #3

    Default Re: How to interrogate array cell?

    > Hi,
    >
    > Richard Heintze wrote:
    >
    > >I have an array of hashes. What function should I be
    > >using to interrogate each array cell when I want to
    > >know if it is occupied?
    > >
    > > "exists" seemed to do the job nicely. What about
    > >"defined"?
    > >
    > exists only works with hashes. Please check the recent conversation
    about "Array containment", where Rob Dixon advised me to turn my array
    into a hash to use the exists function.
    >
    Caveat version... depends on the version of Perl. Newer 'exists' can
    check arrays... from perldoc -f exists:

    "Given an expression that specifies a hash element or array element,
    returns true if the specified element in the hash or array has ever been
    initialized...."

    Works at least as of 5.6.1.

    As to your other question, check out:

    perldoc -f ref

    You may also find,

    perldoc UNIVERSAL

    particularly 'isa' helpful...

    [url]http://danconia.org[/url]
    Wiggins D Anconia Guest

  5. #4

    Default Re: How to interrogate array cell?

    On Feb 10, 2004, at 8:50 AM, Richard Heintze wrote:
    > I have an array of hashes. What function should I be
    > using to interrogate each array cell when I want to
    > know if it is occupied?
    >
    > "exists" seemed to do the job nicely. What about
    > "defined"?
    exists() tests if the slot was ever assigned to. defined() tests if
    the slot contains a defined value (read: not undef). It's a subtle
    difference and it gets a little fuzzier inside a loop, where skipped
    slots will come up, as long as a later slot has been assigned to. (See
    code below.) For that reason, I generally want defined() when talking
    about arrays.
    > Now I am curious: how would I implement a switch
    > statement (er, I mean, set of if-elsif statements) for
    > a hetrogeneous array where some array cells contain
    > arrays, others integers, other hashes?
    See if this give you some ideas:

    #!/usr/bin/perl

    use strict;
    use warnings;

    my @complex = ( [ 1, 2, 3 ], 405, { dogs => 200, cats => 16 } );
    $complex[4] = qr/a regex/;

    print "Skipped slot.\n\n" unless exists $complex[3];

    foreach (@complex) {
    unless (defined $_) {
    print "Undefined slot.\n";
    next;
    }
    my $type = ref($_) || "INTEGER";

    if ($type eq "ARRAY") { print "Array found: ", join(", ", @$_), "\n";
    }
    elsif ($type eq "HASH") {
    my %hash = %$_;
    print "Hash Found: ",
    join(", ", map { "$_ => $hash{$_}" } keys %hash), "\n"; }
    elsif ($type eq "INTEGER") { print "Integer found: $_\n"; }
    else { print "Unknown type: $type\n"; }
    }

    __END__

    James

    James Edward Gray II Guest

  6. #5

    Default Re: How to interrogate array cell?

    Jan Eden wrote:
    >
    > Richard Heintze wrote:
    >
    > >I have an array of hashes. What function should I be
    > >using to interrogate each array cell when I want to
    > >know if it is occupied?
    > >
    > > "exists" seemed to do the job nicely. What about
    > > "defined"?
    > >
    >
    > exists only works with hashes. Please check the recent conversation about "Array
    > containment", where Rob Dixon advised me to turn my array into a hash to use the
    > exists function.
    Sorry if I misled you Jan, but 'exists' works on both hash and array elements. Look:

    use strict;
    use warnings;

    my @array;

    @array[2, 4] = (1, 1);

    for (0 .. 6) {
    print "Element $_ Exists\n" if exists $array[$_];
    }

    **OUTPUT

    Element 2 Exists
    Element 4 Exists


    The only reason I suggested that you generate a hash from your array was that,
    if the array was invariant and you needed to do several lookups, it would be
    faster doing hash accesses than linear searches through the array.

    Look at

    perldoc -f exists

    for this topic, and make sure you read

    perldoc -q "array contains"

    about your previous question.

    Rob


    Rob Dixon Guest

Posting Permissions

  • You may not post new threads
  • You may 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