LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-21-2007, 06:51 AM   #1
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Rep: Reputation: 30
Perl substr goes crazy when presented with zero's


Im relatively new to Perl, and well heres my problem:
If I run the following program
Code:
#!/usr/bin/perl

@lines=("<table width=811>\r\n","</table>\r\n");

foreach(@lines)
{
	$x=0;
	while($c=substr($_,$x,1))
	{
		print $c;
		$x++;
	}
}
I get the correct output:
Code:
<table width=811>
</table>
cool. but if I change it to:
Code:
#!/usr/bin/perl

@lines=("<table width=800>\r\n","</table>\r\n");

foreach(@lines)
{
	$x=0;
	while($c=substr($_,$x,1))
	{
		print $c;
		$x++;
	}
}
I get this output:
Code:
<table width=8</table>
Seems like when it encounters a zero it just bails out completely and moves on to the next iteration of the loop.
 
Old 01-21-2007, 08:47 AM   #2
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

Yes, that's how while knows when to stop:
Quote:
The number 0, the strings '0' and '', the empty list "()", and "undef" are all false in a boolean context. All other values are true.
...
"while" repeats the statement while the condition is true.
-- man perlsyn
I'm not sure what you are trying to do, but function index, or defined might be of some use ... cheers, makyo
 
Old 01-21-2007, 08:54 AM   #3
tbutttbutt
Member
 
Registered: Jan 2006
Location: India
Distribution: Fedora 7, Linuxfromscratch 6.2
Posts: 66

Rep: Reputation: 15
You could print the strings as they are:

print "<table width=800>\r\n","</table>\r\n";

The '\n's and '\r's inside will get interpreted and you should get the result you want.
 
Old 01-21-2007, 08:55 AM   #4
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Original Poster
Rep: Reputation: 30
thanks I should have figured that out.
 
Old 01-21-2007, 09:49 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
I can't imagine why you'd want to go through a string in this manner, but assuming it's just some experimentation, you could do it by adding a defined(...):
Code:
#!/usr/bin/perl

@lines=("<table width=800>\r\n","</table>\r\n");

foreach(@lines)
{
	$x=0;
	while(defined($c=substr($_,$x,1)))
	{
		print $c;
		$x++;
	}
}
A [probably slightly less weird] way to iterate over each character in a string might be like this:
Code:
my $str = "J. R. Bob Dobbs";
for (my $i=0; $i<length($str); $i++) { 
    print "got char: " . substr($str,$i,1) . "\n";
}
Or maybe if you're a smartass:
Code:
my $str = "Hello"; 
$str=reverse $str; 
while(length($str) > 0) { 
    print "gimme a " . chop($str) . "\n";
}
 
  


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
perl: using 'substr' in regex ananthbv Programming 5 11-03-2004 01:58 AM
PHP (substr) & UTF-8 liquid sky Programming 3 09-08-2004 12:26 PM
C++: char* to string, converting? rewriting program? need substr! lrt2003 Programming 2 06-19-2004 03:20 AM
Apache: login dialog presented without .htaccess dkochan Linux - Networking 2 02-29-2004 07:39 PM
how to substr ? black Programming 5 12-23-2002 12:39 AM

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

All times are GMT -5. The time now is 03:20 AM.

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