baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

hi

i'm trying to solve this problem elegantly, so any suggestions are welcomed.

use 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 }
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 not

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

    Although you can pull apart a structure in that fashion to see what state it is in, it is probably smarter to understand why the structure doesn't contain the data that is expected. If the issue is that code elsewhere is wrong then ignoring the problem here is wrong too.

    If it is normal for some of the data to be missing or in an unexpected form then you should try to structure you code to make that clear. For example, you could use exceptions (see eval and die) to escape from areas of code where things aree bad and avoid propagating corrupted data to areas of the code that doesn't have any sensible way of handling the situation.

    If this doesn't make sense to you, then you need to tell us something about the bigger problem you are trying to solve so we can suggest a better solution.


    True laziness is hard work
Re: how to check contents of my array ref
by rovf (Priest) on Mar 03, 2009 at 11:23 UTC

    Is it a requirement, that if the all of the array elements are of the same kind, or is it for instance allowed that $array->[0] is an array ref, $array->[1] is a hash ref, etc.? Because this decision has an impact on how error checking can be done best.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: how to check contents of my array ref
by cdarke (Prior) on Mar 03, 2009 at 12:02 UTC
    I would take a different approach. You state that "each line has an indicator". Why not have a hash with each indicator as a key. The value for each key would then be a reference to a subroutine to be executed for that type of data. It is then a fairly simple generic call, assuming the indicator is defined.
Re: how to check contents of my array ref
by AnomalousMonk (Archbishop) on Mar 03, 2009 at 15:18 UTC
    I agree with other monks who have commented that the true inelegance may lie in other parts of the program, but I just wanted to point out that the condition sub-expression
        [...] !(ref $array->[0] eq 'ARRAY') && ref $array->[0] eq 'HASH'
    is redundant: if  $array->[0] is an array reference, it cannot be a hash reference; if it's a hash ref., it can't be an array ref.

    A simpler, non-redundant condition expression would be:
        ref $array eq 'ARRAY' && ref $array->[0] eq 'HASH'