in reply to Re^2: I have a perl snippet. And I need help understanding it. Can you help answer these questions.
in thread I have a perl snippet. And I need help understanding it. Can you help answer these questions.

hey bud. So when I replaced the for loop exactly the way you have mentioned I get the following error.

Use of uninitialized value in numeric gt()) at logprocess.pl in line 119

which is that loops line number where the for loop is at.

Also, can you please teach me how to print the value inside of a nested nested nested hash. THis one. I want to see the value of rxABC from? $obj->{$j}->{rxABC} I tried print "Found: $$obj->{$j}->{rxABC}" and it wont work. :(

  • Comment on Re^3: I have a perl snippet. And I need help understanding it. Can you help answer these questions.
  • Download Code

Replies are listed 'Best First'.
Re^4: I have a perl snippet. And I need help understanding it. Can you help answer these questions.
by davido (Cardinal) on Nov 08, 2011 at 05:22 UTC

    "It depends" (as the professors in my Economics classes liked to say).

    If $obj->{$j}->{rxABC} holds a scalar value, you should be able to print it like this:

    print "Found: $obj->{$j}->{rxABC}\n";

    That reminds me. Anytime you have a -> between two brackets you can omit it. So that becomes:

    print "Found: $obj->{$j}{rxABC}\n";

    On the other hand, if $obj->.....{rxABC} holds a reference to another layer (an array ref or hash-ref, for example), you will need to deal with that layer too. Fortunately Perl can print an array in a sensible way. So let's say that {rxABC} holds an array ref like this:

    $obj->{$j}{rxABC} = ['a', 'b', 'c'];

    Then you would need to dereference that array ref to make sense of it too. Here's one way:

    print "Found: ", join( ' ', @{ $obj->{$j}{rxABC} } ), "\n";

    Or another way:

    print "Found: @{$obj->{$j}{rxABC}}\n";

    Dave

Re^4: I have a perl snippet. And I need help understanding it. Can you help answer these questions.
by aaron_baugher (Curate) on Nov 08, 2011 at 14:16 UTC

    I'm not sure what you did, because the for loop I suggested doesn't have a numeric gt() to cause that error. I meant that instead of using the C-style, three-arguments-separated-by-commas for loop, it's almost always better in Perl to do something like this:

    for my $i (0..99){ # do something with $i from 0 to 99 } # instead of this: for( my $i=0; $i<100; $i++){ # do something with $i from 0 to 99 }

    See how much cleaner the first one is? It also has the advantage of showing you exactly what number it'll start and stop on, so you don't have to remember that $i<100 means it'll stop at 99 (likely to cause off-by-one errors), or use the even uglier $i>=99 as your condition.

    There are occasions when the C-style method may make sense, like if you want to step by some number other than 1. They're just very rare. In 15 years of programming in Perl, I'm sure I could count on one hand how many times I've used it, and even those times there was probably a better way.

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.