user2941526
user2941526

Reputation: 497

Perl: Program Crashing Upon Creation of a Hash

As a follow up to a question asked earlier, I am creating a hash with keys between a user specified range, also with a specified increment size ($binsize). The code to create this hash seems to be correct:

my %hash;
for (my $increm = $lowerbound; $increm <= $upperbound; ++$binsize) {

        $hash{$increm} = 0;

}

with default variable values:

$lowerbound = 1000
$upperbound = 1500
$binsize = 1

But when I go to print the keys of the hash using:

print "$_\n" for keys %hash;

The program does not perform the operation and crashes. Does anyone have any ideas?

Thank you in advance!

EDIT: The aim is eventually to turn the completed hash into a CSV, so if I cannot view the hash at this stage, how can I check if the keys are populated correctly?

PROBLEM FIXED NOW:

changing:

for (my $increm = $lowerbound; $increm <= $upperbound; ++$binsize) {

to:

for (my $increm = $lowerbound; $increm <= $upperbound; $increm+=$binsize) {

seemed to fix the problem

Thanks!

Upvotes: 2

Views: 98

Answers (1)

ccheneson
ccheneson

Reputation: 49410

for (my $increm = $lowerbound; $increm <= $upperbound; ++$binsize) {

$increm doesnt change value so this for loop is endless

Maybe you wanted this:

for (my $increm = $lowerbound; $increm <= $upperbound; $increm++) {

Edit:

If you want to increment $increm by the value of $binsize:

foreach (my $increm = $lowerbound; $increm <= $upperbound; $increm += $binsize) {

Upvotes: 3

Related Questions