0

I want to ask you how can I read the specific hash from array [{},{},{}] using string key?

  1. First is an initialization
  2. There are 2x writing hashes to array
  3. I need to read the particular hash using string key

How can I do that?

sub SpecComp {
    my ($this, $comp_name, $component, $index) = @_;
    if ($component) {
        $this->{specComp}[$index] =  $component;
    }
    my $ref = \$this; #?????

    return $ref; #?????
} 

sub new {
    my $this = {};
    my $this->{specComp} = [];

    return $this;
}

#  Initialize
my $ts = new test();

#  I want to write - it is OK
my $Comp1 = {      
    "ANOZZLE" => "first",
};

$ts->SpecComp("ANOZZLE", $Comp1, 1);

my $Comp2 = {      
    "BBUCKET" => "second",
};

$ts->SpecComp("BBUCKET", $Comp2, 2);

#  I want to read - is not OK

my $out = SpecComp("ANOZZLE");

print $out; # ???????? I want output $Comp1 
2
  • Where is test coming from? Why are you using new without bless? Commented Jun 1, 2020 at 19:55
  • I've reformatted your code to fix the indentation and add some whitespace. You're very welcome, but please consider doing it yourself in the future. Careful indentation makes code far easier to read and understand and if you're asking a large group of people to read and understand your code, then it's only polite to make it as easy as possible for them. Commented Jun 1, 2020 at 20:07

1 Answer 1

1

When using Object Orientation, use bless to actually bind a reference to a class.

To output a structure, use Data::Dumper.

It's possible to find an element in an array using grep (see below).

#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

{   package My::Object;

    sub new {
        bless { specComp => [] }, shift
    }

    sub SpecComp {
        my ($self, $component, $index) = @_;
        if ($index) {
            $self->{specComp}[$index] =  $component;
        } else {
            return (grep $_ && exists $_->{$component},
                    @{ $self->{specComp} })[0];
        }
    }
}

my $ts = 'My::Object'->new;
my $Comp1 = {
    ANOZZLE => 'first',
};
$ts->SpecComp($Comp1, 1);

my $Comp2 = {
    BBUCKET => 'second',
};
$ts->SpecComp($Comp2, 2);

my $out = $ts->SpecComp('ANOZZLE');
print Dumper($out);

But do you really need to store the hash references in an array? You don't show why you need it, and using just plain hash would make retrieval much easier.

#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

{   package My::Object;

    sub new {
        bless { specComp => {} }, shift
    }

    sub SpecComp {
        my ($self, $component) = @_;
        if (ref $component) {
            my ($key) = keys %$component;
            $self->{specComp}{$key} = $component->{$key};
            $self->{index}{$key} = ++$self->{INDEX};
        } else {
            return { $component => $self->{specComp}{$component} }
        }
    }
}

my $ts = 'My::Object'->new;
my $Comp1 = {
    ANOZZLE => 'first',
};
$ts->SpecComp($Comp1);

my $Comp2 = {
    BBUCKET => 'second',
};
$ts->SpecComp($Comp2);

my $out = $ts->SpecComp('ANOZZLE');
print Dumper($out);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.