0

I iterate through files in Perl and I'd like to get the correct "Adresse" field in the file. "Adresse" is a hash. Either the file contains only one "Adresse", and I take it, or it contains several "Adresse" and "Adresse" is actually an Array containing several "Adresse", and I just need the one having "type" = "postale". Here is my code:

  my $ad;
  my $adresse;
  if(ref($doc->{'Organisme'}->{'Adresse'}) eq 'ARRAY') {
    print "\nI'M AN ARRAY!\n";
    foreach $ad ($doc->{'Organisme'}->{'Adresse'}) {
      print Dumper $ad;
      if ($ad->{'type'} == 'postale') {
       my $adresse = $ad;
      }
    }
  } else {
    my $adresse = $doc->{'Organisme'}->{'Adresse'}
  }
  print $fd $adresse->{'Ligne'};

I get the error:

Not a HASH reference at ./scripts/actualiserDonnees.pl line 35

and line 35 is:

if ($ad->{'type'} == 'postale') {

Apparently the "foreach" doens't iterate through "$doc->{'Organisme'}->{'Adresse'}" when the latter is an array, because the Dumper gives me this:

$VAR1 = [
          {
            'Localisation' => {
                                "Pr\x{e9}cision" => '8',
                                'Longitude' => '1.9751304',
                                'Latitude' => '43.2279035'
                              },
            'type' => 'physique',
            'CodePostal' => '11270',
            "Accessibilit\x{e9}" => {
                                      'type' => 'ACC'
                                    },
            'NomCommune' => 'Laurac',
            'Ligne' => 'Place Blanche-de-Laurac'
          },
          {
            'Ligne' => '8 rue du Pont',
            'CodePostal' => '11270',
            'type' => 'postale',
            'NomCommune' => 'Laurac'
          }
        ];

If I didn't explain myself enough, feel free to ask questions. Thanks in advance :)

4
  • try to remove the braces from ($doc->{'Organisme'}->{'Adresse'}), maybe perl treats this as a list of one Array, Ah long time since i used Perl, shouldn't it be @ad? Commented Mar 16, 2020 at 6:57
  • Sorry, I meant %ad Commented Mar 16, 2020 at 7:08
  • I tried this, but actually the solution given below by ikegami is correct. Commented Mar 16, 2020 at 8:25
  • I was the first to give it an upvote :-) Commented Mar 16, 2020 at 8:27

1 Answer 1

3

my $adresse creates a new variable. Replace both instances of my $adresse = ... with $adresse = ...


$doc->{'Organisme'}->{'Adresse'} is a scalar (a reference to an array), so foreach $ad ($doc->{'Organisme'}->{'Adresse'}) only loops over one item (the reference to an array). You want foreach $ad (@{ $doc->{'Organisme'}->{'Adresse'} })

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, the "my" was an copy/paste error, I didn't have it in my code, which was more complicated. The "@" thing did the trick. I tried @$doc before that, now thanks to you I understand more why I've been wrong.
or in perl 5.24 or later, foreach $ad ($doc->{'Organisme'}{'Adresse'}->@*)

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.