Printing and Formatting data from hash (or array)

M

Mike

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.
 
M

Mike

Mike said:
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
 
J

jl_post

Mike said:
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
 
M

Mike

Mike said:
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

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

J. Gleixner

Mike said:
Mike wrote:
[...]
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):

Correct. A hash has unique keys.
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
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";
 
J

Jim Gibson

Mike said:
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.
 
J

Jim Gibson

Mike said:
Mike said:
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.
[...]


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.
 
J

Joe Smith

Mike said:
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.

That while() loop _will_ print out each key once and only once.
It does not produce any duplicates and does not skip anything.

Whatever you are doing in your program, it must be more than
what is being shown there. (I have seen this sort of problem
show up before, but it turns out the programmer had two
different instances of each() on the same hash in nested loops.)

Can you produce any code that we can run to duplicate your problem?
-Joe
 
A

anno4000

J. Gleixner said:
Mike wrote:
[...]
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";

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

J. Gleixner

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top