develooper Front page | perl.beginners | Postings from March 2002

RE: reading one character at a time

Thread Previous | Thread Next
From:
Timothy Johnson
Date:
March 4, 2002 11:12
Subject:
RE: reading one character at a time
Message ID:
C0FD5BECE2F0C84EAA97D7300A500D500258102E@SMILEY

I don't think that applies to split(), however.  In this case it works works
works! :)

-----Original Message-----
From: Nikola Janceski [mailto:nikola_janceski@summithq.com]
Sent: Monday, March 04, 2002 10:55 AM
To: 'Jonathan E. Paton'; beginners@perl.org
Subject: RE: reading one character at a time


actually searching on a empty pattern is wrong wrong wrong!
a pattern in a search that evalutes to "" will use the previous successful
pattern match.

so the following:

if($x =~ /pattern/){
	print "yes\n" if($y =~ //); ## this really means if($y =~ /pattern/)
if you made it this far
	}
__END__

man perlop; #line 1100
             If the PATTERN evaluates to the empty string, the
             last successfully matched regular expression is used
             instead.


-----Original Message-----
From: Jonathan E. Paton [mailto:jonathanpaton@yahoo.com]
Sent: Monday, March 04, 2002 1:29 PM
To: beginners@perl.org
Subject: RE: reading one character at a time


> Is there a perl function that reads one character at
> a time from a string and and returns that character?

There are VERY FEW situations that require this kind of
action... but assuming you have one then the well proven
shortest approach is:

$string = "Hello World";
@string = $string =~ /./g;

OR:

$string = "Hello World";
for ($string =~ /./g) { print $_."\n"}

Split on a null length string is probably more readable,
but this simple technique works for pairs of characters...
and allows you to avoid pack() and unpack() a little
longer!

Take care,

Jonathan Paton

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

----------------------------------------------------------------------------
--------------------
The views and opinions expressed in this email message are the sender's
own, and do not necessarily represent the views and opinions of Summit
Systems Inc.


-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org


--------------------------------------------------------------------------------
This email may contain confidential and privileged 
material for the sole use of the intended recipient. 
If you are not the intended recipient, please contact 
the sender and delete all copies.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About