in reply to Re: Parsing hash from XML::Simple
in thread Parsing hash from XML::Simple

I am getting an error:
Can't use string ("visibility") as a HASH ref while "strict refs" in u +se at
Using a loop:
foreach my $vals (keys %{$xml}) { print $vals->{city}{name}, "\n"; }

Replies are listed 'Best First'.
Re^3: Parsing hash from XML::Simple
by NetWallah (Canon) on May 16, 2016 at 19:11 UTC
    Change "keys %{$xml}" to "values %$xml".

    UPDATE2: Added quotes around "values %$xml" .. apparently the OP missed the "values" keyword. My bad for assuming the meaning was clear.

    Alternatively, (if you want to keep using keys), try

    print $xml->{$vals}{city}{name}
    Update: The above assumes you have a HOH containing info on multiple cities.

    Your posted DUMP contains only ONE city. For that case, the "for loop" and "keys" does not make sense. simply use $xml->{city}{name}.

            This is not an optical illusion, it just looks like one.

      I know, but it will be multiple cities. And using this I am still geting the same error
      Can't use string ("visibility") as a HASH ref while "strict refs" in use at...
      with:
      foreach my $vals (%{$xml}) { print $vals->{city}{name}, "\n"; }
      When you say "does not make sense" do you mean it will also not run?/code foreach my $vals (%{$xml}) { print $vals-

        Please note that NetWallah suggested

        foreach my $vals (values %{ $xml }) {

        and not what you wrote:

        foreach my $vals (%{$xml}) {

        The two are distinctly different.

        When you say "does not make sense" do you mean it will also not run?/
        The data structure you posted contains only one hashref (with substructures). It does not make sense to run a foreach loop on that hashref, since you can access the data by direct dereferencing.

        We can only assume that this hashref is part of a bigger structure on which you would want to loop, but you don't show the larger structure, so that we can only make guesses.

      Thanks for the information, but unfortunately none of the answers helped me. I know I only posted one block of code for only one city, it will be more, but can this one block of code be used to show what I am trying to do? Can it still be looped and display the value of city->name? I tried you suggestion but if I run it it gives me this:
      Use of uninitialized value in print at...

      And here is the code as you posted
      ... foreach my $vals (values %{ $xml }) { print Dumper $vals->{city}{name}, "\n"; }
      Thanks for trying to help!

        You will need to learn about data structures and how to traverse them. There are already replies with links to perldsc and maybe other tutorials.

        If you refuse to show us the data structure, how do you expect us to give you code that will traverse an unknown data structure?

        If you get errors while traversing your data structure, that usually means that your data structure is not what you think it is.

        Please also note that XML::Simple is a bad choice for ingesting data which may or may not have multiple elements somewhere in its structure. See also the ForceArray parameter in its documentation. But again, this boils down to you not showing the relevant parts of your data structure and your incoming data.

        Until you understand your data structure (by using Data::Dumper), and/or understand what parts of your data structure are arrays and what parts of your data structure are hashes, there is little we can do to help you.

        The built-in perl debugger is particularly useful in learning, and testing data structure access.

        Run like this:

        perl -d your-program-name.pl
        It has a "h" (help) command to get you started.

        Basically, you will be using the "x" (Examine) command to look at the data.

        See perldebug for details.

                This is not an optical illusion, it just looks like one.