in reply to subroutine no error

Howdy adriang, welcome back to the Monastery!

You're only declaring %CS101 after calling ave(), so at the time ave is running, it does not exist yet and therefore can't be accessed. Move the declaration to somewhere before the subroutine call, and all will be fine.

Replies are listed 'Best First'.
Re^2: subroutine no error
by adriang (Sexton) on Jul 23, 2014 at 10:31 UTC

    Thanks for the quick replay. Yes, this is true but the question is why I didn't get any error? Thanks in advance

      Good question, actually: and my apologies, I misread your post.

      Looking at the script again, I think that the reason is as follows. When Perl compiles your script, the fact that ave is called before %CS101 doesn't matter: by the time the Perl compiler gets to the body of ave, %CS101% has been declared and thus can be accessed.

      However, when Perl runs your script, ave is called before %CS101 gets assigned a value, so the hash is empty and you don't get any output.

        Thanks for the explanation. Now all is clear. Adrian