Newsgroups: comp.lang.perl
Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!sid
From: sid@think.com (Sid Stuart)
Subject: Re: Associative arrays as indirect filehandles?
Message-ID: <1991Jun12.115103.7436@Think.COM>
Sender: news@Think.COM
Reply-To: sid@think.com
Organization: Thinking Machines Corporation, Cambridge MA, USA
References: <1991Jun11.230027.17017@Think.COM>
Distribution: na
Date: Wed, 12 Jun 91 11:51:03 GMT
Lines: 64

>>>I have a program in which I would like to split a file
>>>up into smaller files, based on the data in the main file.
>>>For example, if a line has the word moop in it, I would
>>>like to put it in the file moop. To do this I would like
>>>to use an associative array as indirect filehandles. Perl
>>>kind of wants to let me do this, but not all the way.
>>>(Reminds me of my first girl friend.) A call to the open
>>>command will pass the syntax checker when using an associative
>>>array, but a call to print fails with a syntax error.


Following a suggestion from someone on the net that I needed
to initialize the variable, I tried the following piece of
code to see if it would work:

  #!/usr/local/bin/perl
  $froob{'myfroob'} = "myfroob";
  open($froob{'myfroob'}, ">/tmp/testfile");
  print ($froob{'myfroob'} "Froob\n");


It didn't. It fails with a syntax erorr on the print statement.
I then tried the following routine to see if I could hack it
to work:

  #!/usr/local/bin/perl
  $froob{'myfroob'} = "myfroob";
  open($froob{'myfroob'}, ">/tmp/testfile");
  $tmphandle = $froob{'myfroob'};
  print ($tmphandle "Froob\n");

This did work. Just as I was about to accept the idea of sticking
this in my program, a light shown down from above, the
angels sang in the distance and I UNDERSTOOD. I don't
need to use associative arrays. I can just use a scalar
variable and change the value of the variable. The following
code, which does work as expected illustrates what I mean:

  #!/usr/local/bin/perl
  
  $filehandle = "opp";
  open($filehandle, ">/tmp/$filehandle");
  $filehandle = "moop";
  open($filehandle, ">/tmp/$filehandle");
  
  $filehandle = "opp";
  print $filehandle "Line 1\n";
  $filehandle = "moop";
  print $filehandle "Line 1\n";
  $filehandle = "opp";
  print $filehandle "Line 2\n";
  $filehandle = "moop";
  print $filehandle "Line 2\n";
  

Thanks to all on the net that took the time to read and
reply to my question.


-- 
Sid Stuart, Thinking Machines Corp.

sid@think.com
{uunet,harvard}!think!sid
