Ask a Question related to PERL Miscellaneous, Design and Development.
-
Vsevolod Afanassiev #1
How to put a filehandle into a hash array?
I need to build an array of filehandles
so different records go into different output files
depending on a value. This is the code:
$dt = 20030701;
open ( "$FH{$dt}", ">output" . "." . $dt ) or die "$!";
print { "FH{$dt}" } "something ...\n";
close ( "$FH{$dt}" );
It works when run without "-w" but with "-w"
complains of use of uninitialized value.
What's the correct way of doing it?
Thanks
Vsevolod Afanassiev Guest
-
Updating an array within a hash
Hi Everybdy, I am stuck in a problem for which I need your help. My problem spins around adding an element in an array within a hash. I have a... -
hash of hash of array slices
This works Foreach ( @{$hash{$key1}{$key2}} ) This does note Foreach ( @{($hash{$key1}{$key2})} ) This gives me this error .... Can't... -
adding an array as a hash value
Hi Dermot. Dermot Paikkos wrote: And use warnings; These variables need to have better names so that it's more -
Array and Hash to_s
On Saturday, July 19, 2003, at 12:56 PM, Yukihiro Matsumoto wrote: While we're on the subject of "to_s" and similar friends, I have a question... -
testing for array or scalar in a hash.
Hi, I have a hash containing, data, everything is generated on the fly so my hash, has some scalars, and some arrays, in it. How can I test wether... -
Eric J. Roode #2
Re: How to put a filehandle into a hash array?
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
[email]vafanassiev@aapt.com.au[/email] (Vsevolod Afanassiev) wrote in
news:4f7d504c.0307022359.64f19424@posting.google.c om:
Why are you putting quotes around $FH{$dt} everywhere?> I need to build an array of filehandles
> so different records go into different output files
> depending on a value. This is the code:
>
> $dt = 20030701;
>
> open ( "$FH{$dt}", ">output" . "." . $dt ) or die "$!";
>
> print { "FH{$dt}" } "something ...\n";
>
> close ( "$FH{$dt}" );
>
> It works when run without "-w" but with "-w"
> complains of use of uninitialized value.
> What's the correct way of doing it?
Lose the quotes and it should work.
- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print
-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>
iQA/AwUBPwP8WWPeouIeTNHoEQIXfQCgja/dLnvGJjnmeuEln0Yh3ekU3OgAoN9N
6RwEEc6oja0OwJnNLwO9B+AY
=6qa/
-----END PGP SIGNATURE-----
Eric J. Roode Guest
-
Matthew Browning #3
Re: How to put a filehandle into a hash array?
On Thu, 03 Jul 2003 00:59:59 +0000, Vsevolod Afanassiev wrote:
> I need to build an array of filehandles so different records go into
> different output files depending on a value. This is the code:
>
> $dt = 20030701;
>
> open ( "$FH{$dt}", ">output" . "." . $dt ) or die "$!";
If I understand your question correctly, you may find the IO::File
module helpful, which will let you do something like this:
my $file1 = IO::File->new( '>file1' );
my $file2 = IO::File->new( '>file2' );
push @filehandles, ( $file1, $file2 );
for( @filehandles ) {
# write something.
}
....which is a bit more tidy and intuitive.
Matthew Browning.
Matthew Browning Guest
-
Tad McClellan #4
Re: How to put a filehandle into a hash array?
Vsevolod Afanassiev <vafanassiev@aapt.com.au> wrote:
> I need to build an array of filehandles
perldoc -q filehandle
How can I make a filehandle local to a subroutine?
How do I pass filehandles between subroutines?
How do I make an array of filehandles?
^ ^> open ( "$FH{$dt}", ">output" . "." . $dt ) or die "$!";
^ ^
Why are you forcing stringification of the hash value?
See also:
perldoc -q quoting
What's wrong with always quoting "$vars"?
> print { "FH{$dt}" } "something ...\n";
You know that you are not accessing that same (or any) hash
value here, right?
> It works when run without "-w"
Then it will work _with_ warnings too, since warnings never change
how your program executes...
> What's the correct way of doing it?
The way the FAQ answer shows.
--
Tad McClellan SGML consulting
[email]tadmc@augustmail.com[/email] Perl programming
Fort Worth, Texas
Tad McClellan Guest



Reply With Quote

