Ask a Question related to PERL Beginners, Design and Development.
-
Richard Heintze #1
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
-
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 -
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... -
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... -
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... -
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++ -
Jan Eden #2
Re: How to interrogate array cell?
Hi,
Richard Heintze wrote:
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.>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"?
>
- 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
-
Wiggins D Anconia #3
Re: How to interrogate array cell?
> Hi,
about "Array containment", where Rob Dixon advised me to turn my array>
> Richard Heintze wrote:
>> exists only works with hashes. Please check the recent conversation> >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"?
> >
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
-
James Edward Gray II #4
Re: How to interrogate array cell?
On Feb 10, 2004, at 8:50 AM, Richard Heintze wrote:
exists() tests if the slot was ever assigned to. defined() tests if> 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"?
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.
See if this give you some ideas:> 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?
#!/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
-
Rob Dixon #5
Re: How to interrogate array cell?
Jan Eden wrote:
Sorry if I misled you Jan, but 'exists' works on both hash and array elements. Look:>
> 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.
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



Reply With Quote

