Operating System - Linux
1753449 Members
6566 Online
108794 Solutions
New Discussion юеВ

Re: How to access hash values in "arrays of hases" ?

 
SOLVED
Go to solution
Greg Gerke
New Member

Re: How to access hash values in "arrays of hases" ?

Another issue you're going to have is with the line:

my %loopdata;

being inside of your while loop. I'm thinking that having it inside rather than outside is going to cause that variable to be defined each time you go through the loop. For instance, try these two small examples to see the difference:

my $j = 0;
my $k; # $k defined on the outside
while ($k < 10) {
$j++;
$k = $j;
print "k = $k\n"; # prints 1 - 10
}

--- vs. ---

my $j = 0;
while ($k < 10) {
my $k; # $k defined on the inside
$j++;
$k = $j;
print "k = $k\n"; # prints 1 - infinity
}
Pankaj Yadav_1
Frequent Advisor

Re: How to access hash values in "arrays of hases" ?

I want to know that how can I access two consecutive values of the array when I am inside the FOREACH loop.
Greg Gerke
New Member

Re: How to access hash values in "arrays of hases" ?

do you mean something that won't change the value of your $i along the way...?

my @array=(80,70,60,50,40,30,20,10);
my $i = 3;

print "i before = $i\n";
print "array elements $i = $array[$i]\n";
print "array elements " . ($i + 1) . " = " . $array[$i + 1] . "\n";
print "i after = $i\n";
Pankaj Yadav_1
Frequent Advisor

Re: How to access hash values in "arrays of hases" ?

Thanks a lot to all of you.
I got what I wanted.

I have given you the points.
Ralph Grothe
Honored Contributor

Re: How to access hash values in "arrays of hases" ?

As for your last question,
you may not have come accross array and hash slices.
These allow you to "slice" more than one list element in one go.
Remember in Perl (up until now, but they are going to change this in Perl6 because it seems to cause too much confusion among Perl beginners) that an "$" in a variable refers to always what you get, viz. a scalar,
while a "@" refers to a list.
So often in beginners' scripts you will see statements like

$someval = @ary[$i];

which in fact is calling for a slice.
But as you only index one element it doesn't make much sence.
Whereas if you are after, say the 3rd, 8th, and 12th to 14th element, you could reference them in one go

($elem3, $elem8, $elem12, $elem13, $elem14) = @ary[3,8,12..14];

or better

@myslice = @ary[3,8,12..14];

The same of course works for hash slices.
Say those keys exist, neglecting testing for their existence.

@myslice = @loopdata{qw(interval type date)}
Madness, thy name is system administration