CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Does Perl have a indexof for counting string characters?

 



kruzer
Deleted

Jun 19, 2000, 8:35 AM

Post #1 of 4 (371 views)
Does Perl have a indexof for counting string characters? Can't Post

Does Perl have a way of counting string characters? I want to count 22 characters in a string and then add a carriage return. I'm having problems with a string extending past the limited viewing area and I need to add a carriage return.

something similar to this

if($company.index == 22)
x = $company->write('\r');
Thanks,

Dano


Kanji
User / Moderator

Jun 19, 2000, 9:12 AM

Post #2 of 4 (371 views)
Re: Does Perl have a indexof for counting string characters? [In reply to] Can't Post

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

#!/usr/bin/perl -w

use strict;

my $string = "I want to count 22 characters [..] add a carriage return.";

if ( length( $string ) > 22 ) {
substr( $string, 22, 0 ) = "\n";
}

print $string, "\n";
__END__

I want to count 22 cha
racters [..] add a carriage return.</pre><HR></BLOCKQUOTE>

It's prolly easier just to use a regexp, though:-
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$string =~ s/(.{22})/$1\n/g;</pre><HR></BLOCKQUOTE>


kruzer
Deleted

Jun 19, 2000, 1:13 PM

Post #3 of 4 (371 views)
Re: Does Perl have a indexof for counting string characters? [In reply to] Can't Post

Using ‘\n\’ the results

‘Old town Furniture s
hop’
I tried a ‘\r\’ instead of ‘\n\’

‘Old town Furniture s’

Is there a way to keep ‘shop’ together and start a new line?




Kanji
User / Moderator

Jun 19, 2000, 1:35 PM

Post #4 of 4 (371 views)
Re: Does Perl have a indexof for counting string characters? [In reply to] Can't Post

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

#!/usr/bin/perl -w

use strict;

my $ugly = "Old town Furniture Shop";
my $pretty = "";
my $width = 10;

foreach my $word ( split /\s+/, $ugly ) {
$pretty .= "\n" if length( "$pretty $word" ) > $width;
$pretty .= "$word ";
}
# Strip all dangling whitespace.
$pretty =~ s/ (\n|$)/\n/sg;


print $pretty, "\n";

__END__
Old town
Furniture
Shop</pre><HR></BLOCKQUOTE>

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives