Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: The error says the value is uninitialized, but it works anyway

by jcb (Parson)
on Aug 19, 2019 at 00:42 UTC ( [id://11104670]=note: print w/replies, xml ) Need Help??


in reply to Re: The error says the value is uninitialized, but it works anyway
in thread The error says the value is uninitialized, but it works anyway

It is probably worth noting here that $i, $j, $k, $l, and so on are only good loop iteration variables because of the sheer weight of the long-standing convention of using them that way.

As far as I know, this convention originated with the C programming language, although the fact that I, J, K and some others default as integers in FORTRAN suggests that it is older than C. These variables are probably best used when your iteration variables will have integer values, as that is also a (weaker) long-standing convention. In the example that started this discussion, the iteration was over the positions in an array, which are integers in Perl. If you are iterating over the values of an array, as in the solution presented by holli above, you should use a descriptive iterator variable as holli did.

The solution holli presented also illustrates another problem: algorithmic complexity. Nested loops are usually the slowest practical way to solve a problem, and when you see such a construction, you should consider if there is a better way, and explicitly checking two elements of @drop is really just an unrolled nested loop. (See also: Loop unrolling) On modern hardware, the issue will be unnoticeable until you are trying to process large amounts of data, and then your program will suddenly be horrendously slow. For more enlightenment on this, analyze the expected running time of the various solutions here and then run actual benchmarks with both the small data set given in the original assignment and some (much) larger generated data sets, with hundreds, thousands, tens of thousands, hundreds of thousands, and millions of items.

Replies are listed 'Best First'.
Re^3: The error says the value is uninitialized, but it works anyway
by AnomalousMonk (Archbishop) on Aug 19, 2019 at 17:27 UTC
Re^3: The error says the value is uninitialized, but it works anyway
by soonix (Canon) on Aug 19, 2019 at 08:36 UTC
    I think usage of i, j, k for indexes is older, FORmulaTRANslator is oriented towards mathematical notation.

    However, in homework tasks for beginners, loop unrolling is less likely in the curriculum, and my guess is that it's all about that bass nested loops. All the other solutions given so far won't work if @drop has more or less that two elements…

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11104670]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-18 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found