Re: the FOR Loop in the English Language



jm-1@xxxxxxxxxxxxxxxxx wrote:

@one_to_ten = (1 .. 10);
$top_limit = 25;
for $i (@one_to_ten, 15, 20 .. $top_limit) {
print "$i\n";
}

You would have less to explain if you didn't bother with the constraint
variables (maybe having $top_limit is OK, but @one_to_ten is silly).
And it would be more clear if you named your loop variable something
more sensible. Consider:

foreach my $number ( (1..10), 15, 20, (20..25) ) {
print "$number\n";
}

Of course, you don't need parens around (1..10) but I think it improves
readability.

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?

It's almost always unnecessary (and unwise) to read a file into an
array (if you try this with a very large file, you can easily run out
of memory). This is a common mistake in programming logic, especially
for new programmers. A file is usually processed one record at a time,
so just read one record at a time and process each record as it is
read. For example:

open($shopping_list, 'shopping-list.txt') or die "Oops - $!\n";
while ( my $item = <$shopping_list> ) {
print "$item\n";
}

For such a trivial loop, though, many programmers would simply do:

print while <$shopping_list>;

But if you insist on using the array,

my @unnecessary_array = <$shopping_list>; #bad idea

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..

Any of the above are possible. It depends on what you have defined as
a record separator. By default, the record separator is a newline
(whatever a newline is in your operating system), so it reads one line
at a time. You can set your record separator to whatever you like (if
you make it a space, Perl will read one word at a time). The value for
the record separator is contained in the Perl special variable $/. If
that name is too cryptic, you can "use English;" and then refer to it
by either $INPUT_RECORD_SEPARATOR or $RS.

I would love to see a website that explained things in this way!

Then you may appreciate Elizabeth Castro's 'Visual Quickstart' books.
You will outgrow them quickly, though.

--
David Filmer (http://DavidFilmer.com)

.



Relevant Pages

  • Re: local $/ = ;
    ... That is not Perl's "paragraph" mode. ... You have set the record separator to null, ... which causes the file to be split on lines containing _only_ a newline. ... is pushed into an array, you have a null array element for every record split. ...
    (perl.beginners)
  • Re: sed multiline match
    ... Thanks, but I still think sed should work fine in this case as awk does, ... define some record separator other than a newline. ... as a single record. ...
    (comp.unix.shell)
  • Re: match new line as first item in string
    ... the first character in a string (in this case a variable where I read ... If your record separator is newline then ... you had a blank line in your input data, ...
    (perl.beginners)