Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I have a quite complicated object (for various reason it has to be like this) and by using this example I want to ask how to do to specific tasks. I hope it will be easy to understand.
#Created by using; push @{ $Second{Company}->{Identifier} }, Element;
%Hash -> {Company_A} -> { Identifier_1 } -> One or more 'Elements' e +xample (a, b) -> { Identifier_2 } -> One or more 'Elements' e +xample (b, g) -> { Identifier_3 } -> One or more 'Elements' e +xample (c) -> {Company_B} -> { Identifier_1 } -> One or more 'Elements' e +xample (a) -> { Identifier_2 } -> One or more 'Elements' e +xample (g) -> { Identifier_3 } -> One or more 'Elements' e +xample (x)
1) I want to know how many 'b' elements is there in Company_A. Usually I would use something like map { $Count+= 1 if 'a' eq values %Hash{Company_A}{$_} } values %Hash{Company_A}; but it will only access the first element of each Identifier. How can I write a map command to go through every element?
2) When I need to print this hash I have to look if each identifier has one or more elements (scalar) and fork the prints to print out everything in line by line case (foreach and use count++). Amount of elements vary and there is a lot of data inside so I don't want to create duplicate entries</pp>
scalar () if more than 1 { foreach $Company_A{Identifier_1}[$Count] $Company_A{Identifier_2}[$Count] $Company_A{Identifier_3} $Count++ } if = 1 { $Company_B{Identifier_1} $Company_B{Identifier_2} $Company_B{Identifier_3} } Ouptut: Company_A a, b, c Company_A b, g, c Company_B a, g, x
Some Identifiers have more elements (always equal amount) and other have only one. I need to print the ones with multiple elements separately - they share the elements which only appear once. Because I am doing it for separate objects I was wondering if there is a way to print everything in one block with a single command?
Thanks so much for any tips. Humble apprentice
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Searching and printing complex data structures
by hdb (Monsignor) on Apr 10, 2013 at 19:22 UTC | |
by Anonymous Monk on Apr 10, 2013 at 19:26 UTC | |
|
Re: Searching and printing complex data structures
by Cristoforo (Curate) on Apr 10, 2013 at 19:10 UTC | |
by Anonymous Monk on Apr 10, 2013 at 19:17 UTC | |
|
Re: Searching and printing complex data structures
by aitap (Curate) on Apr 10, 2013 at 18:22 UTC | |
|
Re: Searching and printing complex data structures
by jethro (Monsignor) on Apr 10, 2013 at 18:24 UTC |