printing with <<

B

billy

I am writing some code that write some more code.
example:
my (%h1,%h2,%h3);

# assume each hash has 100 widgets

foreach $h1 (keys %h1)
print <<P1;
print some stuff
P1
foreach $h2 (keys %h2)
print <<P2;
print some stuff
P2
foreach $h3 (keys %h3)
print <<P3;
print some stuff
P3
} # loop p3
} # loop 2
} # loop 1

What I would like is to eliminate the 3 PN to just one
print <<P1
foreach blabla But don't print the foreach loop, just loop.
P1

foreach \$h1(keys \%h1)
{
a(\$h1);
b(\$h1);
c(\$h1);
P1
foreach
 
L

l v

billy said:
I am writing some code that write some more code.
example:
my (%h1,%h2,%h3);

# assume each hash has 100 widgets

foreach $h1 (keys %h1)
print <<P1;
print some stuff
P1
foreach $h2 (keys %h2)
print <<P2;
print some stuff
P2
foreach $h3 (keys %h3)
print <<P3;
print some stuff
P3
} # loop p3
} # loop 2
} # loop 1

What I would like is to eliminate the 3 PN to just one
print <<P1
foreach blabla But don't print the foreach loop, just loop.
P1


Try a hash reference,

for my $hash_ref (\%h1, \%h2, \%h3) {
for my $key (keys %{$hash_ref}) {
print <<P1;
$key = $$hash_ref{$key}
P1
}
}

Len
 
B

Brian McCauley

l said:
for my $hash_ref (\%h1, \%h2, \%h3) {

Of course as soon as you see something like that a little voice in your
head should be screaming the word "ARRAY" so loudly that is actually
causes you physical pain.

for my $hash_ref ( @AoH ) {
 
B

Brian McCauley

billy said:
I am writing some code that write some more code.

The is even more important than ever that you make sure your
illustrative code is valid code.

Sometimes it's possible for use to guess where you've made typos but
when the code contains heredocs that in turn contain stuff that looks
like code it would be a waste of time to try.

Please produce a _minimal_ but _complete_ example that illustrates what
it is that you are currently doing. Post it here directly by cutting
and pasting - do not re^H^Hmistype.

This and much other useful advice can be found in the posting
guidelines.

I'm guessing you could replace the repetative code with either want a
loop or recursion but without seeing real code I'm not going to try to
guess which.
 
L

l v

Brian said:
Of course as soon as you see something like that a little voice in your
head should be screaming the word "ARRAY" so loudly that is actually
causes you physical pain.

for my $hash_ref ( @AoH ) {

I was thinking "unnamed array" when I posted. Is unnamed a concern?

Len
 
B

Brian McCauley

l said:
I was thinking "unnamed array" when I posted. Is unnamed a concern?

No it wouldn't be.

But I don't think you are thinking "unnamed array". I think you are
confusing a list and an array ( see FAQ).

And I think you've got the wrong end of the stick anyhow.

What I'm saying is that the very existance of three variables named
something1, something2 and something3 screams out for them to be
replaced by an array.

Note this is a very general rule and has _nothing_ to do with the fact
that the variables in this case happen to be hashes and very little to
do with the fact that the programming language happens to be Perl.
 
L

l v

Brian said:
And I think you've got the wrong end of the stick anyhow.

What I'm saying is that the very existance of three variables named
something1, something2 and something3 screams out for them to be
replaced by an array.

Got it. And agree. I was using the same variable names the OP has
used.

Len
 
B

billy

Here is a sample of what I'm trying to do. The hash is 100k lines long
in the real deal and the print statements are more of them.
But as you can see it is a lot of loops and having to step into a <<
and out of it again to have valid perl code

#!/usr/local/bin/perl

my $hash = {
'dogs' => {
'cats' => {
'sheep' => {
'goats' => {
'mice' => undef,
'rats' => undef,
'birds' => undef,}}}}};

use Data::Dumper;
$Data::Dumper::Indent = 1;
print Dumper($hash);
my ($l1,$l2,$l3,$l4,$l5,$l6,$l7);

foreach $l1 (keys %$hash) { # dogs
print <<L1;
If I had a big dog
Then all my money would
be used to feed him
L1
foreach $l2 (keys %{$hash->{$l1}}) { # cats
print <<L2;
If I had a cat
then I would have to buy
kitty litter
L2
foreach $l3 (keys %{$hash->{$l1}->{$l2}}) { #sheep
print <<L3;
If I had a sheep
then I could knit my
own wool sweaters
L3
foreach $l4 (keys %{$hash->{$l1}->{$l2}->{$l3}}) { #goats
print <<L4;
if I had my own goats
then I could have some cabrito
L4
foreach $l5 (keys %{$hash->{$l1}->{$l2}->{$l3}->{$l4}}) { #mice
rats birds
if ($l5 eq 'mice') {
print <<L5A;
If I had mice
then I need a cat
L5A
} elsif ($l5 eq 'rats') {
print <<L5B;
If I had some rats
Then I would need a rat terrier.
L5B
} elsif ($l5 eq 'birds') {
print <<L5C;
If I had some birds
then I would need to get rid of my cat.
L5C
} else {
print <<DEF;
I'm so lonely I don\'t know what to do.
I know I\'ll get a pet
DEF
}}}}}
}
 

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,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top