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
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
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
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
> "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
> "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 $_
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
;
> 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:
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
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
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
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
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
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
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>
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
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
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
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
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
20 matches
Mail list logo