I don't want to sound harsh, but your code makes my eyes bleed. The good news is that there are a few easy steps you can take to make it much easier to read, understand, and maintain.

Use descriptive variable names. Names like %Myhash are fine in CS textbook examples, but in real code they are pure evil. What kind of information does the hash contain? Put it in the name. Something like %system_info can be much easier to understand.

Use consistent capitalization and formatting in your variable names. Find a style you like and stick to it. Generally variable names are lower_case_with_underscores but some people prefer initialLowerCaseWithCamelCaseAfter. Take a look at perlstyle.

Banishing $iteminiteminiteminitem and friends will take care of 90% of the problem with this code. What else can we do to fix it?

Format your code for readability. Break up long lines. Use whitespace. Use consistent indentation.

foreach $item (keys %Myhash){ if ($item eq $My_Value) { foreach $iteminitem (sort(keys %{$Myhash{$item}})){ foreach $iteminitem1 (sort(keys %{$Myhash{$item}{$iteminit +em}})){ if( $iteminitem1 eq 'name' && $user_number == $Myhash{$item}{$iteminitem}{$itemi +nitem1} ) { my $User = $iteminitem; } } } } }

All I did was reformat your code and it is a fair bit easier to read. You might want to take a look at perltidy. It does a pretty good job of reformatting perl code for you. Some people require that all code be passed through perltidy before check in to their source repositories.

Remember that you should be writing your code so that you will understand it 6 months from now, when you get a call at 4am on a Saturday morning about the emergency bug that must be fixed right away.

Well written code is primarily a document written to communicate the functioning of the system for maintenance programmers. The fact that it runs and does work is just a side effect of a well documented system.

OK, the last statement isn't really true, but I've found that keeping this point of view in mind helps to improve the readability of my code.

After cleaning up your code a bit, I noticed that you seem to be doing some unnecessary loops.

# This: foreach my $top_item ( keys %Myhash ){ if ($top_item eq $My_Value) { # do stuff } } # can be reduced to this: if ( exists $Myhash{$My_Value} ) { # do stuff } # And this: foreach $iteminitem1 (sort(keys %{$Myhash{$item}{$iteminitem}})){ if( $iteminitem1 eq 'name' && $user_number == $Myhash{$item}{$iteminitem}{$itemi +nitem1} ) { my $User = $iteminitem; } } # can be reduced to this: if ( exists $Myhash{$My_Value}{$iteminitem}{'name'} and $user_number == $Myhash{$My_Value}{$iteminitem}{'name'} ) { my $User = $iteminitem; } # The combined restructuring yields: if ( exists $Myhash{$My_Value} ) { foreach $iteminitem (sort(keys %{$Myhash{$item}})){ if ( exists $Myhash{$My_Value}{$iteminitem}{'name'} and $user_number == $Myhash{$My_Value}{$iteminitem}{'name'} ) { my $User = $iteminitem; } } }

That eliminates several unneeded sorts and a fair bit of looping over arrays.

Keep working on your code, you'll get better. Improving the readability of your code will make it easier to debug as you do your initial development and easier to add features and debug in the future. Learning a few simple habits now will increase your productivity, make your teeth whiter and brighter, and get you the girls.


TGI says moo


In reply to Re: Can't use string ("2") as a HASH ref while "strict refs" in use by TGI
in thread Can't use string ("2") as a HASH ref while "strict refs" in use by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.