the FOR Loop in the English Language
- From: jm-1@xxxxxxxxxxxxxxxxx
- Date: 29 Jun 2006 07:45:15 -0700
Hi Folks
I am totally new to programming and I am trying to write notes that
will help me to understand the different perl programming concepts in
plain English. If I tried to explain the code below to my dear
Grandmother so that she could understand it, would this sound correct?
@one_to_ten = (1 .. 10);
$top_limit = 25;
for $i (@one_to_ten, 15, 20 .. $top_limit) {
print "$i\n";
}
Line One
Create an array called "one_to_ten" and assign the values 1 through 10
to this array. By the way Grandmother, an array is just a list (like a
shopping list) that can contain almost any value you like. In this case
it will be a list of numbers from 1 to 10.
Line Two
Create a scalar called "top_limit" and assign the value 25 to this
scalar. By the way Grandmother a scalar is just a container that can
hold any "single" value (any valid character on a computer).
Line Three
Now Grandmother, this line sets up a loop. Let's use an analogy
before we delve into this. I give you a shopping list with 10 items on
it. For each of those ten items I would like you to copy them onto
another *** of paper. The FOR loop will assign these 10 items one at
a time to a container called "$i" (it's a scalar variable) and
perform the PRINT function on each item before getting the next one in
the list.
So what the line states is this;
for each item in the box $i, print the item followed by a carriage
return "that's the \n characters" and then go back to the beginning
of line Three. Keep doing this until you have processed all of the
items in the list. Then leave the loop and continue onto the next line
of instruction.
So in turn the box $i will contain the values 1 through 10, 15 and then
20 through 25. The curly brackets contain the instructions on what to
do with the value of $i). In this case it prints it to the screen.
Can somebody explain this same code whilst reading a text file into an
array? What is the syntax to read a file called "shopping-list.txt"
into an array and print each item out to the screen?
How does perl decide what characters or words or lines will be assigned
to what element of the array? A line at a time? A word at a time? A
letter at a time etc..
I would love to see a website that explained things in this way!
Many Thanks
Andy
.
- Follow-Ups:
- Re: the FOR Loop in the English Language
- From: Paul Lalli
- Re: the FOR Loop in the English Language
- From: usenet
- Re: the FOR Loop in the English Language
- Prev by Date: Perl Availability Checker
- Next by Date: Re: Hash balanced initialization dilemma
- Previous by thread: Perl Availability Checker
- Next by thread: Re: the FOR Loop in the English Language
- Index(es):