RE: Why is this a problem?

2005-07-01 Thread Gardner, Sam
Title: Message



See 
answers:
my %fh = ( aa = 1, bb =2, cc = 3) ; 
# note that each instance of $fh{$fhkey} has a different valueforeach my 
$fhkey ( keys %fh){ open ( $fh{$fhkey}, "$fhkey" 
) ;## so, you open 3 file handles, 
one named "1", one named "2", and one named "3" 
}

non-working 
code 
my %fh = ( aa = 1, bb =1, cc = 1) 
; # note - only change, each $fh{} has value of 
1foreach my $fhkey ( keys %fh){ open ( 
$fh{$fhkey}, "$fhkey" ) ; }## 
Mmmm, because you only 
open one uniquely named file handle? Specifically, named 
"1"?

  
  
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Why is this a problem?

2005-07-01 Thread $Bill Luebkert
Hugh Loebner wrote:

 Hello perl gurus,
 
 I spent almost a whole day solving this problem.  However, I wonder why
 it's a problem.

 #
 # The following program (which is what I started out with) will NOT work
 properly:  Each element of %fh has a value of 1
 # Why doesn't this work properly?  - all statements write to one file
 #
 
 
 my%fh = ( aa = 1, bb =1, cc = 1) ;# note - only change, each
 $fh{} has value of 1

The proper way to do this would be to use Symbol:

use Symbol;
my %fh = (aa = gensym, bb = gensym, cc = gensym);

Barring that, you could use a ref to typeglob :

my %fh = (aa = \*1, bb = \*2, cc = \*3);

If you use 1,1,1 instead of 1,2,3 - then they'll all be pointing to the
same symbolic reference and essentially the same file.

 foreach my $fhkey ( keys %fh){
 open  ( $fh{$fhkey}, $fhkey )  ;   
 }

 foreach my $fhkey ( keys %fh ){
 print { $fh{$fhkey} }  xxx $fhkey xxx \n ;# note file handle
 $fh{$fhkey} has to be in a block   
 } ;
 
 foreach my $fhkey ( keys %fh ){
 close $fh{$fhkey} ;   
 }

-- 
  ,-/-  __  _  _ $Bill LuebkertMailto:[EMAIL PROTECTED]
 (_/   /  )// //   DBE CollectiblesMailto:[EMAIL PROTECTED]
  / ) /--  o // //  Castle of Medieval Myth  Magic http://www.todbe.com/
-/-' /___/__/_/_http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Why is this a problem?

2005-07-01 Thread Chris Wagner
At 09:20 AM 7/1/05 -0400, [EMAIL PROTECTED] wrote:
# this works fine, produces three files, aa , bb, cc with contents 'xxx aa 
xxx' etc

my %fh = ( aa = 1, bb =2, cc = 3) ; # note that each instance of 
$fh{$fhkey} has a different value

foreach my $fhkey ( keys %fh){
open ( $fh{$fhkey}, $fhkey ) ; 
}

In this case, ur successively opening three file handles, with the values,
1, 2, and 3.  If you want the file handle to be the name of the key, it
should be $fhkey not $fh{$fhkey}.

# The following program (which is what I started out with) will NOT work 
properly: Each element of %fh has a value of 1
# Why doesn't this work properly? - all statements write to one file
my %fh = ( aa = 1, bb =1, cc = 1) ; # note - only change, each $fh{} has 
value of 1
foreach my $fhkey ( keys %fh){
open ( $fh{$fhkey}, $fhkey ) ; 
}

Here ur opening the same file handle, 1, three times.








--
REMEMBER THE WORLD TRADE CENTER ---= WTC 911 =--
...ne cede males

0100

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Why is this a problem?

2005-07-01 Thread Jim Guion








Because the first parameter passed to open
is the file handle. In the first case, there are 3 file handles: 1,2,3. In
the second case, the same file handle is used for each file, always 1.












From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Hugh Loebner
Sent: Friday, July 01, 2005 9:21
AM
To: Perl-Win32-Users Mailing List;
activeperl@listserv.ActiveState.com
Subject: Why is this a problem?





Hello perl gurus,

I spent almost a whole day solving this problem. However, I wonder why
it's a problem.

# 
use strict;
no strict refs ;

# this works fine, produces three files, aa , bb, cc with contents 'xxx aa xxx'
etc

my %fh = ( aa = 1, bb =2, cc = 3) ; # note
that each instance of $fh{$fhkey} has a different value

foreach my $fhkey ( keys %fh){
 open ( $fh{$fhkey}, $fhkey )
; 
}

foreach my $fhkey ( keys %fh ){
 print { $fh{$fhkey} } xxx $fhkey xxx \n
; # note file handle $fh{$fhkey} has to be in a block
} ;

foreach my $fhkey ( keys %fh ){
 close $fh{$fhkey} ; 
}

#
# The following program (which is what I started out with) will NOT work
properly: Each element of %fh has a value of 1
# Why doesn't this work properly? - all statements write to one file
#


my %fh = ( aa = 1, bb =1, cc = 1)
; # note - only change, each $fh{} has value of 1

foreach my $fhkey ( keys %fh){
 open ( $fh{$fhkey}, $fhkey )
; 
}

foreach my $fhkey ( keys %fh ){
 print { $fh{$fhkey} } xxx $fhkey xxx \n
; # note file handle $fh{$fhkey} has to be in a
block 
} ;

foreach my $fhkey ( keys %fh ){
 close $fh{$fhkey} ; 
}

# 
Hugh Loebner






___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Why is this a problem?

2005-07-01 Thread Thomas, Mark - BLS CTR
Hugh Loebner wrote:
 foreach my $fhkey ( keys %fh){
 open  ( $fh{$fhkey}, $fhkey )  ;
 }

Nobody mentioned this yet: Since you want your filehandles to be unique, why
not use the key of the hash instead of the value? In other words, change the
above to:

foreach my $fhkey ( keys %fh){
open  ( $fhkey, $fhkey )  ;
}

- Mark.


___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs