Perl: Counting elements in a complex data structure

409 Views Asked by At

I am new to complex data structures. I kind of understand the idea behind them but am having some difficulty getting the data out. I found out the structure of my current problem child by using Data::Dumper....

$VAR1 = {
      '4' => {
               'engine_coded' => 0,
               'name' => 'FILTER_1',
               'filter_actions' => {
                                     'X_Override_Queue_Level' => 'Value'
                                   },
               'filter_criteria' => [
                                      [
                                        'X_Charge',
                                        '=',
                                        'X_CHARGE_1'
                                      ]
                                    ]
             }
    };

What I am needing to do is make sure that given a filter name ("4" in this case") that "name" has a value, as well as "filter_actions" and "filter_criteria".

Anyone have an idea how to best accomplish this? Many thanks! Janie

3

There are 3 best solutions below

4
On BEST ANSWER

Let's break this down into pieces...

First, create a function which validates a structure:

sub validate
{
    my ($data) = @_;

    # always return true for now
    return 1;
}

Now let's start filling in the bits... you want to use a filter name as part of the validation checks, so let's add that as an argument:

sub validate
{
    my ($data, $filter_name) = @_;

    # always return true for now
    return 1;
}

Before doing anything else, it would make sense to check if that filter name exists as a key; if it doesn't, validation has failed:

sub validate
{
    my ($data, $filter_name) = @_;

    return if not exists $data->{$filter_name};

    # otherwise, return true
    return 1;
}

Now also check that there is a value. Since definedness in a hash key is a superset of 'exists' (any value that is defined must also exist, but not every value that exists needs to be defined - as undef could be the value), the first check can be omitted:

sub validate
{
    my ($data, $filter_name) = @_;

    return if not defined $data->{$filter_name};

    # otherwise, return true
    return 1;
}

We've checked that the filter_name key is present in the data and it is defined, but before looking one level deeper, we need to confirm that it really is a hashref:

sub validate
{
    my ($data, $filter_name) = @_;

    return if not defined $data->{$filter_name};

    return if ref $data->{$filter_name} ne 'HASH';

    # otherwise, return true
    return 1;
}

Now look for the 'filter_actions' and 'filter_criteria' keys under the filter name:

sub validate
{
    my ($data, $filter_name) = @_;

    return if not defined $data->{$filter_name};

    return if ref $data->{$filter_name} ne 'HASH';

    return if not defined $data->{$filter_name}{filter_actions};
    return if not defined $data->{$filter_name}{filter_actions};

    # otherwise, return true
    return 1;
}

That's it! Be sure to read up on using perl data structures in perldoc perlreftoot, perldoc perlref, and perldoc perldsc.

0
On

You can access filter_actions/etc by checking $var->{4}->{filter_actions}. You might look at perldsc for a comprehensive overview of perl's data structures.

0
On

Here's my take. You are just checking to see if the data is there in the filter. If you wanted more structural validation that part comes later.

use List::Util qw<first>;

sub validate_filter { 
    my ( $filters_ref, $filter_name ) = @_;
    my $filter  = $filter_name ? $filters_ref->{$filter_name} : $filters_ref;
    return 1 unless 
        my $missing 
            = first { !!$filter->{ $_ } } 
              qw<name filter_actions filter_criteria>
       ;
    if ( $missing ) { 
        Carp::croak( '"Missing '$missing' in filter!" );
    }
}

Okay, my first thought was that it would accept the structure and the name, but of course is you know enough when you call

validate_filter( $filters, 4 );

you know enough to pass:

validate_filter( $filters->{4} );

So the parameter processing might not be the easiest to understand, but it makes sense in terms of parameter passing.

If you're after validating structure, you might choose this route. Based on your data, I show an example of a validation failure if a given filter_criteria cluster does not have a '=' operator every 3rd slot.

Like so:

use Carp       qw<croak>;
use List::Util qw<first>;
use Params::Util ();

sub _test { 
    return 1 if shift->( $_ );
    local $Carp::CarpLevel = $Carp::CarpLevel + 2;
    Carp::croak( shift );
}

my $validators 
    = { filter_actions => sub {
           croak 'filter_actions is not deinfed!' unless defined;
           _test( \&Params::Util::_HASH, 'filter_actions must be hash!' );
        }
      , filter_criters => sub {
            croak 'filter_criteria is not defined!' unless defined $crit;
            _test( \&Params::Util::_ARRAY, 'filter_criteria must be non-empty ARRAY!' );
            foreach ( @$crit ) { 
                _test( \&Params::Util::_ARRAY, 'criteria must be non-empty ARRAYs' );
                _test( sub { 
                           my $arr = shift;
                           return if @$arr % 3;
                           # return whether any slot in sequence is not '='
                           return !first { $arr->[$_] ne '=' } 
                                   # every 3 beginning at 1
                                   grep  { $_ % 3 == 1 } (1..$#$arr)
                                   ;
                       }
                     , 'criteria must be key-value pairs separated by equal sign!' 
                     );
            }
        }
    };

And this would change the validate_filter sub like so:

sub validate_filter { 
    my ( $filters_ref, $filter_name ) = @_;
    my $filter  = $filter_name ? $filters_ref->{$filter_name} : $filters_ref;
    return 1 unless 
        my $missing 
            = first { 
                  return 1 unless $filter->{ $_ };
                  return   unless my $validator = $validators->{ $_ };
                  local $_ = $filter->{ $_ };
                  return 1 if $validator->( $_ );
              } 
              qw<name filter_actions filter_criteria>
       ;
    if ( $missing ) { 
        Carp::croak( "Missing '$missing' in filter!" );
    }
}