Re: the FOR Loop in the English Language



jm-1@xxxxxxxxxxxxxxxxx wrote:
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

That may be acceptable for your grandmother, but not for a Perl
programmer. An array is *not* a list. They are separate and distinct
(though obviously related) concepts. Please see:

perldoc -q list.*array
What is the difference between a list and an array


(like a
shopping list) that can contain almost any value you like.

No need for such impreciseness as "almost". Very specifically, lists
and arrays can contain any *scalar* value. Lists and arrays cannot
contain other lists or arrays, nor hashes. They can, however, contain
*references* arrays and hashes. References are also scalar values.

Can somebody explain this same code whilst reading a text file into an
array?

95% of the time, you don't want to do that. There is no need to read
an entire file's contents into memory, and then to process each element
of that new array. Instead, read one line at a time from the file,
process that line, and then read the next line.

What is the syntax to read a file called "shopping-list.txt"
into an array and print each item out to the screen?

Again, don't do that unless you have no other choice.
You do need to read:
perldoc -f open
perldoc -f readline
perldoc perlop (search for the $/ variable)

open my $fh, '<', 'shopping-list.txt' or die "Cannot open file: $!\n";
while (my $line = <$fh>) {
print $line;
}
close $fh;

How does perl decide what characters or words or lines will be assigned
to what element of the array?

Perl doesn't. The programmer does. The programmer most often accepts
the default of "one line at a time".

This is controlled by setting the $/ variable to whatever string you
want to terminate a "record". It is therefore referred to as the
"input record separator", and defaults to the newline ("\n"), so that
records are ended after each line.

A line at a time?

$\ = "\n";

A word at a time?

$\ = ' '; #terminate each record at a space

A letter at a time

Hmm. Not strictly possible, I don't think. The setting of $\ = '';
actually enacts a special case, whereby records are separated by
"paragraphs" (ie, sequences of two or more newlines).

etc..

For fancier record separators (ie, using regexps instead of plain
strings), see: perldoc File::Stream. There may be a way to get the
"letter at a time" behavior with that module, perhaps using lookaheads
or lookbehinds. All in all, it's probably easier to just read a line
at a time, and if you really need to work with each character, split
the line on the null-pattern.

my $line = <$fh>;
my @chars = split //, $line;

Paul Lalli

.



Relevant Pages

  • Re: Comments requested: brief summary of Perl
    ... | Perl uses the term list to describe an ordered collection of scalars. ... so the terms "array" and "list" are often thought of as being the same. ... | As a shortcut for lists of strings, you can use qw: ... | are related the same way as if and unless), and also do/while and do/until loops. ...
    (comp.lang.perl.misc)
  • Re: problem with eval
    ... What is in the @_ array? ... only subroutine arguments go to ... evaluates that string as *Perl* code. ... perldoc -f system ...
    (perl.beginners)
  • Re: garbage collection problem in large linked lists
    ... Instead all buckets are allocated at once in an array. ... VG.net, which must be very scalable, but only for lists which would normally ... Linked lists require pointer dereferencing in order to traverse. ... When you run out of space, you allocate a new smaller array. ...
    (microsoft.public.dotnet.framework.performance)
  • RE: Why doesnt this work?
    ... Any time you don't understand a Perl function, look it up via perldoc. ... the shiftin the sub: ... Removes the first element of an array and stores it in $search. ...
    (perl.beginners)
  • Re: Need help understanding an Array push
    ... complex script that generates some reports based on some text input ... Why are you reading in the whole file into an array? ... perldoc perldata ... It is know in Perl as a HoA ...
    (comp.lang.perl.misc)