| |||||||||||
| |||||||||||
Iterators - expressions tha change each time you call them
If you're programming and you write the same expression into your code twice without changing any of the variables, you're simply writing the same expression twice, right? For example, here's a piece of Perl code that exits if $userval is 0, but adds it in to a total and keep going if it's not zero.
if ($userval == 0) { If you're reading from a file, though, things are different. Refer to the same file handle twice, and you'll get the next line read in each time - thus: if (<FH> == 0) { will read from a file handle TWICE - if the first line read is zero, the program exits. If the first line was NOT zero, a second line is read and that value in that line is added to $total. Why the difference? Because the read from operator - <> in Perl - is what we call an iterator, and each time we refer to it, it moves on to the next value. This one often catches new programmers ... the solution is to read the value just once and save it into a variable - thus: $userval = <FH>; (written 2006-04-27 06:21:54) Associated topics are indexed under P207 - Perl - File Handling
Some other Articles
May day awayDisc Partitioning Erin Brent - rest in peace Copying files and preserving ownership Iterators - expressions tha change each time you call them Minature Cyclists only - limited headroom As I walked in to Oxford Extremes costs of getting on line Catch up weekend For any North and West Wilts and Swindon Readers 1635 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 at 50 posts per pageThis is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price. Link to Ezine home page (for reading). Link to Blogging home page (to add comments). |
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho | |||||||||||