Re: the FOR Loop in the English Language
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 30 Jun 2006 09:34:15 -0700
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
.
- References:
- the FOR Loop in the English Language
- From: jm-1
- the FOR Loop in the English Language
- Prev by Date: Re: Variable inside regular expression
- Next by Date: Re: using MS Excel generated .xml files
- Previous by thread: Re: the FOR Loop in the English Language
- Next by thread: Script Required to Check a range of IP's
- Index(es):
Relevant Pages
|