RobinV has asked for the wisdom of the Perl Monks concerning the following question:

Ok this is driving me nuts. I Thought I could figure it out myself however I think Its time to ask the monks.
So here it is. I got this piece of code:

foreach ($self->{'sections'}) { ..use $_ here to do stuff.. }

But I can't seem to access the value in $_ I tried to figure out what type of value it was by doing this:

print ref($_);

And I got nothing back. SO I googled around and I found the following snipper:

my $val = $_; use Carp qw(confess); if ( ! defined $val ) { return 'null'; } elsif ( ! ref($val) ) { if ( $val =~ /^-?\d+$/ ) { return 'int'; } elsif ( $val =~ /^-?\d+(\.\d+)?$/ ) { return 'float'; } else { return 'string'; } } else { my $type = ref($val); if ( $type eq 'HASH' || $type eq 'ARRAY' ) { return 'array'; } elsif ( $type eq 'CODE' || $type eq 'REF' || $type eq 'GLOB' | +| $type eq 'LVALUE' ) { return $type; } else { # Object... return 'obj'; } }

This code also returned nothing. When I just print $_ I get a value 300 something (I think this is the amount of dimentions the array has). This snipper will show you how the Variable is defined as is.

my $sections = (qx!rabin -Svv $filename!); my @detected_sections = (); while ($sections =~ m/idx\=(\d*) address=(0x(\d|[a-f])*) offset=(0x(\d +|[a-f])*) size=(\d*) align=(0x(\d|[a-f])*) perm=((r|w|x|-){4}) name=( +.*)/gi) { my %section_int = ( index => $1, address => $2, offset => $4, size +=> $6, align => $7, rights => $9, name => $11 ); push (@detected_sections, %section_int); } $report->{'sections'} = @detected_sections;

So am I stupid to guess that the foreach will do foreach array and I would get a hash but I can't use the $_ still in anyway.
So My Question is, What am I doing wrong? And how can I use the $_ in question?


Thanks in advance!
--Robin

Replies are listed 'Best First'.
Re: Variable Unusable?
by GrandFather (Saint) on Dec 29, 2009 at 10:15 UTC

    As a matter of style it is good practice to always use a locally declared (lexical) loop variable in for loops. It makes the scope of the variable clear and avoids issues with knowing what may be mucking with $_ within the scope of the loop.


    True laziness is hard work
Re: Variable Unusable?
by Anonymous Monk on Dec 29, 2009 at 08:46 UTC
    probable error
    push (@detected_sections, %section_int);
    you probably meant \%section_int

    another error here

    $report->{'sections'} = @detected_sections;
    an array in scalar context returns the number of elements in the array, you probably meant \@detected_sections, and you probably meant
    foreach ( @{ $self->{'sections'} }) { ..use $_ here to do stuff.. }
      Thanks!
      That seemed to do the Trick!

      I do know what the \ does normally. But I don't see why I'll have to use References here? Also the @{} thing is new. I guess I'll spend NewYears reading the Camel book.

        You have to use references to the arrays and hashes because scalars can only contain scalars and references are scalars.