0

I'm not familiar with perl and am trying to edit an irssi translation script. The result from a web request returns as:

$result = {
 "data" => {
  "translations" => [
   {
    "translatedText" => "Halloween"
   }
  ]
 }
}

How can I fetch only the translatedText portion, so that

$string = 'Halloween'

Thanks.

2 Answers 2

7

"Halloween" could be obtained as:

$result->{"data"}->{"translations"}->[0]->{"translatedText"}

The arrows after the first one can be omitted, so an even shorter variant would be:

$result->{"data"}{"translations"}[0]{"translatedText"}

Basically you have multiple indirections at different levels:

  • reference to a hash
  • its "data" key being a reference to another hash
  • the "translations" key of the last hash being a reference to an array
  • the first element of that array is a reference to a hash
  • the "translatedText" key of that hash is a string
Sign up to request clarification or add additional context in comments.

Comments

2

That would be

$result->{data}->{translations}->[0]->{translatedText};

$result is a hash ref. The key 'data' points at yet another hash ref, which has a key 'translations' pointing at an array ref. The first and only element in that array ref has a key 'translatedText' which points at the data of interest: 'Halloween'.

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.