Re: A strange problem with array outside I/O

2010-12-03 Thread Odin Liu
Thanks to all. I have learned a lot tonight. Syntax sugar should be only used when I know clearly what happened. On Sat, Dec 4, 2010 at 12:34 AM, Jim Gibson wrote: > On 12/3/10 Fri Dec 3, 2010 8:26 AM, "Odin Liu" > scribbled: > > > Interesting. So if I need deal with an array of files, and I

Re: A strange problem with array outside I/O

2010-12-03 Thread Jim Gibson
On 12/3/10 Fri Dec 3, 2010 8:31 AM, "Odin Liu" scribbled: > No, actually I need deal with these files twice. So I need to keep the array > of these files. But after the first time dealing with these files, the array > of these files had been changed. Then you should modify your program so that

Re: A strange problem with array outside I/O

2010-12-03 Thread Jim Gibson
On 12/3/10 Fri Dec 3, 2010 8:30 AM, "Roche, Johnny" scribbled: > To my understanding, "$_" is the default input of the current running > script/program, hence, if you overwrite $_ in an inner loop, it will overwrite > $_ for the whole process. If you do not want to overwrite it, you'll need to

Re: A strange problem with array outside I/O

2010-12-03 Thread Odin Liu
Oh, it works! Thanks a lot. On Sat, Dec 4, 2010 at 12:29 AM, Shawn H Corey wrote: > On 10-12-03 11:26 AM, Odin Liu wrote: > >> Interesting. So if I need deal with an array of files, and I have to use >> the array after reading each file. I have to save another copy of the >> array? >> > > No, use

Re: A strange problem with array outside I/O

2010-12-03 Thread Uri Guttman
> "OL" == Odin Liu writes: OL> I consider $_ is a value-copy of the loop-element, but as shown in OL> a while loop inside a for loop, it seems that $_ is a reference of OL> the loop-element. it doesn't matter what you consider $_ to be. don't use it for loops. use a lexical variable wh

Re: A strange problem with array outside I/O

2010-12-03 Thread Uri Guttman
> "RJ" == Roche, Johnny writes: RJ> To my understanding, "$_" is the default input of the current RJ> running script/program, hence, if you overwrite $_ in an inner RJ> loop, it will overwrite $_ for the whole process. If you do not RJ> want to overwrite it, you'll need to assign $_

Re: A strange problem with array outside I/O

2010-12-03 Thread Shawn H Corey
On 10-12-03 11:40 AM, Odin Liu wrote: I consider $_ is a value-copy of the loop-element, but as shown in a while loop inside a for loop, it seems that $_ is a reference of the loop-element. The loop variable for a foreach loop is the actual element of the array. Consider: #!/usr/bin/perl u

Re: A strange problem with array outside I/O

2010-12-03 Thread Odin Liu
; > Johnny Roche > Associate, Help Desk > Harvard Management Company > Tel: 617-720-6827 > Fax: 617-878-6827 > Email: roc...@hmc.harvard.edu > > -Original Message- > From: Odin Liu [mailto:odinushu...@gmail.com] > Sent: Friday, December 03, 2010 11:26 AM > To:

Re: A strange problem with array outside I/O

2010-12-03 Thread Shawn H Corey
On 10-12-03 11:30 AM, Roche, Johnny wrote: To my understanding, "$_" is the default input of the current running script/program, hence, if you overwrite $_ in an inner loop, it will overwrite $_ for the whole process. If you do not want to overwrite it, you'll need to assign $_ to a scalar be

RE: A strange problem with array outside I/O

2010-12-03 Thread Roche, Johnny
Sent: Friday, December 03, 2010 11:33 AM To: beginners@perl.org Subject: Re: A strange problem with array outside I/O On 10-12-03 11:31 AM, Odin Liu wrote: > No, actually I need deal with these files twice. So I need to keep the > array of these files. But after the first time dealing with

Re: A strange problem with array outside I/O

2010-12-03 Thread Jim Gibson
On 12/3/10 Fri Dec 3, 2010 8:26 AM, "Odin Liu" scribbled: > Interesting. So if I need deal with an array of files, and I have to use the > array after reading each file. I have to save another copy of the array? No. You need to learn where and when it is appropriate to use the default variable

Re: A strange problem with array outside I/O

2010-12-03 Thread Shawn H Corey
On 10-12-03 11:31 AM, Odin Liu wrote: No, actually I need deal with these files twice. So I need to keep the array of these files. But after the first time dealing with these files, the array of these files had been changed. Only if you try to use $_ twice. Use a lexical variable for all your

Re: A strange problem with array outside I/O

2010-12-03 Thread Odin Liu
mpany > Tel: 617-720-6827 > Fax: 617-878-6827 > Email: roc...@hmc.harvard.edu > -Original Message- > From: Shawn H Corey [mailto:shawnhco...@gmail.com] > Sent: Friday, December 03, 2010 11:16 AM > To: Odin Liu; beginners@perl.org > Subject: Re: A strange problem with array

RE: A strange problem with array outside I/O

2010-12-03 Thread Roche, Johnny
AM To: Shawn H Corey; beginners@perl.org Subject: Re: A strange problem with array outside I/O Interesting. So if I need deal with an array of files, and I have to use the array after reading each file. I have to save another copy of the array? Anyway, thanks for helping. On Sat, Dec 4, 2010 at 12

Re: A strange problem with array outside I/O

2010-12-03 Thread Shawn H Corey
On 10-12-03 11:26 AM, Odin Liu wrote: Interesting. So if I need deal with an array of files, and I have to use the array after reading each file. I have to save another copy of the array? No, use a lexical variable for each loop: foreach my $file ( @ins ){ while( defined( my $line = <$in_fh>

Re: A strange problem with array outside I/O

2010-12-03 Thread Odin Liu
Interesting. So if I need deal with an array of files, and I have to use the array after reading each file. I have to save another copy of the array? Anyway, thanks for helping. On Sat, Dec 4, 2010 at 12:16 AM, Shawn H Corey wrote: > On 10-12-03 11:12 AM, Odin Liu wrote: > >> Still confusing. We

RE: A strange problem with array outside I/O

2010-12-03 Thread Roche, Johnny
0-6827 Fax: 617-878-6827 Email: roc...@hmc.harvard.edu -Original Message- From: Shawn H Corey [mailto:shawnhco...@gmail.com] Sent: Friday, December 03, 2010 11:16 AM To: Odin Liu; beginners@perl.org Subject: Re: A strange problem with array outside I/O On 10-12-03 11:12 AM, Odin Liu wrot

Re: A strange problem with array outside I/O

2010-12-03 Thread Shawn H Corey
On 10-12-03 11:12 AM, Odin Liu wrote: Still confusing. We know that inner loop's $_ overwrite the outer loop's $_ only in the inner loop. When the inner loop was over, the $_ would be it was in outer loop before the inner loop begin. Outer $_ would be restored by Perl automatically. But why this

Re: A strange problem with array outside I/O

2010-12-03 Thread Shawn H Corey
On 10-12-03 10:36 AM, Odin Liu wrote: Suppose file test.pl is: my @ins = @ARGV; foreach(@ins) { open IN, $_; # the three argument open is preferred. See `perlodc -f open`. open my $in_fh, '<', $_ or die "could not open $_: $!\n"; while() { while( defined( my $line = <$in_f

A strange problem with array outside I/O

2010-12-03 Thread Odin Liu
Hi, all Suppose file test.pl is: my @ins = @ARGV; foreach(@ins) { open IN, $_; while() { chomp; print "@ins\n"; } close IN; } and file case is: a b c When I run it in shell, using: $ perl test.pl case I thought it would be print: case