Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > printing with <<

Reply
Thread Tools

printing with <<

 
 
billy
Guest
Posts: n/a
 
      03-06-2006
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

 
Reply With Quote
 
 
 
 
l v
Guest
Posts: n/a
 
      03-06-2006
billy wrote:
> 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

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      03-07-2006

l v wrote:

> 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 ) {

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      03-07-2006
billy wrote:

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

 
Reply With Quote
 
l v
Guest
Posts: n/a
 
      03-08-2006
Brian McCauley wrote:
> l v wrote:
>
> > 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 ) {


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

Len

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      03-08-2006

l v wrote:
> Brian McCauley wrote:
> > l v wrote:
> >
> > > 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 ) {

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

 
Reply With Quote
 
l v
Guest
Posts: n/a
 
      03-08-2006

Brian McCauley wrote:

> 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

 
Reply With Quote
 
billy
Guest
Posts: n/a
 
      03-09-2006
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:umper;
$Data:umper::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
}}}}}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 2 11-27-2010 12:12 PM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:52 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:50 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:28 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM



Advertisments