Solved

Foreach on a string.

Posted on 1998-07-19
8
296 Views
Last Modified: 2010-03-05
Is there anyway to do a foreach loop on a string?  Or a similar loop.  So it loops through once for every char in the string.  So in the word "hello" it would loop 5 times and $k would hold the letter for which it's going through the loop for.
Thanks in Advance.
0
Comment
Question by:cide
  • 4
  • 3
8 Comments
 
LVL 6

Accepted Solution

by:
alamo earned 10 total points
ID: 1208515
$str ="'hello";
foreach $k (split('',$str)) { print "$k\n"};

This doesn't get you the ability to change the characters in the original string, though, of that's what you want. In that case a simple for loop would do, iterating character by character:

for ($i = 0; $i < length($str); $i++) { print substr($str,$i,1),"\n"; }


0
 
LVL 84

Expert Comment

by:ozo
ID: 1208516
#also,
for( $str=~/(.)/sg ){ print; }
while( $str=~/(.)/sg ){ print "$1\n"; }
#if you want to change each character:
$str=~s/(.)/$change{$1}/gs;
#if you want the ord of each character:
for( 0..length($str)-1 ){ $k = vec($str,$_,8) }
for( unpack'C*',$str ){print $_,"\n" }
0
 

Author Comment

by:cide
ID: 1208517
Thanks,  I don't actually need to change the string so that works perfectly.  I was origionally doing it the second way you said, but the first way is better.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:cide
ID: 1208518
How would I use split to make it every three characters? I.E.  it would split 1000000 into three elements in an array 1, 000 and 000 and 999999 would split into 999 and 999.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208519
For every three characters, /(...)/sg may be easier than using split
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208520
Wait a minute, in your 1,000 example, the dirst element doesn't have three characters.
Perhaps you want "How can I output my numbers with commas added?" from perlfaq5?
0
 

Author Comment

by:cide
ID: 1208521
Sort of but I don't want commas added.  exactly like the comma thing but I don't want commas.  Where the comma should be a new element should start.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208522
How about:
reverse map{''.reverse $_} (reverse($str)=~/(\d{1,3})/g);
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question