Getting substrings within a String

T

Tony

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
 
G

Gunnar Hjalmarsson

Tony said:
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??
 
T

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");


-Tony
 
G

Gunnar Hjalmarsson

Tony said:
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. :)
 
C

Clay Irving

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?
 
T

Tad McClellan

Tony said:
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.]
 
B

bd

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
 
B

bd

On Sun, 03 Aug 2003 00:46:35 -0700, Tony wrote:


[Reply moved to bottom, where it belongs]
(e-mail address removed) (Tony) wrote in message

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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top