Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Foreach on a string.

Posted on 1998-07-19
8
Medium Priority
?
304 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 40 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

609 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