Channel Button

There is 1 article on this title. You are reading the article ranked and rated #1 by Helium's members.

Computers & Technology   >

Software & Web Development

PERL programming: good loop techniques

(typos corrected)

The obvious way to loop data is foreach, but it's only a good thing to use if a small number of things need to be looped, and not many times, ideally just once.

If you are going to loop the items from one to a million, you will be slowing things down with the easy command...

foreach $n(1..1000000){}

and it is far better to go for the only slightly cumbersome:

while ($n lt "1000"){$n... }...

(i omitted the plus signs inside the curly bracket after the $n, but the idea is to increment it by one each time, hence it mimics the foreach loop in outcome, but not in process - the publishing media prevents me using pointy brackets and maybe also pluses, but possibly the plusses are okay. I'll try it some time - it's an edit-once-only environment here where I am writing)

while is much more efficient than foreach.

If you have the blue camel book it is worth looking up loops and seeing all the various things you can have inside the initial while brackets so that you can be as deft and professional as possible (unlike me) in making while loops with variables and counters and increments and whatnots all thrown in.

If you do noy have the blue camel book and yet are interested in writing perl and/or have perl writing experience then you ought to get the blue camel book.

Naturally while loops get used even without it being as a result of making a foreach loop more efficient. When opening files and reading their content or reading in results from sql queries there is no other normal way you'd immediately think of using.

Of course you can also read a file in with an array and then you would be forced into a foreach loop if you wanted to access the data you had already read in. A cumbersome approach, but you could interrupt it and make it a better day for you and Michael Jackson by joining the array's content. This calls for more looking up in the blue camel book - it's a long time since I wrote anything like that, but I have done and it's no doubt very easy to master if you sit with the blue camel book for a bit.

Efficiency in repetitive processes is vital in perl development. Because of how and where perl fits into things the sort of thing you need it for requires of you that you avoid being slapdash when structuring the processes. While loops are a cornerstone in perl efficiency. The best way to learn this is as I did - use the easy foreach methods with bigger and bigger volumes of data until one day you see with your own eyes that if you overload a container, the container breaks.

Learn more about this author, Two one three four six six.
Contact this writer Click here to send author comments or questions.


Below are the top articles rated and ranked by Helium members on:

PERL programming: good loop techniques

Add your voice

Know something about PERL programming: good loop techniques?
We want to hear your view. Write_penWrite now!

Debate Icon

Cast your vote!

Will Internet Explorer 8 solve Microsoft's Web browser problems?

Click for your side. Must be logged in.

136238

Featured Partner

Environment Northeast (ENE)

Environment Northeast (ENE) has partnered with Helium, giving you the chance to write for a cause. Browse ENE's ...more

What is Helium? | User Guide | Community | Link to Helium | Privacy | User agreement | DMCA

Helium, Inc.
200 Brickstone Square Andover, MA 01810 USA