Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Getting substrings within a String

Reply
Thread Tools

Getting substrings within a String

 
 
Tony
Guest
Posts: n/a
 
      08-02-2003
Hello All,
I am very new to Perl and I am trying to pull a substring out of a
larger string as such:

xyxyxyxyxyxyZZxyxyxyxyxyx

I know if I get the index of ZZ in the string can use substr($string,
$index, $len) where len is the number of charcters I want.

I am looking through the Programming Perl book from O'Reilly and I can
not find any easy way to search through the string and get this value.

I have done this several time in java like such and I am trying to the
same thing in Perl:

String str = "xyxyxyxyxyxyZZxyxyxyxyxyx";
int len1 = str.indexOf("ZZ");
String str2 = str.substring(len1, (len1 + "ZZ".length()));

I am sure between something I get here and what I am reading I will
figure this out. Thanks in advance for any help

-Tony
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-03-2003
Tony wrote:
> Hello All,
> I am very new to Perl and I am trying to pull a substring out of a
> larger string as such:
>
> xyxyxyxyxyxyZZxyxyxyxyxyx
>
> I know if I get the index of ZZ in the string can use substr($string,
> $index, $len) where len is the number of charcters I want.
>
> I am looking through the Programming Perl book from O'Reilly and I can
> not find any easy way to search through the string and get this value.
>
> I have done this several time in java like such and I am trying to the
> same thing in Perl:
>
> String str = "xyxyxyxyxyxyZZxyxyxyxyxyx";
> int len1 = str.indexOf("ZZ");
> String str2 = str.substring(len1, (len1 + "ZZ".length()));


I fail to understand the point with what you are trying to do. If you
already know which string you need, why bother with looking up the
index and then pull out just that string from a larger one??

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

 
Reply With Quote
 
 
 
 
Tony
Guest
Posts: n/a
 
      08-03-2003
Everyone,
Thanks for the response. I found the documentation for Index()
about 30 minutes after I posted. Thanks to everyone who responded I
apprciate it.

Gunnar to answer your question what I am trying to do is pull a
particular segent out of an EDI document so i can tell what doc it is
so I can route it via Perl to the corret place. I know what the
segment is but not the value. So a line could look like

*anything*ST*850*anything*......

I solved it as:

my $pos = index($str, "ST");


-Tony
http://www.velocityreviews.com/forums/(E-Mail Removed) (Tony) wrote in message news:<(E-Mail Removed). com>...
> Hello All,
> I am very new to Perl and I am trying to pull a substring out of a
> larger string as such:
>
> xyxyxyxyxyxyZZxyxyxyxyxyx
>
> I know if I get the index of ZZ in the string can use substr($string,
> $index, $len) where len is the number of charcters I want.
>
> I am looking through the Programming Perl book from O'Reilly and I can
> not find any easy way to search through the string and get this value.
>
> I have done this several time in java like such and I am trying to the
> same thing in Perl:
>
> String str = "xyxyxyxyxyxyZZxyxyxyxyxyx";
> int len1 = str.indexOf("ZZ");
> String str2 = str.substring(len1, (len1 + "ZZ".length()));
>
> I am sure between something I get here and what I am reading I will
> figure this out. Thanks in advance for any help
>
> -Tony

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-03-2003
Tony wrote:
> Gunnar to answer your question what I am trying to do is pull a
> particular segent out of an EDI document so i can tell what doc it
> is so I can route it via Perl to the corret place. I know what the
> segment is but not the value. So a line could look like
>
> *anything*ST*850*anything*......
>
> I solved it as:
>
> my $pos = index($str, "ST");


Thanks for explaining. Suddenly it makes very much sense.

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

 
Reply With Quote
 
Clay Irving
Guest
Posts: n/a
 
      08-03-2003
In article <(E-Mail Removed) >, Tony wrote:
> Everyone,
> Thanks for the response. I found the documentation for Index()
> about 30 minutes after I posted. Thanks to everyone who responded I
> apprciate it.
>
> Gunnar to answer your question what I am trying to do is pull a
> particular segent out of an EDI document so i can tell what doc it is
> so I can route it via Perl to the corret place. I know what the
> segment is but not the value. So a line could look like
>
> *anything*ST*850*anything*......
>
> I solved it as:
>
> my $pos = index($str, "ST");


Unrelated to Perl, but why would there be an element before an ST segment?
Is your segment terminator something other than a newline?

--
Clay Irving <(E-Mail Removed)>
Human history becomes more and more a race between education and
catastrophe.
- Herbert George Wells
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      08-03-2003
Tony <(E-Mail Removed)> wrote:

> So a line could look like
>
> *anything*ST*850*anything*......



Can *anything* include the "ST" character sequence?

Or did you mean to say *anything_NOT_containing_ST* instead?


> I solved it as:
>
> my $pos = index($str, "ST");



What should that return when $str = 'The ST1300 rocks! ST 850 some more' ?

If "anything" really does mean _any_ thing, then you have
not solved your problem yet...




[snip TOFU. Please learn the proper way to format a followup.]

--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
bd
Guest
Posts: n/a
 
      08-03-2003
On Sat, 02 Aug 2003 16:45:49 -0700, Tony wrote:

> Hello All,
> I am very new to Perl and I am trying to pull a substring out of a
> larger string as such:
>
> xyxyxyxyxyxyZZxyxyxyxyxyx
>
> I know if I get the index of ZZ in the string can use substr($string,
> $index, $len) where len is the number of charcters I want.
>
> I am looking through the Programming Perl book from O'Reilly and I can not
> find any easy way to search through the string and get this value.
>
> I have done this several time in java like such and I am trying to the
> same thing in Perl:
>
> String str = "xyxyxyxyxyxyZZxyxyxyxyxyx"; int len1 = str.indexOf("ZZ");
> String str2 = str.substring(len1, (len1 + "ZZ".length()));
>
> I am sure between something I get here and what I am reading I will figure
> this out. Thanks in advance for any help


What do you want to pull out? In any case, a regex is what you want.
E.g., to get everything after ZZ:
$foo =~ s/^.*ZZ//;

See perldoc perlre for more info
--
Freenet distribution not available
You shall judge of a man by his foes as well as by his friends.
-- Joseph Conrad

 
Reply With Quote
 
bd
Guest
Posts: n/a
 
      08-03-2003
On Sun, 03 Aug 2003 00:46:35 -0700, Tony wrote:


[Reply moved to bottom, where it belongs]

> (E-Mail Removed) (Tony) wrote in message
> news:<(E-Mail Removed). com>...
>> Hello All,
>> I am very new to Perl and I am trying to pull a substring out of a
>> larger string as such:
>>
>> xyxyxyxyxyxyZZxyxyxyxyxyx
>>
>> I know if I get the index of ZZ in the string can use substr($string,
>> $index, $len) where len is the number of charcters I want.
>>
>> I am looking through the Programming Perl book from O'Reilly and I can
>> not find any easy way to search through the string and get this value.
>>
>> I have done this several time in java like such and I am trying to the
>> same thing in Perl:
>>
>> String str = "xyxyxyxyxyxyZZxyxyxyxyxyx"; int len1 = str.indexOf("ZZ");
>> String str2 = str.substring(len1, (len1 + "ZZ".length()));
>>
>> I am sure between something I get here and what I am reading I will
>> figure this out. Thanks in advance for any help
>>
>> -Tony

> Everyone,
> Thanks for the response. I found the documentation for Index()
> about 30 minutes after I posted. Thanks to everyone who responded I
> apprciate it.
>
> Gunnar to answer your question what I am trying to do is pull a particular
> segent out of an EDI document so i can tell what doc it is so I can route
> it via Perl to the corret place. I know what the segment is but not the
> value. So a line could look like
>
> *anything*ST*850*anything*......
>
> I solved it as:
>
> my $pos = index($str, "ST");


Why not:
$str =~ /(ST[0-9]+)/;
my $stnum = $1;
--
Freenet distribution not available
If I'm over the hill, why is it I don't recall ever being on top?
-- Jerry Muscha

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
replacing substrings within strings amadain Python 11 02-14-2007 02:31 PM
Matching substrings within a Regex newjazzharmony@hotmail.com Perl Misc 12 05-16-2006 08:04 PM
Getting substrings in C marcwentink@hotmail.com C Programming 1 12-23-2005 10:03 AM
Replacing palindrome substrings of an input string with a given string Tung Chau C Programming 1 08-06-2004 07:27 PM
Replacing palindrome substrings of an input string with a given string. Any effecient algorithm? Tung Chau C Programming 0 08-06-2004 10:18 AM



Advertisments