For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > October 2006 > Printing and Formatting data from hash (or array)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Printing and Formatting data from hash (or array)
Mike

2006-10-30, 7:10 pm

I'm trying to format the data I have in a hash (or, I could throw it
into an array also) so I can produce a report.

I've attempted to throw that hash data into an array, but I'm not sure
how to arrange the data, because as it appears, the key would be $array
ofhash[0] and the value would be $arrayofhash[1] would be the value.
I'd like to throw a tab in between the matching keys and values. The
only problem I'm having is figuring out how to throw that into a loop,
so it extracts all the data, for example, lets say there are 150
elements ( $arrayofhash[150] ) in that array.

Or, if you are more skilled than me (most people are), how could I
format that data from the hash? I've been tinkering with printing the
values from hashes, but it seems like there is a lot of duplicated
data, for instance, I found the example of this to print all the values
I have in a hash, but it seems like it is a single array within the
hash repeating (not printing multiple items in hash):

while ( ($key,$value) = each %hash ) {
print "$key => $value\n";
}

However, when I copy all the hash data to an array, and print the
array, it doesn't show all the duplicated data.

Thanks for the help

Mike

PS

I've learned how to (and how not to) multi-post and cross-post in
google groups, sorry for previous questions asked the group.

Mike

2006-10-30, 7:10 pm


Mike wrote:
> I'm trying to format the data I have in a hash (or, I could throw it
> into an array also) so I can produce a report.
>
> I've attempted to throw that hash data into an array, but I'm not sure
> how to arrange the data, because as it appears, the key would be $array
> ofhash[0] and the value would be $arrayofhash[1] would be the value.
> I'd like to throw a tab in between the matching keys and values. The
> only problem I'm having is figuring out how to throw that into a loop,
> so it extracts all the data, for example, lets say there are 150
> elements ( $arrayofhash[150] ) in that array.
>
> Or, if you are more skilled than me (most people are), how could I
> format that data from the hash? I've been tinkering with printing the
> values from hashes, but it seems like there is a lot of duplicated
> data, for instance, I found the example of this to print all the values
> I have in a hash, but it seems like it is a single array within the
> hash repeating (not printing multiple items in hash):
>
> while ( ($key,$value) = each %hash ) {
> print "$key => $value\n";
> }
>
> However, when I copy all the hash data to an array, and print the
> array, it doesn't show all the duplicated data.
>
> Thanks for the help
>
> Mike
>
> PS
>
> I've learned how to (and how not to) multi-post and cross-post in
> google groups, sorry for previous questions asked the group.


Ok, I think I've figured it out, this is what I'm doing (two tabs,
instead of one, yes, I know), is their a cleaner way to do this?:

my $n = 0;
while ($temphash[$n]) {
print $temphash[$n];
$n++;
print "\t\t";
print $temphash[$n];
print "\n";
$n++;
}

Thanks

Mike

jl_post@hotmail.com

2006-10-30, 7:10 pm

Mike wrote:
>
> I've attempted to throw that hash data into an array, but I'm not sure
> how to arrange the data, because as it appears, the key would be $array
> ofhash[0] and the value would be $arrayofhash[1] would be the value.
> I'd like to throw a tab in between the matching keys and values. The
> only problem I'm having is figuring out how to throw that into a loop,
> so it extracts all the data, for example, lets say there are 150
> elements ( $arrayofhash[150] ) in that array.


You might consider using the Data::Dumper module. It's a standard
module, so you should already have it installed.

With the Data::Dumper module, you can write your code like this:

use Data::Dumper;
print Dumper \%hash;

and your output will look like:

$VAR1 = {
'cat' => 1,
'dog' => 2,
'bird' => 3
};

which may or may not be what you want. You can tweak how the output
appears -- to find out how, read the Data::Dumper's documentation with
"perldoc Data::Dumper".

I hope this helps, Mike.

-- Jean-Luc

Mike

2006-10-30, 7:10 pm


jl_post@hotmail.com wrote:
> Mike wrote:
>
> You might consider using the Data::Dumper module. It's a standard
> module, so you should already have it installed.
>
> With the Data::Dumper module, you can write your code like this:
>
> use Data::Dumper;
> print Dumper \%hash;
>
> and your output will look like:
>
> $VAR1 = {
> 'cat' => 1,
> 'dog' => 2,
> 'bird' => 3
> };
>
> which may or may not be what you want. You can tweak how the output
> appears -- to find out how, read the Data::Dumper's documentation with
> "perldoc Data::Dumper".
>
> I hope this helps, Mike.
>
> -- Jean-Luc


Works perfect, thanks Jean-Luc. Now on to my next problem ;-)

J. Gleixner

2006-10-30, 7:10 pm

Mike wrote:
> Mike wrote:


[...]
> I've been tinkering with printing the

Correct. A hash has unique keys.
[color=darkred]
Post a short example of how you're doing that.

Possibly, you want to have a hash of arrays?

push( @{ $report{ $key } }, 'some value' );
push( @{ $report{ $key } }, 'another value' );

For many examples of data structures, see: perldoc perldsc
[color=darkred]
> Ok, I think I've figured it out, this is what I'm doing (two tabs,
> instead of one, yes, I know), is their a cleaner way to do this?:
>
> my $n = 0;
> while ($temphash[$n]) {


Is this an array or a hash?? Review the differences and name your
variables accordingly.

> print $temphash[$n];
> $n++;
> print "\t\t";
> print $temphash[$n];
> print "\n";
> $n++;
> }


If that actually prints the values you want. You may put the print on
one line:

print "$tmphash[$n++]\t\t$temphash[$n++]\n";
Jim Gibson

2006-10-30, 7:10 pm

In article <1162225891.850942.204730@m7g2000cwm.googlegroups.com>, Mike
<mikedawg@gmail.com> wrote:

> I'm trying to format the data I have in a hash (or, I could throw it
> into an array also) so I can produce a report.
>
> I've attempted to throw that hash data into an array, but I'm not sure
> how to arrange the data, because as it appears, the key would be $array
> ofhash[0] and the value would be $arrayofhash[1] would be the value.
> I'd like to throw a tab in between the matching keys and values. The
> only problem I'm having is figuring out how to throw that into a loop,
> so it extracts all the data, for example, lets say there are 150
> elements ( $arrayofhash[150] ) in that array.
>
> Or, if you are more skilled than me (most people are), how could I
> format that data from the hash? I've been tinkering with printing the
> values from hashes, but it seems like there is a lot of duplicated
> data, for instance, I found the example of this to print all the values
> I have in a hash, but it seems like it is a single array within the
> hash repeating (not printing multiple items in hash):
>
> while ( ($key,$value) = each %hash ) {
> print "$key => $value\n";
> }
>
> However, when I copy all the hash data to an array, and print the
> array, it doesn't show all the duplicated data.


Please post a complete, working program that illustrates this. The
above loop should definitely print all of the keys and values in %hash.

>
> Thanks for the help
>
> Mike
>
> PS
>
> I've learned how to (and how not to) multi-post and cross-post in
> google groups, sorry for previous questions asked the group.
>


Great! Thanks for paying attention. Next thing to learn: post complete,
working, short programs that demonstrate the problem you are having,
including data if required, and a description of what the program is
doing or not doing that you expect it should.
Jim Gibson

2006-10-30, 7:10 pm

In article <1162226842.201440.263640@m7g2000cwm.googlegroups.com>, Mike
<mikedawg@gmail.com> wrote:

> Mike wrote:

[...]
[color=darkred]
>
> Ok, I think I've figured it out, this is what I'm doing (two tabs,
> instead of one, yes, I know), is their a cleaner way to do this?:
>
> my $n = 0;
> while ($temphash[$n]) {
> print $temphash[$n];
> $n++;
> print "\t\t";
> print $temphash[$n];
> print "\n";
> $n++;
> }


There is definitely a cleaner way to do what you are doing, but it is
not clear exactly what you are attempting to do, or what values are in
@temphash. Please post a complete, working, short program.

Thanks.
anno4000@radom.zrz.tu-berlin.de

2006-10-31, 7:58 am

J. Gleixner <glex_no-spam@qwest-spam-no.invalid> wrote in comp.lang.perl.misc:
> Mike wrote:


[...]

>
> If that actually prints the values you want. You may put the print on
> one line:
>
> print "$tmphash[$n++]\t\t$temphash[$n++]\n";


You're accessing two different variables.

That's risky. Perl is, in principle, allowed to evaluate parts of
an expression in any sequence, so the results of the two ++ operations
are not well defined. I realize the current Perl produces the expected
result, and it's unlikely to change, but the behavior is undocumented.
A safe alternative is

print "$temphash[ $n]\t\t$temphash[ $n + 1]\n";
$n += 2;

but isn't a single statement anymore.

Anno
J. Gleixner

2006-10-31, 7:02 pm

anno4000@radom.zrz.tu-berlin.de wrote:
> J. Gleixner <glex_no-spam@qwest-spam-no.invalid> wrote in comp.lang.perl.misc:


>
> You're accessing two different variables.
>
> That's risky. Perl is, in principle, allowed to evaluate parts of
> an expression in any sequence, so the results of the two ++ operations
> are not well defined. I realize the current Perl produces the expected
> result, and it's unlikely to change, but the behavior is undocumented.
> A safe alternative is
>
> print "$temphash[ $n]\t\t$temphash[ $n + 1]\n";
> $n += 2;
>
> but isn't a single statement anymore.


Thanks for the correction Anno. I knew that and I guess I
was so focused on getting rid of all the print statements
that I over-looked it.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com