Solved

Foreach on a string.

Posted on 1998-07-19
8
295 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl match and sort unique result 2 129
HTTP Error 502.2 - Bad Gateway 3 205
Expect & sshpass package for Solaris 10 x86 15 896
Search in text file in column and compare 4 46
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now