For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > March 2004 > split string to chars









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author split string to chars
Frank de Bot

2004-03-29, 10:35 am

Hi,

I got the following case

$string = "Test String";
@split = split(//,$string);

in each element of @split will be each character as string ( 'T\0' for
example)
But I want to have only the char in each element of the array, so this
could be done for example:

for ($i=0;$i<@split;$i++) {
printf "This char is: %c\n", $split[$i];
}

Is there any way to do this? I do know how to do this in C, but I've no
realy clue how to do this in perl.

thanks in advanced,

Frank de Bot
Gunnar Hjalmarsson

2004-03-29, 11:47 am

Frank de Bot wrote:
> I got the following case
>
> $string = "Test String";
> @split = split(//,$string);
>
> in each element of @split will be each character as string ( 'T\0'
> for example)
> But I want to have only the char in each element of the array, so
> this could be done for example:
>
> for ($i=0;$i<@split;$i++) {
> printf "This char is: %c\n",
> $split[$i]; }


Is this what you mean:

@split = map ord, split //, $string;

perldoc -f map
perldoc -f ord

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Abhinav

2004-03-29, 11:47 am



Gunnar Hjalmarsson wrote:

> Frank de Bot wrote:
>

If i got the question correctly, the OP wants to remove the "\0" ?

Just that in perl there is no "\0"[color=darkred]
print "This char is : $split[$i]\n"
,[color=darkred]
>
>
> Is this what you mean:
>
> @split = map ord, split //, $string;
>
> perldoc -f map
> perldoc -f ord
>

Is this what is required ??

--
Abhinav

Anno Siegel

2004-03-29, 11:47 am

Frank de Bot < ppi_doesnt_like_@spam_inhis_email_search
y.net> wrote in comp.lang.perl.misc:
> Hi,
>
> I got the following case
>
> $string = "Test String";
> @split = split(//,$string);
>
> in each element of @split will be each character as string ( 'T\0' for
> example)


Perl strings have no trailing null byte. Their length is kept elsewhere.

> But I want to have only the char in each element of the array, so this
> could be done for example:
>
> for ($i=0;$i<@split;$i++) {
> printf "This char is: %c\n", $split[$i];
> }


There's no need to use indexing to access array elements (untested):

printf "This char is: %s\n", $_ for @split;

Note that I changed the printf format to "%s". Perl represents single
characters as strings of length 1. There is no character data type.
As in C, the "%c" format prints a character whose numeric value is given,
so to use the "%c" format, you'd write

printf "This char is: %s\n", ord $_ for @split;

But nobody does that.

Anno
Frank de Bot

2004-03-29, 11:47 am

Gunnar Hjalmarsson wrote:
> Frank de Bot wrote:
>
>
>
> Is this what you mean:
>
> @split = map ord, split //, $string;
>
> perldoc -f map
> perldoc -f ord
>


This is exactly what I ment. Thanks :-D
Frank de Bot

2004-03-29, 11:47 am

Anno Siegel wrote:
> Frank de Bot < ppi_doesnt_like_@spam_inhis_email_search
y.net> wrote in comp.lang.perl.misc:
>
>
>
> Perl strings have no trailing null byte. Their length is kept elsewhere.
>
>
>
>
> There's no need to use indexing to access array elements (untested):
>
> printf "This char is: %s\n", $_ for @split;


sometimes it is handy (to me) if I know on which element it is, making
such loops is something I was kinda thaught when I was starting to learn
perl ;-)
>
> Note that I changed the printf format to "%s". Perl represents single
> characters as strings of length 1. There is no character data type.
> As in C, the "%c" format prints a character whose numeric value is given,
> so to use the "%c" format, you'd write
>
> printf "This char is: %s\n", ord $_ for @split;
>
> But nobody does that.


Except me. I can explain a little what I need it for. the code I posted
was just a little piece of a big code. the @split originates from a
incoming stream (can be file or socket stream), but I sometime I don't
know if it's completly right.

>
> Anno

Anno Siegel

2004-03-29, 12:33 pm

Frank de Bot < ppi_doesnt_like_@spam_inhis_email_search
y.net> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> comp.lang.perl.misc:
>
> sometimes it is handy (to me) if I know on which element it is, making
> such loops is something I was kinda thaught when I was starting to learn
> perl ;-)


Even when the element count is wanted, most Perl programmers would write
that

my $count = 0;
for my $char ( @split ) {
# work with $char
$count ++;
}

and avoid the clumsy "$split[ $i]". Unlike an index, $count might as well
start at 1.

The only good reason for indexing into arrays in a loop is processing
parallel arrays, when you need the values from multiple arrays for
the same index. Even there are ways...

>
> Except me. I can explain a little what I need it for. the code I posted
> was just a little piece of a big code. the @split originates from a
> incoming stream (can be file or socket stream), but I sometime I don't
> know if it's completly right.


I was alluding to the "%c" format with "nobody does that".

For dumping suspect input, traditionally "%x" and "%o" have been used
together with ord(), but decimal format is also okay. In such a case,
you don't want the character output of either "%s" or "%c", because
control sequences can make it hard to understand.

Anno
Frank de Bot

2004-03-29, 1:49 pm

Anno Siegel wrote:
> Frank de Bot < ppi_doesnt_like_@spam_inhis_email_search
y.net> wrote in comp.lang.perl.misc:
>
>
>
> Even when the element count is wanted, most Perl programmers would write
> that
>
> my $count = 0;
> for my $char ( @split ) {
> # work with $char
> $count ++;
> }
>
> and avoid the clumsy "$split[ $i]". Unlike an index, $count might as well
> start at 1.
>
> The only good reason for indexing into arrays in a loop is processing
> parallel arrays, when you need the values from multiple arrays for
> the same index. Even there are ways...
>

hmmmm. good point..

>
>
>
> I was alluding to the "%c" format with "nobody does that".
>
> For dumping suspect input, traditionally "%x" and "%o" have been used
> together with ord(), but decimal format is also okay. In such a case,
> you don't want the character output of either "%s" or "%c", because
> control sequences can make it hard to understand.


Ok, what I've put here wasn't quite complete. The %c is almost always
accomponied with a %d in my debugging stuff. The %c is mostly handy to
let it be readable for me. If I get a lot of text I can faster see where
malicious bytes and so occure

>
> Anno

Anno Siegel

2004-03-29, 1:49 pm

Frank de Bot < ppi_doesnt_like_@spam_inhis_email_search
y.net> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> comp.lang.perl.misc:


>
> Ok, what I've put here wasn't quite complete. The %c is almost always
> accomponied with a %d in my debugging stuff. The %c is mostly handy to
> let it be readable for me. If I get a lot of text I can faster see where
> malicious bytes and so occure


printf "%d (%s)\n", ord $_, $_;

I don't think there is a good use for "%c" in Perl. Where would "characters
as integers" come from? Perl doesn't produce them (unless on demand), and
from a file you could as well read them as characters.

Anno
Uri Guttman

2004-03-29, 4:34 pm

>>>>> "AS" == Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> writes:

AS> I don't think there is a good use for "%c" in Perl. Where would
AS> "characters as integers" come from? Perl doesn't produce them
AS> (unless on demand), and from a file you could as well read them as
AS> characters.

it can be useful for various binary formats where individual bytes need
to be written or stored in a string. pack C/c or chr() can do the same
thing. (s)printf supports it because c's printf has it. the number may
be generated from an expression and you can't put it into a byte
directly or easily. timtowtdi and all.

uri
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com