2

I try to add array in hash.

if ( not exists $hashtime{ $arr[0] }{ $date }{ $hour }{ $min } ) {
    print "$min not exist";
    $hashtime{ $arr[0] }{ $date }{ $hour }{ $min } = [ $sec ];
    $create++;
};

And received the error:

Not a HASH reference at ./sort_log_by_ip.pl line 63, line 1.

Why is this code wrong?

In perldoc perldsc I see this construction, and I'm using something similar:

while ( <> ) {
     next unless s/^(.*?):\s*//;
     $HoA{$1} = [ split ];
}

Update

Code before:

if ( not exists $hashtime{ $arr[0] } ) {
    $hashtime{ $arr[0] } = ( $date => { $hour => { $min => [ $sec ] } } );
    $create++; 
    print "create for IP: $arr[0]\n";
}

if ( not exists $hashtime{ $arr[0] }{$date} ) {
    $hashtime{ $arr[0] }{ $date } = ( $hour => { $min => [ $sec ] } );
    $create++;
    print "create for IP: $arr[0] DATE: $date\n";
}

if ( not exists $hashtime{ $arr[0] }{$date}{$hour} ) {
    $hashtime{ $arr[0] }{ $date }{ $hour } = ( $min => [$sec] );
    $create++;
    print "create for IP: $arr[0] DATE: $date HOUR: $hour\n";
}
2
  • 2
    Show the complete code. Your code errors at line 63, but there's no way to know what line 63 is in your code-snippet or what any of the other variables are or do... Commented Nov 23, 2017 at 9:25
  • if (not exists $hashtime{$arr[0]}{$date}{$hour}{$min}){ - this is line 63. Commented Nov 23, 2017 at 9:42

1 Answer 1

5

You are using a list ( ) instead of a hash reference { }, in all if blocks.

When you say

$hashtime{$arr[0]} = ( $date => { $hour => { $min => [$sec] } } );

because of LIST ( ) being evaluated in scalar context what happens is equivalent to

$hashtime{$arr[0]} = ( $date, { $hour => { $min => [$sec] } } );

ending up as

$hashtime{$arr[0]} = { $hour => { $min => [$sec] } };

since the , operator evaluates and discards operands one at a time, returning the last one.

The next if goes similarly and you then have either of (or both)

$hashtime{$arr[0]}{$date}{$min}{[$sec]}
$hashtime{$arr[0]}{$hour}{$min}{[$sec]}

However, the code that draws the error

if (not exists $hashtime{$arr[0]}{$date}{$hour})

needs a hashref at both $arr[0] and {$date}, while it clearly doesn't have both.


In both if blocks you need to assign a hash reference, obtained using { }

$hashtime{$arr[0]} = { $date => { $hour => { $min => [$sec] } } };

and

$hashtime{$arr[0]}{$date} = { $hour => { $min => [$sec] } };

as well as in the last if block.


Please indent your code properly. It is very hard to work with it the way it was posted.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for help! $hashtime{$arr[0]} = ( $date, { $hour=>{$min=>[$sec]} } ); - error is here. You are write/ I have not written for perl more then 10 year.
@Skif That appears to be the only error; and you got that same thing right everywhere else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.