in reply to Re: 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.

Good answers to the specific questions. I just want to chime in and agree that this is pretty dense code to try to learn from. It's got unpack, which is fairly complex in itself, plus OO (and an object structure several levels deep), subroutine argument handling, and more. On top of that, it's not very Perlish code. If you're learning Perl, you don't want to learn to use C-style for loops like the first line below when you can do the second one:

for( my $j=0; $j<2; $j++){ # C-style loop for my $j (0..1){ # Perl-style loop, much clearer
  • Comment on Re^2: 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^3: I have a perl snippet. And I need help understanding it. Can you help answer these questions.
by Fighter2 (Novice) on Nov 08, 2011 at 03:24 UTC

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

      "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

      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.