Solved

Foreach on a string.

Posted on 1998-07-19
8
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

749 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