Re: hashes of arrays
by suaveant (Parson) on Apr 03, 2001 at 23:19 UTC
|
{
local $/;
foreach (@file) {
open(FILE,"$_");
$hash{$_}{file_contents} = <FILE>;
close(FILE)
}
}
that sets line separator to undef, and slurps in whole file instead of one line.
- Ant | [reply] [Watch: Dir/Any] [d/l] |
|
Another solution, since you said you wanted an array, is to read the file in array context, without changing the input line separator :
@{$hash{$_}{file_contents}} = <FILE>;
-- the array guy | [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] [d/l] |
|
#!/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.
| [reply] [Watch: Dir/Any] [d/l] |
|
woo.
Thanks... I'd like it emphasize that making it local to a block is a good thing ... for some reason i didn't bother when i tested it real quick ... ;-)
feel free to beat me for my stupidity.
Mbond.
| [reply] [Watch: Dir/Any] |
Re: hashes of arrays
by ton (Friar) on Apr 03, 2001 at 23:39 UTC
|
foreach (@file) {
open(FILE,"$_");
@{$hash{$_}{file_contents}} = <FILE>;
close(FILE)
}
note that $hash{$_}{file_contents} contains a _reference_ to an array, not an array.
| [reply] [Watch: Dir/Any] [d/l] |
Re: hashes of arrays
by chipmunk (Parson) on Apr 04, 2001 at 06:34 UTC
|
Here's an alternate syntax to store an array ref:
$hash{$_}{file_contents} = [ <FILE> ];
The result will be the same as the @{} form suggested above (except if $hash{$_}{file_contents} already has some non-array-ref value in it; the @{} form won't do the right thing in that case). | [reply] [Watch: Dir/Any] [d/l] |
Re: hashes of arrays
by SuperCruncher (Pilgrim) on Apr 04, 2001 at 02:51 UTC
|
This isn't really answering your question, but I feel
it is important to point out something that you're
doing.
open(FILE,"$_");
When you do "$foo", it incurs a slight
performance hit. Simply $foo on its
own is better. Also, apparently, if $foo
happens to be a scalar reference, Perl will dereference
it if you don't put it in quotes, but won't otherwise.
I doubt you want to open a file named with the memory
address of the reference?
See the part of the Perl FAQ that says "What's wrong
with always quoting "$vars"?" | [reply] [Watch: Dir/Any] [d/l] [select] |
|
The performance hit for stringfying $foo pales in comparison to the overhead of the open().
Don't worry about performance at this level. It's like trying to pry small stones out of your tires so that your car will go faster. It just isn't worth it. Worry about effective use of your time, and your reader's time, instead.
| [reply] [Watch: Dir/Any] [d/l] [select] |