Re: Text munging problem: question on while loop differences

2007-04-06 Thread Jeni Zundel

What is the difference between:

a.  while(defined(my $line = )) ...

and

b.  while()



On Apr 6, 2007, at 7:52 AM, Rob Dixon wrote:




Why shift and unshift?  Just use a for loop:
#!/usr/bin/perl
use strict;
use warnings;
while (defined (my $line = )) {
   chomp $line;
   my $record;
   for my $field (split /\|/, $line) {
   $field =~ s/\b(.)(.*?)\b/\u$1\L$2/g;
   $record .= $field|;
   }
   print $record\n
}
We don't even really need the for loop; it is just there if we  
want to
avoid processing a field later.  That means we can make a really  
short

one liner:
perl -pe 's/\b(.)(.*?)\b/\u$1\L$2/g' file


s/(\S+)/\u\L$1/g;

Rob


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Text munging problem: question on while loop differences

2007-04-06 Thread Jeff Pang
: Re: Text munging problem:  question on while loop differences

What is the difference between:

a. while(defined(my $line = )) ...

and

b. while()

Hello,

When you say my $line =  you read the content from input (maybe file or 
terminal STDIN or pipe) line by line and store it on variable $line.but when 
you just say while() this would store each line on Perl's builtin variable 
$_.



--
mailto: [EMAIL PROTECTED]
http://home.arcor.de/jeffpang/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Text munging problem: question on while loop differences

2007-04-06 Thread Jeni Zundel
So, if I put a filehandle in the diamond, instead of empty diamond,  
does that mean that the first would operate line by line and the  
second would pull the whole file into memory?


Thanks much,
Jen


On Apr 6, 2007, at 9:31 AM, Jeff Pang wrote:


: Re: Text munging problem:  question on while loop differences


What is the difference between:

a.  while(defined(my $line = )) ...

and

b.  while()


Hello,

When you say my $line =  you read the content from input (maybe  
file or terminal STDIN or pipe) line by line and store it on  
variable $line.but when you just say while() this would store  
each line on Perl's builtin variable $_.




--
mailto: [EMAIL PROTECTED]
http://home.arcor.de/jeffpang/

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Text munging problem: question on while loop differences

2007-04-06 Thread Chas Owens

On 4/6/07, Jeni Zundel [EMAIL PROTECTED] wrote:

What is the difference between:

a.  while(defined(my $line = )) ...
and
b.  while()

snip

while () {}

is shorthand for

while (defined ($_ = )) {}

The biggest difference is the use of a named variable $line instead of
the default variable $_.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Text munging problem: question on while loop differences

2007-04-06 Thread Jeff Pang


So, if I put a filehandle in the diamond, instead of empty diamond,  
does that mean that the first would operate line by line and the  
second would pull the whole file into memory?


When the diamond() is appeared with while(),it means you read the file line 
by line.
ig,while(FILEHANDLE){ .. } and while(} { .. } are almost the same.

When the diamond() is appeared on list context,it would read all file content 
in memory at one time.
ig,
my @file = FILEHANDLE;
or,
my @file = sort { ...} FILEHANDLE;
my @file = map { ...} FILEHANDLE;
my @file = grep { ... } FILEHANDLE;

This would slurp all the file content into memory.

but,when the diamond() is appeared on scalar context,it just read one line 
each time.
ig,
my $file = scalar FILEHANDLE; 
or,
my $file = FILEHANDLE;
or just say,
FILEHANDLE;

This would read only one line in that file into memory.



--
mailto: [EMAIL PROTECTED]
http://home.arcor.de/jeffpang/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Text munging problem: question on while loop differences

2007-04-06 Thread Chas Owens

On 4/6/07, Jeni Zundel [EMAIL PROTECTED] wrote:

So, if I put a filehandle in the diamond, instead of empty diamond,
does that mean that the first would operate line by line and the
second would pull the whole file into memory?


No, both while loops read line by line.  The difference is where the
line gets stored.  In longer loops using a named variable is
preferable (due to conflicts on $_ usage).  It was probably overkill
for the loop in the example code, but I used it because I was already
writing a verbose program.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Text munging problem: question on while loop differences

2007-04-06 Thread Chas Owens

On 4/6/07, Jeff Pang [EMAIL PROTECTED] wrote:



So, if I put a filehandle in the diamond, instead of empty diamond,
does that mean that the first would operate line by line and the
second would pull the whole file into memory?


When the diamond() is appeared with while(),it means you read the file line 
by line.
ig,while(FILEHANDLE){ .. } and while(} { .. } are almost the same.

When the diamond() is appeared on list context,it would read all file content 
in memory at one time.
ig,
my @file = FILEHANDLE;
or,
my @file = sort { ...} FILEHANDLE;
my @file = map { ...} FILEHANDLE;
my @file = grep { ... } FILEHANDLE;

This would slurp all the file content into memory.

but,when the diamond() is appeared on scalar context,it just read one line 
each time.
ig,
my $file = scalar FILEHANDLE;
or,
my $file = FILEHANDLE;
or just say,
FILEHANDLE;

This would read only one line in that file into memory.

snip

Where a line is defined as a string of bytes ending with the value
in $/ (usually \n)

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/