Ask a Question related to PERL Miscellaneous, Design and Development.
-
Mothra #1
Printing a hash of hashes using an array for the headings and getting the columns to line up
Hi All,
I am trying to print out a report using a Hash of Hashes and am having
trouble
getting the columns to print out correctly. I have an array that contains
the column
headings in the correct order but I am stuck as how to get the HoH to use
this information. I have provided a test script as to what I have tried.
How can I get the script to print the columns in the correct order.
Thanks
Mothra
#!/app/perl5.8.0/bin/perl
use strict;
use warnings;
my @family = qw(UNIGRAPHICS_NX UNIGRAPHICS SOLID_EDGE WEBTOOLS other);
my %people = (
'wattsl' => {
'SOLID_EDGE' => 3,
'WEBTOOLS' => 10,
'UNIGRAPHICS' => 19,
'UNIGRAPHICS_NX' => 57
},
'friasd' => {
'other' => 2,
'SOLID_EDGE' => 10,
'WEBTOOLS' => 25,
'UNIGRAPHICS' => 21,
'UNIGRAPHICS_NX' => 81
},
'yamaguch' => {
'other' => 4,
'SOLID_EDGE' => 12,
'WEBTOOLS' => 11,
'UNIGRAPHICS' => 9,
'UNIGRAPHICS_NX' => 56
},
'jung' => {
'other' => 2,
'SOLID_EDGE' => 5,
'WEBTOOLS' => 16,
'UNIGRAPHICS' => 13,
'UNIGRAPHICS_NX' => 39
},
'riches' => {
'other' => 2,
'SOLID_EDGE' => 18,
'WEBTOOLS' => 24,
'UNIGRAPHICS' => 10,
'UNIGRAPHICS_NX' => 83
},
);
printf "%24s %10s %10s %8s %5s \n", @family;
foreach my $person( sort keys %people ) {
printf "%9s %2d %2d %2d %2d %2d\n", $person,
map { $people{$person}{$_} } sort keys %{ $people{$person} };
}
Mothra Guest
-
hash of hashes
Hi all. How can I create $Subject for future population? -- Yours truly, WBR, Paul Argentoff. Jabber: paul@jabber.rtelekom.ru RIPE: PA1291-RIPE -
XML parse - hash of hashes
Greetings, I am fairly new to Perl and to XML and I’m trying to (1) parse a XML document (snippet attached), (2) update specified data using a... -
Printing Array of Hashes
Hi John, I received your code. Thanks. I would like to know how to check the values of the keys in the hash. I checked the Perl cookbook and... -
Accesing hash of hashes
Hello: I am trying to create and access a multidimensional hash. For example the following works ====== $route {$routeDest} = $cost ; print... -
Sort a hash based on values in the hash stored as arrays of hashes
Hmm. I'm not quite sure if I got the subject right, but I'll try to explain. :-) I've got a hash of elements stored like this: $VAR1 = {... -
David K. Wall #2
Re: Printing a hash of hashes using an array for the headings and getting the columns to line up
Mothra <mothra@nowhereatall.com> wrote:
# Add this to construct a format strinf for printf()> I am trying to print out a report using a Hash of Hashes and am
> having
> trouble
> getting the columns to print out correctly. I have an array that
> contains the column
> headings in the correct order but I am stuck as how to get the HoH
> to use this information. I have provided a test script as to what
> I have tried. How can I get the script to print the columns in the
> correct order.
>
> Thanks
>
> Mothra
>
>
>
> #!/app/perl5.8.0/bin/perl
> use strict;
> use warnings;
>
> my @family = qw(UNIGRAPHICS_NX UNIGRAPHICS SOLID_EDGE WEBTOOLS
> other);
my $form_str = '%9s ' . join '', map '%' . (1 + length $_) . 's'
=> @family;
printf "$form_str\n", ' ', @family;>
> my %people = (
> 'wattsl' => {
> 'SOLID_EDGE' => 3,
> 'WEBTOOLS' => 10,
> 'UNIGRAPHICS' => 19,
> 'UNIGRAPHICS_NX' => 57
> },
> 'friasd' => {
> 'other' => 2,
> 'SOLID_EDGE' => 10,
> 'WEBTOOLS' => 25,
> 'UNIGRAPHICS' => 21,
> 'UNIGRAPHICS_NX' => 81
> },
> 'yamaguch' => {
> 'other' => 4,
> 'SOLID_EDGE' => 12,
> 'WEBTOOLS' => 11,
> 'UNIGRAPHICS' => 9,
> 'UNIGRAPHICS_NX' => 56
> },
> 'jung' => {
> 'other' => 2,
> 'SOLID_EDGE' => 5,
> 'WEBTOOLS' => 16,
> 'UNIGRAPHICS' => 13,
> 'UNIGRAPHICS_NX' => 39
> },
> 'riches' => {
> 'other' => 2,
> 'SOLID_EDGE' => 18,
> 'WEBTOOLS' => 24,
> 'UNIGRAPHICS' => 10,
> 'UNIGRAPHICS_NX' => 83
> },
>
> );
>
> printf "%24s %10s %10s %8s %5s \n", @family;
>
> foreach my $person( sort keys %people ) {
>
> printf "%9s %2d %2d %2d %2d %2d\n",
> $person,
> map { $people{$person}{$_} } sort keys %{ $people{$person}
> };
>
>}
foreach my $person( sort keys %people ) {
printf "$form_str\n", $person,
map { $people{$person}{$_} } @family;
}
--
David Wall
David K. Wall Guest
-
Mothra #3
Re: Printing a hash of hashes using an array for the headings and getting the columns to line up
Hi David,
"David K. Wall" <usenet@dwall.fastmail.fm> wrote in message
news:Xns93F27CE764669dkwwashere@216.168.3.30...[snipped]> Mothra <mothra@nowhereatall.com> wrote:
>
Thanks for replying :)
Ok, generate a form.> # Add this to construct a format strinf for printf()
>
> my $form_str = '%9s ' . join '', map '%' . (1 + length $_) . 's'
> => @family;
>
>
[more snippage]
I added this and receive:> printf "$form_str\n", ' ', @family;
>
> foreach my $person( sort keys %people ) {
> printf "$form_str\n", $person,
> map { $people{$person}{$_} } @family;
> }
>
Use of uninitialized value in printf at F:\scripts\rep.pl line 52.
I think this is because of the wattsl value is missing the
'other' key value. After checking the orginal test script I found
that I was forcing a %2d in the format line.
I went back and reran the test script I orginally wrote and did not
receive the uninitialized value warning. I wonder why I did not receive
a warning? Any way, for now do you know of a good
way to remove the warning? (other that commenting out the use warnings
line).
Thanks
Mothra
Mothra Guest
-
David K. Wall #4
Re: Printing a hash of hashes using an array for the headings and getting the columns to line up
Mothra <mothra@nowhereatall.com> wrote:
I noticed the missing value and got the same warning message, but>"David K. Wall" <usenet@dwall.fastmail.fm> wrote in message
>news:Xns93F27CE764669dkwwashere@216.168.3.30...> I added this and receive:>>
>> printf "$form_str\n", ' ', @family;
>>
>> foreach my $person( sort keys %people ) {
>> printf "$form_str\n", $person,
>> map { $people{$person}{$_} } @family;
>> }
>>
> Use of uninitialized value in printf at F:\scripts\rep.pl line 52.
since you didn't seem to care I didn't either.
foreach my $person( sort keys %people ) {
printf "$form_str\n", $person,
map { $people{$person}{$_} or 0 } @family;
}
Replace the '0' with whatever value is appropriate.
--
David Wall
David K. Wall Guest
-
John W. Krahn #5
Re: Printing a hash of hashes using an array for the headings andgetting the columns to line up
Mothra wrote:
>
> I am trying to print out a report using a Hash of Hashes and am having
> trouble getting the columns to print out correctly. I have an array that
> contains the column headings in the correct order but I am stuck as how
> to get the HoH to use this information. I have provided a test script as
> to what I have tried. How can I get the script to print the columns in
> the correct order.
>
> [snip]
>
> my @family = qw(UNIGRAPHICS_NX UNIGRAPHICS SOLID_EDGE WEBTOOLS other);
>
> [snip]
>
> printf "%24s %10s %10s %8s %5s \n", @family;
>
> foreach my $person( sort keys %people ) {
>
> printf "%9s %2d %2d %2d %2d %2d\n", $person,
> map { $people{$person}{$_} } sort keys %{ $people{$person} };
> }
printf "%9s %2d %2d %2d %2d %2d\n",
$person, @{ $people{ $person } }{ @family };
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Mothra #6
Re: Printing a hash of hashes using an array for the headings and getting the columns to line up
"David K. Wall" <usenet@dwall.fastmail.fm> wrote in message
news:Xns93F29150BF9E5dkwwashere@216.168.3.30...[snipped]> Mothra <mothra@nowhereatall.com> wrote:
>> >"David K. Wall" <usenet@dwall.fastmail.fm> wrote in message
> >news:Xns93F27CE764669dkwwashere@216.168.3.30...> >>Hmn, now I am confused, how did you come to the conclusion that I didn't>
> I noticed the missing value and got the same warning message, but
> since you didn't seem to care I didn't either.
seem to care?
In my orginal post the code I posted did not generate the warning.
Yep!! this works great!!!>
>
> foreach my $person( sort keys %people ) {
> printf "$form_str\n", $person,
> map { $people{$person}{$_} or 0 } @family;
> }
>
> Replace the '0' with whatever value is appropriate.
>
Thanks very much!!
Mothra
Mothra Guest
-
David K. Wall #7
Re: Printing a hash of hashes using an array for the headings and getting the columns to line up
Mothra <mothra@nowhereatall.com> wrote:
I copied, pasted, and ran the original code unaltered, and got this>
> "David K. Wall" <usenet@dwall.fastmail.fm> wrote in message
> news:Xns93F29150BF9E5dkwwashere@216.168.3.30...> [snipped]>> Mothra <mothra@nowhereatall.com> wrote:
>>>> >"David K. Wall" <usenet@dwall.fastmail.fm> wrote in message
>> >news:Xns93F27CE764669dkwwashere@216.168.3.30...
>> >>>>>
>> I noticed the missing value and got the same warning message, but
>> since you didn't seem to care I didn't either.
> Hmn, now I am confused, how did you come to the conclusion that I
> didn't seem to care?
> In my orginal post the code I posted did not generate the warning.
output:
Use of uninitialized value in printf at E:\perlprog\junk.pl line 50.
UNIGRAPHICS_NX UNIGRAPHICS SOLID_EDGE WEBTOOLS other
friasd 10 21 81 25 2
jung 5 13 39 16 2
riches 18 10 83 24 2
wattsl 3 19 57 10 0
yamaguch 12 9 56 11 4
Looks like a warning to me.
You're welcome.>> Yep!! this works great!!!>>
>>
>> foreach my $person( sort keys %people ) {
>> printf "$form_str\n", $person,
>> map { $people{$person}{$_} or 0 } @family;
>> }
>>
>> Replace the '0' with whatever value is appropriate.
>>
> Thanks very much!!
--
David Wall
David K. Wall Guest
-
Mothra #8
Re: Printing a hash of hashes using an array for the headings and getting the columns to line up
"David K. Wall" <usenet@dwall.fastmail.fm> wrote in message
news:Xns93F29F04AC5A9dkwwashere@216.168.3.30...[snipped]> Mothra <mothra@nowhereatall.com> wrote:
>>> >
> > "David K. Wall" <usenet@dwall.fastmail.fm> wrote in message
> > news:Xns93F29150BF9E5dkwwashere@216.168.3.30...> > [snipped]> >> Mothra <mothra@nowhereatall.com> wrote:
> >>
> >> >"David K. Wall" <usenet@dwall.fastmail.fm> wrote in message
> >> >news:Xns93F27CE764669dkwwashere@216.168.3.30...
> >> >>> >> >>
> >> I noticed the missing value and got the same warning message, but
> >> since you didn't seem to care I didn't either.
> > Hmn, now I am confused, how did you come to the conclusion that I
> > didn't seem to care?
> > In my orginal post the code I posted did not generate the warning.
> I copied, pasted, and ran the original code unaltered, and got this
> output:
>
> Use of uninitialized value in printf at E:\perlprog\junk.pl line 50.
I think I know what is going on.
F:\scripts>x.pl
UNIGRAPHICS_NX UNIGRAPHICS SOLID_EDGE WEBTOOLS other
friasd 10 21 81 25 2
jung 5 13 39 16 2
riches 18 10 83 24 2
wattsl 3 19 57 10 0
yamaguch 12 9 56 11 4
F:\scripts>perl -v
This is perl, v5.6.1 built for MSWin32-x86-multi-thread
(with 1 registered patch, see perl -V for more detail)
I knew I was not going crazy.
then this.
F:\scripts>x.pl
UNIGRAPHICS_NX UNIGRAPHICS SOLID_EDGE WEBTOOLS other
friasd 10 21 81 25 2
jung 5 13 39 16 2
riches 18 10 83 24 2
Use of uninitialized value in printf at F:\scripts\x.pl line 50.
wattsl 3 19 57 10 0
yamaguch 12 9 56 11 4
F:\scripts>perl -v
This is perl, v5.8.0 built for MSWin32-x86-multi-thread
I guess I need to use the latest version of perl, Sorry about that :)
Thanks
Mothra
Mothra Guest
-
Anno Siegel #9
Re: Printing a hash of hashes using an array for the headings and getting the columns to line up
Mothra <mothra@nowhereatall.com> wrote in comp.lang.perl.misc:
I can't resist the temptation to plug my module Text::Table. Here> Hi All,
>
> I am trying to print out a report using a Hash of Hashes and am having
> trouble
> getting the columns to print out correctly. I have an array that contains
> the column
> headings in the correct order but I am stuck as how to get the HoH to use
> this information. I have provided a test script as to what I have tried.
> How can I get the script to print the columns in the correct order.
>
> Thanks
>
> Mothra
>
>
>
> #!/app/perl5.8.0/bin/perl
> use strict;
> use warnings;
>
> my @family = qw(UNIGRAPHICS_NX UNIGRAPHICS SOLID_EDGE WEBTOOLS other);
>
> my %people = (
> 'wattsl' => {
> 'SOLID_EDGE' => 3,
> 'WEBTOOLS' => 10,
> 'UNIGRAPHICS' => 19,
> 'UNIGRAPHICS_NX' => 57
> },
> 'friasd' => {
> 'other' => 2,
> 'SOLID_EDGE' => 10,
> 'WEBTOOLS' => 25,
> 'UNIGRAPHICS' => 21,
> 'UNIGRAPHICS_NX' => 81
> },
> 'yamaguch' => {
> 'other' => 4,
> 'SOLID_EDGE' => 12,
> 'WEBTOOLS' => 11,
> 'UNIGRAPHICS' => 9,
> 'UNIGRAPHICS_NX' => 56
> },
> 'jung' => {
> 'other' => 2,
> 'SOLID_EDGE' => 5,
> 'WEBTOOLS' => 16,
> 'UNIGRAPHICS' => 13,
> 'UNIGRAPHICS_NX' => 39
> },
> 'riches' => {
> 'other' => 2,
> 'SOLID_EDGE' => 18,
> 'WEBTOOLS' => 24,
> 'UNIGRAPHICS' => 10,
> 'UNIGRAPHICS_NX' => 83
> },
>
> );
>
> printf "%24s %10s %10s %8s %5s \n", @family;
>
> foreach my $person( sort keys %people ) {
>
> printf "%9s %2d %2d %2d %2d %2d\n", $person,
> map { $people{$person}{$_} } sort keys %{ $people{$person} };
>
> }
is how:
use Text::Table;
my $tb = Text::Table->new( 'name', @family);
$tb->add( $_, @{ $people{ $_}}{ @family}) for sort keys %people;
print $tb;
The advantage is that it figures out the alignment for you, that is,
you don't have to write and maintain the printf format.
Anno
Anno Siegel Guest
-
Mothra #10
Re: Printing a hash of hashes using an array for the headings and getting the columns to line up
Hi Anno,
"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
news:bjpp9q$7o$4@mamenchi.zrz.TU-Berlin.DE...
[snipped]Oh, that is cool!!!>
> I can't resist the temptation to plug my module Text::Table. Here
> is how:
>
> use Text::Table;
>
> my $tb = Text::Table->new( 'name', @family);
> $tb->add( $_, @{ $people{ $_}}{ @family}) for sort keys %people;
>
> print $tb;
>
> The advantage is that it figures out the alignment for you, that is,
> you don't have to write and maintain the printf format.
>
Off to CPAN I go :-)
Mothra
Mothra Guest
-
David K. Wall #11
Re: Printing a hash of hashes using an array for the headings and getting the columns to line up
Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
I like it, and I have a project at hand for which it is very useful.> I can't resist the temptation to plug my module Text::Table. Here
> is how:
>
> use Text::Table;
>
> my $tb = Text::Table->new( 'name', @family);
> $tb->add( $_, @{ $people{ $_}}{ @family}) for sort keys
> %people;
>
> print $tb;
>
> The advantage is that it figures out the alignment for you, that
> is, you don't have to write and maintain the printf format.
Thank you!
But there's one thing I haven't been able to figure out, and that's
how to right-align a column title. For example,
use Text::Table;
my $tb = Text::Table->new("TLA");
$tb->load( qw(188.52 0.00 97.36 185.51) );
print $tb;
produces
TLA
188.52
0.00
97.36
185.51
What I want to produce is
TLA
188.52
0.00
97.36
185.51
Maybe I'm being obtuse or haven't read the docs closely enough, but I
don't see how to do this.
--
David Wall
David K. Wall Guest
-
Anno Siegel #12
Re: Printing a hash of hashes using an array for the headings and getting the columns to line up
David K. Wall <usenet@dwall.fastmail.fm> wrote in comp.lang.perl.misc:
That's because you can't. Sorry.> Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
>>> > I can't resist the temptation to plug my module Text::Table. Here
> > is how:
> >
> > use Text::Table;
> >
> > my $tb = Text::Table->new( 'name', @family);
> > $tb->add( $_, @{ $people{ $_}}{ @family}) for sort keys
> > %people;
> >
> > print $tb;
> >
> > The advantage is that it figures out the alignment for you, that
> > is, you don't have to write and maintain the printf format.
> I like it, and I have a project at hand for which it is very useful.
> Thank you!
>
> But there's one thing I haven't been able to figure out, and that's
> how to right-align a column title. For example,
>
> use Text::Table;
> my $tb = Text::Table->new("TLA");
> $tb->load( qw(188.52 0.00 97.36 185.51) );
> print $tb;
>
> produces
>
> TLA
> 188.52
> 0.00
> 97.36
> 185.51
>
> What I want to produce is
>
> TLA
> 188.52
> 0.00
> 97.36
> 185.51
>
>
> Maybe I'm being obtuse or haven't read the docs closely enough, but I
> don't see how to do this.
In fact there's too little user control over alignment in more respects,
title-to-body alignment is just one of them. It will be the first thing
to fix if there is another release, but I'm not yet sure how to do it.
The problem isn't that these features are hard to implement, but how
to squeeze them into the user interface, which isn't exactly a beauty
to begin with. In fact, that's why I left them out in the first place,
that, and that I'd have to describe them all. I thought I'd get away
with it, but you're not the first to complain.
Anno
Anno Siegel Guest
-
David K. Wall #13
Re: Printing a hash of hashes using an array for the headings and getting the columns to line up
Anno Siegel wrote:
[regarding Text::Table]
I haven't examined the internals closely, so I may be way off-base here, but> In fact there's too little user control over alignment in more respects,
> title-to-body alignment is just one of them. It will be the first thing
> to fix if there is another release, but I'm not yet sure how to do it.
>
> The problem isn't that these features are hard to implement, but how
> to squeeze them into the user interface, which isn't exactly a beauty
> to begin with. In fact, that's why I left them out in the first place,
> that, and that I'd have to describe them all. I thought I'd get away
> with it, but you're not the first to complain.
since it already allows users to define columns with hashes, wouldn't that be
the obvious place to add new features for column formats? But I'd guess
you're looking at far more features than I have in mind, so I'll shut up
before I embarrass myself *too* much. :-)
David K. Wall Guest
-
Anno Siegel #14
Re: Printing a hash of hashes using an array for the headings and getting the columns to line up
David K. Wall <usenet@dwall.fastmail.fm> wrote in comp.lang.perl.misc:
That is very probably what will happen.> Anno Siegel wrote:
>
> [regarding Text::Table]
>>> > In fact there's too little user control over alignment in more respects,
> > title-to-body alignment is just one of them. It will be the first thing
> > to fix if there is another release, but I'm not yet sure how to do it.
> >
> > The problem isn't that these features are hard to implement, but how
> > to squeeze them into the user interface, which isn't exactly a beauty
> > to begin with. In fact, that's why I left them out in the first place,
> > that, and that I'd have to describe them all. I thought I'd get away
> > with it, but you're not the first to complain.
> I haven't examined the internals closely, so I may be way off-base here, but
> since it already allows users to define columns with hashes, wouldn't that be
> the obvious place to add new features for column formats?
I'm also playing with a more radical redesign where table columns would> you're looking at far more features than I have in mind, so I'll shut up
> before I embarrass myself *too* much. :-)
become independent objects, but I think I'll go for a quick fix.
Anno
Anno Siegel Guest
-
Anno Siegel #15
Re: Printing a hash of hashes using an array for the headings and getting the columns to line up
Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote in comp.lang.perl.misc:
You can now (well, after the version propagates) control the alignment> David K. Wall <usenet@dwall.fastmail.fm> wrote in comp.lang.perl.misc:>> > Anno Siegel wrote:
> >
> > [regarding Text::Table]
> >
> > I haven't examined the internals closely, so I may be way off-base
> > here, but since it already allows users to define columns with hashes,
> > wouldn't that be the obvious place to add new features for column
> > formats?
> That is very probably what will happen.
of titles with the body through the key "align_title" in the
hash-specification, much like you suggested.
One feature draws another by the tail... So similarly, "align_title_lines"
controls the alignment of title lines among themselves for muiltiline
titles.
Anno
Anno Siegel Guest
-
David K. Wall #16
Re: Printing a hash of hashes using an array for the headings and getting the columns to line up
Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
[re Text::Table]
Oh my. I was reading the module and trying to understand how it works> You can now (well, after the version propagates) control the
> alignment of titles with the body through the key "align_title" in
> the hash-specification, much like you suggested.
>
> One feature draws another by the tail... So similarly,
> "align_title_lines" controls the alignment of title lines among
> themselves for muiltiline titles.
so that /maybe/ I could send you a patch. So far all I had
accomplished was screwing things up. If you're ever in Cincinnati I'll
buy you a beer, or if not a beer, some Skyline chili. :-)
--
David Wall
David K. Wall Guest
-
John W. Krahn #17
Re: Printing a hash of hashes using an array for the headings andgetting the columns to line up
"David K. Wall" wrote:
Are you saying that beer and Skyline chili are interchangable in>
> Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
>>> > You can now (well, after the version propagates) control the
> > alignment of titles with the body through the key "align_title" in
> > the hash-specification, much like you suggested.
> >
> > One feature draws another by the tail... So similarly,
> > "align_title_lines" controls the alignment of title lines among
> > themselves for muiltiline titles.
> Oh my. I was reading the module and trying to understand how it works
> so that /maybe/ I could send you a patch. So far all I had
> accomplished was screwing things up. If you're ever in Cincinnati I'll
> buy you a beer, or if not a beer, some Skyline chili. :-)
Cincinnati? That's either strong beer or weak chili. :-)
John
--
use Perl;
program
fulfillment
John W. Krahn Guest



Reply With Quote

