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

Hey Monks,

I got a problem with my PERL program, more specific with direferencing. I think I got confused, because first I have to direference it into a Hash, then it says, it contains array elements, then again, hash elements. But let me ask you, what do you think.

$artfList = $ctf->PlanningAp->getArtifactListInPLanningFolder($session +,$parent,$filters,$recursive); %artfList =%$artfList; foreach $element (keys %artfList){ print "$element\n"; #outpus is: dataRows print "$artfList{dataRows}\n"; #output is: ARRAY(0x2139d) } @Array = @{$artfList{dataRows}}; foreach $element (@Array){ print "$element\n"; # output is: ArtifactsInPlanningFolderSoapRow += HASH(0x3a3acf0) }

And here is where I got stucked. What I notice is, that there's again a HASH reference. How to direference that array now back to hash?

This procedure should return me Artifact List (artf000,artf001,etc). The procedure is taken from online notes and that's how the return value is described.

There are NO syntax errors if you might find ones in the above code, since I typed it over and not copy pasted!

DUMPER OUTPUT:

Alot of infromation from the Database that I am trying to access, it's even too much to copy and some information of company I'd rather not display. But I dont know how to access elements, I'll try to retype structure:

bless( { 'priority' => '1', 'id' => 'artf0000', 'category' => 'V0B' }, 'ArtifactsInPlanningFolderSoapRow' ) # and it

Hope you guys can scrumble through this. The above code repeats itself for each different artifact.

Please assist me,

David

Replies are listed 'Best First'.
Re: Direferencing problem
by McA (Priest) on Jul 30, 2014 at 08:09 UTC

    Hi David,

    the output shows that $element is a blessed hash reference, which means it's an object in the Perl world. So, there are two possibilities to extract data from it.

    • The urgly way: Just treat it like a simple hash ref, e.g. my $category = $element->{'category'};.
    • Look around in the code base and search for the package called ArtifactsInPlanningFolderSoapRow. Therein you probably find the right way to access these object attributes in a way intended by the author of that package.

    Regards
    McA

Re: Direferencing problem
by CountZero (Bishop) on Jul 30, 2014 at 08:13 UTC
    Your $element is a hash reference and to make it into a hash, just prepend it with the % sigil, like this: %$element.

    Quite likely you can then access it as follows:

    my %low_level_hash = %$element; my $priority = $low_level_hash{'priority'}; my $id = $low_level_hash{'id'}; my $category = $low_level_hash{'category'};

    From the dumper output it appears that this low level hash structure is actually an object ('ArtifactsInPlanningFolderSoapRow'). Doesn't the class for this object provide you with getter and setter methods?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      Please check the above reply, to another post, I provided a LINK to the FAQ about that Object. Maybe you can understand it better, how to get that List.
        The FAQ is not really helpful as it is written for Java. But somehow the data you received are in the form of a Perl-object so there must be an interface, quite probably a module that implements the class of these objects. Checking the documentation of this module might bring you further.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
Re: Direferencing problem
by tangent (Parson) on Jul 30, 2014 at 13:39 UTC
    If the (Java) link you pointed to is actually the documentation for your Perl classes then this page gives you a list of methods that can be called on the ArtifactsInPlanningFolderSoapRow object. No harm in trying it - something like this:
    # $artfList - a hash reference my $array = $artfList->{dataRows}; for my $row_object (@$array) { my $id = $row_object->getId; my $category = $row_object->getCategory; print "ID: $id, Category: $category\n"; }
    Update: if the above works this is how you could get the list of IDs:
    my @ids; for my $row_object (@$array) { push( @ids, $row_object->getId ); }
    Or if you want to be fancy and do it all in one line:
    my @ids = map { $_->getId } @{ $artfList->{dataRows} };
Re: Direferencing problem
by Anonymous Monk on Jul 30, 2014 at 07:29 UTC

    If you want help instead of $ctf->PlanningAp post the output of  Data::Dump::dd( $artfList )

    That way we can run the program, and fix it, and you can run this short program and believe it :)

      I would gladly do that, but unfortunately, I am a student working for a firm and I dont have the priviliges (rights) to install CPAN modules.. and the Data::Dump is not pre-installed with it. So I'd have to ask someone with right, but my boss is on vacation.

      Will see what I can do about that. But in the meantime, I posted the output of $artfList:

      #Outpus of $artfList: #ArftifactInPlanningFolderSoapList=HASH(0x27d8630)

      Hope, this will be helpful enough

        As an alternative, Data::Dumper is unlikely to be removed and will also give you an overview of your data structure.

        Hi,

        Data::Dumper is part of the perl core and should be available for you to solve the same problem.

        use Data::Dumper; ... print Dumper($element), "\n";

        Regards
        McA

Re: Direferencing problem
by David92 (Sexton) on Jul 30, 2014 at 08:03 UTC

    I added the DUMPER OUTPUT

      The Dumper output shows us that you get an ArtifactsInPlanningFolderSoapRow-object in $element.
      To get what you want, you'll have to access the id-attribute, something like:
      print $element->{id};

      Greetings,
      Janek Schleicher

Re: Dereferencing problem
by Anonymous Monk on Jul 30, 2014 at 15:58 UTC
    direferencing?

    Try dEreferencing. Seriously. Take the time to be correct. Your browser probably even had a red line under that atrocious attempt at spelling. Multiple times even.

      direferencing?
      Try dEreferencing. Seriously. Take the time to be correct. Your browser probably even had a red line under that atrocious attempt at spelling. Multiple times even.

      Take the time to , find something better to do, not mention already mentioned stuff in a snarky manner

      Dear Monks, how does a node like the parent end up with a reputation of currently 4 instead of being reaped for being a simple, lazy flame?

        Dear Monks, how does a node like the parent end up with a reputation of currently 4 instead of being reaped for being a simple, lazy flame?

        Wrong place to ask the question, pmdiscuss is what youwant ... yes, I troll the trolls