baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:
i'm trying to solve this problem elegantly, so any suggestions are welcomed.
so the thing is that my $array can be an array of strings, or array references or hash references. and i need to check if it is a set of strings , or arrays or hashes and if those variables (strings or hashes or arrays) are initialized or notuse strict; use warnings; my $array; if (ref $array eq 'ARRAY' && ref $array->[0] eq 'HASH' # checking if t +he contents of array ref is a hash ref ){ do something }
because some times those variables could be declared but not initialized ! (and then i get warnings :) - don't like them very much)
so far i've been doing it like this but now it seams to me that this way is a little bit ... (can't find the right word).
thanx
rb
update:
ok there is an input file from which i read some lines each line has an indicator to tell the program if it should create an array for storing that data or hash or just plain scalar. and then it imports that data into that array or hash or.. but some lines have just an indicator to create a special type of variable but since there is no data to be imported then that variable stays empty.
then after the load process is finished program starts to check what type of data is in array (array ref, scalar or hash ref). and based on what type of data it is, it does something with that data and if the variable is empty ->[] it does something else ( + reports a warning), so it is important for some variables to be declared but empty. hope this clarifies things
Updata 2:
"all of the array elements are of the same kind" - yes , they have to be
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how to check contents of my array ref
by GrandFather (Saint) on Mar 03, 2009 at 10:26 UTC | |
|
Re: how to check contents of my array ref
by rovf (Priest) on Mar 03, 2009 at 11:23 UTC | |
|
Re: how to check contents of my array ref
by cdarke (Prior) on Mar 03, 2009 at 12:02 UTC | |
|
Re: how to check contents of my array ref
by AnomalousMonk (Archbishop) on Mar 03, 2009 at 15:18 UTC |