How to put a filehandle into a hash array?

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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
    4. 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...
    5. 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...
  3. #2

    Default 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:
    > 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?
    Why are you putting quotes around $FH{$dt} everywhere?
    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

  4. #3

    Default 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

  5. #4

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139