printing with <<

Discussion in 'Perl Misc' started by billy, Mar 6, 2006.

  1. billy

    billy Guest

    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
    billy, Mar 6, 2006
    #1
    1. Advertising

  2. billy

    l v Guest

    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
    l v, Mar 6, 2006
    #2
    1. Advertising

  3. 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 ) {
    Brian McCauley, Mar 7, 2006
    #3
  4. 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.
    Brian McCauley, Mar 7, 2006
    #4
  5. billy

    l v Guest

    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
    l v, Mar 8, 2006
    #5
  6. 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.
    Brian McCauley, Mar 8, 2006
    #6
  7. billy

    l v Guest

    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
    l v, Mar 8, 2006
    #7
  8. billy

    billy Guest

    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
    }}}}}
    }
    billy, Mar 9, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon B
    Replies:
    7
    Views:
    7,496
    =?Utf-8?B?SmFzb25DaG9p?=
    Jan 30, 2006
  2. JustSomeGuy
    Replies:
    13
    Views:
    506
    msalters
    Dec 9, 2004
  3. iffy agbim
    Replies:
    1
    Views:
    105
    Mark Andrews
    May 24, 2004
  4. iffy agbim
    Replies:
    0
    Views:
    101
    iffy agbim
    May 21, 2004
Loading...

Share This Page