On 04/13/2016 08:29 PM, Kenneth Wolcott wrote:
Hi;

   I have the following output from Data::Dumper and I want to extract
the first string that the "Id" name points to.

$VAR1 = [
           bless( {
                    'Id' => [
                            '01tC0000003udXAIAY',
                            '01tC0000003udXAIAY'
                          ],
                    'type' => 'Product2'
                  }, 'sObject' )
         ];

   So if the data structure is contained in a Perl variable called
$data, then I think that this is a reference to a hash.

   So I need to do something like "@{$data}" to get to the hash.

   But I want the "Id" element of the hash so I want something like
@{$data}->{'Id'}

   But that's the array, so what about ${@{$data}->{'Id'}}[0]

   But that isn't right either.

   I'm either getting an undefined reference to a hash error or not a
scalar reference error.

Thanks,
Ken Wolcott


First - consider if you should be walking the object implementation at all -- In a well engineered class, the class "sObject" should include a mechanism to get to its internal parts.

The way presented -- $VAR1 (or $data if you prefer) is actually an arrayref with one element, the object containing a hash, with two elements. At key 'type' is a scalar with the value 'Product2' and at 'Id' is an arrayref of two elements.

(It is possible that the in calling Data::Dumper, you inadvertently added the outermost arrayref)

So - to get to the first object, you dereference $data->[0] (also written less attractively as $$data[0])

my $object = $data->[0];

To get to the referent of 'Id' you would deref the object using

my $arrayref = $object->{Id}  # equivalent to $$object{Id}


Then, to dereference the arrayref, you could turn it back into an array

my @id = @$arrayref;

Of course, you could do that all in one fell swoop:

my @id = @{ $data->[0]{Id} };


Or -- if you prefer the superfluous extra-dollar-sign notation

 my @id = @{${$$data[0]}{Id}};


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to