Re: Counting number of characters in a string
by chromatic (Archbishop) on Mar 13, 2001 at 01:13 UTC
|
| [reply] |
|
$cnt = @{[$str =~ /(\.)/g]};
| [reply] [d/l] |
|
This does not work for me. I expect the length of the string 'abc' to be 3, but your code returns 0:
use strict;
use warnings;
my $str = 'abc';
my $cnt = @{[$str =~ /(\.)/g]};
print "$cnt\n";
__END__
0
| [reply] [d/l] |
|
|
|
hi all,
counting number of characters is so easy..
use:length($stringname);
$x=length($stringname);
| [reply] |
|
the shortest ones are the best Rudif
| [reply] |
(jcwren) Re: Counting number of characters in a string
by jcwren (Prior) on Mar 13, 2001 at 02:42 UTC
|
#!/usr/local/bin/perl -w
use strict;
{
my $string = "Oh please, good sir program, what is my length?";
printf ("The length is %d characters\n", howlong ($string));
use Inline C => <<'END_OF_C_CODE';
#include <string.h>
int howlong(char *s)
{
return strlen (s);
}
END_OF_C_CODE
}
--Chris
e-mail jcwren | [reply] [d/l] |
|
| [reply] |
|
use strict;
{
my $string = "Oh please, good sir program, what is my length?";
printf ("The length is %d characters\n", howlong ($string));
use Inline C => <<'END_OF_C_CODE';
#include <string.h>
int howlong(char *s)
{
return strlen (s);
}
END_OF_C_CODE
}
| [reply] [d/l] |
|
| [reply] |
Re: Counting number of characters in a string
by japhy (Canon) on Mar 13, 2001 at 01:22 UTC
|
| [reply] |
Re (tilly) 1: Counting number of characters in a string
by tilly (Archbishop) on Mar 13, 2001 at 02:29 UTC
|
my $len = map $_, $str =~ /(.)/gs;
| [reply] [d/l] |
Re: Counting number of characters in a string
by OeufMayo (Curate) on Mar 13, 2001 at 02:40 UTC
|
$string = "foobar";
$count = $string =~ s/(.)/$1/sg;
<kbd>--
my $OeufMayo = new PerlMonger::Paris({http => 'paris.mongueurs.net'});</kbd> | [reply] [d/l] |
Re (tilly - silly) 1: Counting number of characters in a string
by tilly (Archbishop) on Mar 13, 2001 at 02:48 UTC
|
{
$str =~ s/./\n/g;
local $/ = "";
$len = chomp($str);
}
| [reply] [d/l] |
Re: Counting number of characters in a string
by t0mas (Priest) on Mar 13, 2001 at 12:51 UTC
|
Yet Another Silly Way Of Counting Characters In A String (YASWOCCIAS):
$_="YASWOCCIAS";
s/./+1/g;
print eval;
/brother t0mas
| [reply] [d/l] |
|
Shouldn't that be
s/./+1/gs;
| [reply] |
|
$_="YASWOCCIAS"; don't contain any newlines so it doesn't matter in this case. In other cases it might, depending on how you define "character" in your application. For some apps (like word processors), s/\S/+1/g; might be the right choise if you like to use this algorithm in your production environment... :-)
/brother t0mas
| [reply] |
Re: Counting number of characters in a string
by Falkkin (Chaplain) on Mar 13, 2001 at 02:38 UTC
|
$str =~ s/.*//;
print 0;
| [reply] [d/l] |
|
| [reply] |
(tye)Re: Counting number of characters in a string
by tye (Sage) on Mar 13, 2001 at 02:35 UTC
|
my $count= 0;
for(0..255){
my $c= pack "C", $_;
$count += () = $str =~ /\Q$c\E/g;
}
Warning! This probably yields an inaccurate count for Unicode strings.
-
tye
(but my friends call me "Tye") | [reply] [d/l] |
|
my $count= 0;
for( keys %{ { map {$_,1} unpack "C*", $str } } ){
my $c= pack "C", $_;
$count += () = $str =~ /\Q$c\E/g;
}
-
tye
(but my friends call me "Tye") | [reply] [d/l] |
Re: Counting number of characters in a string
by dfog (Scribe) on Mar 14, 2001 at 01:05 UTC
|
my $String = "This is the way I do it";
my @Chars = split("", $String);
print ++$#Chars;
| [reply] [d/l] |
|
print scalar @Chars;
instead of:
print ++$#Chars;
Then you won't modify the array size.
--sacked | [reply] [d/l] [select] |
Re: Counting number of characters in a string
by archon (Monk) on Mar 14, 2001 at 01:44 UTC
|
use strict;
use IPC::Open2;
my $pid = open2 (\*RDRFH, \*WTRFH, '/usr/bin/wc', '-c')
or die "Couldn't open pipe to wc: $!";
if ($pid) {
my $string = "shoes and ships and sealing wax";
print WTRFH $string;
close (WTRFH);
my $len = <RDRFH>;
$len =~ s/^\s+//;
print "length: $len";
close (RDRFH);
}
| [reply] [d/l] |
Re: Counting number of characters in a string
by petral (Curate) on Mar 15, 2001 at 01:30 UTC
|
Count the dots using a regular expression:
$str="There's many more ways to do it.\n";
$str =~ tr/\000-\xff/./;
while ( $str =~ /(.)/g ) {
$count++;
}
You can check this using a different method:
$count2++ while chop $str;
$count eq $count2 or warn "there's something wrong with this code.\n";
p | [reply] [d/l] [select] |
Re: Counting number of characters in a string
by Anonymous Monk on Mar 13, 2001 at 01:49 UTC
|
@arrayToReceiveChars = split(//,$varHoldingString);
$len = @arrayToReceiveChars;
print $len; | [reply] |
|
if you're not in a subroutine, doing a split in scalar context: $length=split(//, $string)
acheives the same thing. Don't do this in a subroutine though, or else you'll clobber your @_.
| [reply] |