in reply to Re: hashes of arrays
in thread hashes of arrays

Two tiny nitbits:
  1. It's a good thing to put the local in a block (so ++), but with unexperienced users (the poster) it may be nice to note what it does. Well, the {local....} BLOCK restricts the scope of local to that block. So after the block, the $/ has it's original value (the line separator). One can read more about these and other vars in perlvar.
  2. (Really tiny)The foreach was copied from the original post (and it works), but here is a for more precise. A foreach loop is used to change the contents of an array, for is not. So for reading in a file, I would recommend for. A typical use for foreach:
    @array = 0..100; foreach $item (@array){ $item += 10 if $item <100; } print join "\n", @array; #prints the numbers 10 to 109 and 100
    You can read more about it in for vs foreach.
Hope this helps,

Jeroen
"We are not alone"(FZ)
Update: For clarity, for and foreach *are* synonyms. It's just a matter of convention....

Replies are listed 'Best First'.
Re: Re: Re: hashes of arrays
by Xxaxx (Monk) on Apr 04, 2001 at 12:38 UTC
    I tried the following:
    #!/usr/local/bin/perl use strict; my @array = ('one', 'two', 'three'); for my $element (@array) { $element = uc($element); } for my $element (@array) { print "$element\n"; } exit;
    The output was:
    ONE
    TWO
    THREE

    In this regard the for does in fact seem to be synonymous with foreach. In the extent that the elements of the array are changed both function the same.

    But I must admit that by programming habit and for the sake of readiblity I adhere to the convention:

    foreach loop to change the contents of an array
    "for" when I'm not.

    In spite of my habits and preferences apparently they both work the same. But alas my C background has me using for when I'm just stepping through array indexes.