LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-02-2004, 07:24 PM   #1
lfur
Member
 
Registered: Jul 2003
Location: Slovenia
Distribution: Slackware & FreeBSD
Posts: 209

Rep: Reputation: 30
loop through string with perl


Hi!

I bet this one is simple, but I just don't seem to get it. The thing is ... there is a string of characters stored in a variable that I want to loop through - get a character one by one including spaces and so on.
I tried something like I used to do with JavaScript but it just doesn't work.

Code:
for (my $key = 0; $key < length($word); $key++) {
  print substr ($letter, $key, $key + 1)."\n";
}
let us say that the string is 1234567890, then the output goes like this:

1
23
345
4567
56789
67890
7890
890
90
0

weird .... in JavaScript with substring() it works fine. Any ideas??

Thanks

lfur
 
Old 07-03-2004, 01:38 AM   #2
idaho
Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: RedHat, Libranet
Posts: 438

Rep: Reputation: 30
You are asking for a substring $key+1 characters long. You only want a substring 1 character long. You should use:
for (my $key = 0; $key < length($word); $key++) {
print substr ($letter, $key, 1)."\n";
}
 
Old 07-03-2004, 08:05 AM   #3
lfur
Member
 
Registered: Jul 2003
Location: Slovenia
Distribution: Slackware & FreeBSD
Posts: 209

Original Poster
Rep: Reputation: 30
Thanks a bunch ... just what I was looking for.

lfur
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to break if loop in Perl? Barca Programming 9 08-03-2011 01:15 PM
Perl: Where am I in a foreach loop? jrtayloriv Programming 3 01-30-2005 10:43 PM
Need help with perl loop! morbid_ru Programming 1 02-24-2004 01:14 PM
Need help with perl loop! morbid_ru Programming 2 02-17-2004 05:15 AM
Loop in this perl script is not working meluser Programming 2 04-01-2003 08:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration