Getting substrings within a String

Discussion in 'Perl Misc' started by Tony, Aug 3, 2003.

  1. Tony

    Tony Guest

    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
     
    Tony, Aug 3, 2003
    #1
    1. Advertising

  2. 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
     
    Gunnar Hjalmarsson, Aug 3, 2003
    #2
    1. Advertising

  3. Tony

    Tony Guest

    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
    (Tony) wrote in message news:<>...
    > 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
     
    Tony, Aug 3, 2003
    #3
  4. 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
     
    Gunnar Hjalmarsson, Aug 3, 2003
    #4
  5. Tony

    Clay Irving Guest

    In article <>, 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 <>
    Human history becomes more and more a race between education and
    catastrophe.
    - Herbert George Wells
     
    Clay Irving, Aug 3, 2003
    #5
  6. Tony <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Aug 3, 2003
    #6
  7. Tony

    bd Guest

    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
     
    bd, Aug 3, 2003
    #7
  8. Tony

    bd Guest

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


    [Reply moved to bottom, where it belongs]

    > (Tony) wrote in message
    > news:<>...
    >> 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
     
    bd, Aug 3, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. amadain
    Replies:
    11
    Views:
    438
    Paul McGuire
    Feb 14, 2007
  2. Tung Chau
    Replies:
    1
    Views:
    470
    SM Ryan
    Aug 6, 2004
  3. Tung Chau
    Replies:
    0
    Views:
    376
    Tung Chau
    Aug 6, 2004
  4. Getting substrings in C

    , Dec 23, 2005, in forum: C Programming
    Replies:
    1
    Views:
    1,432
    Richard Bos
    Dec 23, 2005
  5. Replies:
    12
    Views:
    198
    Tad McClellan
    May 16, 2006
Loading...

Share This Page