reading char by char in a string 
Author Message
 reading char by char in a string

How can I read special parts from a string? say:

i have string that contains: someinfo_moreinfo_evenmoreinfo_

I would like to store the info between the underscores ("_"). But I'm
still new at this and need help....

rg
J



Wed, 18 Jun 1902 08:00:00 GMT  
 reading char by char in a string
Try this...

($var1, $var2, $var3) = split(/_/ $string);

where
$string = someinfo_moreinfo_evenmoreinfo
$var1 will = someinfo
$var2 will = moreinfo
$var3 will = evenmoreinfo

Hope this helps.
Jason Malone


Quote:
> How can I read special parts from a string? say:

> i have string that contains: someinfo_moreinfo_evenmoreinfo_

> I would like to store the info between the underscores ("_"). But I'm
> still new at this and need help....

> rg
> J



Wed, 18 Jun 1902 08:00:00 GMT  
 reading char by char in a string

Quote:

>How can I read special parts from a string? say:

>i have string that contains: someinfo_moreinfo_evenmoreinfo_

That is not the same question that you put in your Subject header.

Please be a bit more precise in wording your questions.

I don't know which one to answer...

   reading char by char in a string

      perldoc -f sysread

      (but nobody really wants to do this, it will be slow)

   _processing_ char by char in a string

      foreach ( split //, $string ) {
         # do something with char in $_
      }

Quote:
>I would like to store the info between the underscores ("_").

So there should be two "matches"

   moreinfo
   evenmoreinfo

since someinfo is NOT between underscores?

Quote:
>But I'm
>still new at this and need help....

Though split(), as pointed out in another followup, will do the
job with the particular problem you presented, you can do it
with a regex too.

   perldoc perlre

   perldoc perlop

-----------------------------
#!/usr/bin/perl -w
use strict;

$_ = 'someinfo_moreinfo_evenmoreinfo_';

#while ( /_([^_]*)(?=_)/g ) {    # same thing, all on one line

while ( /_         # underscore
         (         # start remembering matched chars in $1
          [^_]*    # zero or more of anything except underscore
         )         # stop remembering matched chars
         (?=_)     # see if the next char is underscore, without
                   #    counting it in this match (because we
                   #    need it at the beginning of the _next_ match)
        /gx ) {

   print "$1\n";   # do something with the matched chars in $1

Quote:
}

-----------------------------

--
    Tad McClellan                          SGML Consulting

    Fort Worth, Texas



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Char position of 1st non-word char in a string

2. Appending char to strings by char

3. @chars the same as ($#chars) ?

4. Replacing special chars with special chars

5. Q: using a c-lib with char** and even char***

6. s/Hex/Char/ ? s/Char/Hex/

7. Why no regex char class for punc chars???

8. Replacing multiple occurences of newline chars with a single newline char

9. Match any char EXCEPT [char]

10. Char-by-char client/server with echo option!?

11. Char-by-char in internet server?

12. split a string not only by a single char but also by a string

 

 
Powered by phpBB® Forum Software