Newbie question: "Get substring of line"

Discussion in 'Perl Misc' started by Petterson Mikael, Jan 4, 2005.

  1. Hi,

    I am quite new to perl ( but have written numerous shell scripts). I had
    a shell script that used awk to get part of a string on a line.

    Lines looks like this:

    xdt=a2a
    zdt=b2b
    .....

    Is there a simple way in perl to assign a $str the substring after the
    equal sign?

    //Mikael
    Petterson Mikael, Jan 4, 2005
    #1
    1. Advertising

  2. Petterson Mikael

    Uri Guttman Guest

    >>>>> "PM" == Petterson Mikael <> writes:

    PM> xdt=a2a
    PM> zdt=b2b
    PM> ....

    PM> Is there a simple way in perl to assign a $str the substring after the
    PM> equal sign?

    no, perl is useless to handle complex string parsing like that. it can
    only be done by java or lisp so learn those instead.

    have you read ANY documentation or a basic perl book yet? this is beyond
    easy and can be done too many ways to count.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Jan 4, 2005
    #2
    1. Advertising

  3. Uri Guttman wrote:
    >>>>>>"PM" == Petterson Mikael <> writes:

    >
    >
    > PM> xdt=a2a
    > PM> zdt=b2b
    > PM> ....
    >
    > PM> Is there a simple way in perl to assign a $str the substring after the
    > PM> equal sign?
    >
    > no, perl is useless to handle complex string parsing like that. it can
    > only be done by java or lisp so learn those instead.
    >
    > have you read ANY documentation or a basic perl book yet? this is beyond
    > easy and can be done too many ways to count.
    >
    > uri
    >

    Hi,

    I was looking for some help and not rude answers. I know my java but I
    am writing perl ;-)

    Do you have an example of how to do this?

    //Mikael
    Petterson Mikael, Jan 4, 2005
    #3
  4. Petterson Mikael wrote:

    > Hi,
    >
    > I am quite new to perl ( but have written numerous shell scripts). I had
    > a shell script that used awk to get part of a string on a line.
    >
    > Lines looks like this:
    >
    > xdt=a2a
    > zdt=b2b
    > ....
    >
    > Is there a simple way in perl to assign a $str the substring after the
    > equal sign?


    Have you seen the posting guidelines that appear here frequently?

    Perl comes with *lots* of documentation. Start with "man perl" and "man
    perlfaq". <http://learn.perl.org> has lots of links to online tutorials
    and dead trees to help you get started too.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Jan 4, 2005
    #4
  5. Petterson Mikael

    Anno Siegel Guest

    Petterson Mikael <> wrote in comp.lang.perl.misc:
    > Uri Guttman wrote:
    > >>>>>>"PM" == Petterson Mikael <> writes:

    > >
    > >
    > > PM> xdt=a2a
    > > PM> zdt=b2b
    > > PM> ....
    > >
    > > PM> Is there a simple way in perl to assign a $str the substring after the
    > > PM> equal sign?
    > >
    > > no, perl is useless to handle complex string parsing like that. it can
    > > only be done by java or lisp so learn those instead.
    > >
    > > have you read ANY documentation or a basic perl book yet? this is beyond
    > > easy and can be done too many ways to count.
    > >
    > > uri
    > >

    > Hi,
    >
    > I was looking for some help and not rude answers.


    You don't always get what you're asking for.

    > I know my java but I
    > am writing perl ;-)


    No, you're not. You are asking other people to write Perl for you.

    > Do you have an example of how to do this?


    Show an attempt of your own, then we can talk.

    Anno
    Anno Siegel, Jan 4, 2005
    #5
  6. Sherm Pendley wrote:
    > Petterson Mikael wrote:
    >
    >> Hi,
    >>
    >> I am quite new to perl ( but have written numerous shell scripts). I
    >> had a shell script that used awk to get part of a string on a line.
    >>
    >> Lines looks like this:
    >>
    >> xdt=a2a
    >> zdt=b2b
    >> ....
    >>
    >> Is there a simple way in perl to assign a $str the substring after the
    >> equal sign?

    >
    >
    > Have you seen the posting guidelines that appear here frequently?
    >
    > Perl comes with *lots* of documentation. Start with "man perl" and "man
    > perlfaq". <http://learn.perl.org> has lots of links to online tutorials
    > and dead trees to help you get started too.
    >
    > sherm--
    >


    Hi,

    Thanks for the pointers.

    Here is the code I am trying to get working ( see below).

    If the cs_data_file has a line that begins with # it should be treated
    as comment and the next line should be read. However the if-statement is
    reached anyway. What am I doing wrong here?

    //Mikael

    CODE
    **************
    Petterson Mikael, Jan 4, 2005
    #6
  7. On Tue, 04 Jan 2005 09:09:43 +0100, Petterson Mikael wrote:
    > xdt=a2a
    > zdt=b2b
    >
    > Is there a simple way in perl to assign a $str the substring after the
    > equal sign?


    It can be done in several ways. The most efficient way is using regular
    expressions. Take a look here:
    http://www.troubleshooters.com/codecorn/littperl/perlreg.htm

    -leendert bottelberghs
    Leendert Bottelberghs, Jan 4, 2005
    #7
  8. Petterson Mikael <> wrote:

    > I had
    > a shell script that used awk to get part of a string on a line.



    Run your awk through the "a2p" awk-to-perl translator that is
    installed as part of the perl distribution, and see it in Perl.


    > Lines looks like this:
    >
    > xdt=a2a
    > zdt=b2b
    > ....
    >
    > Is there a simple way in perl to assign a $str the substring after the
    > equal sign?



    (all untested)

    my(undef, $str) = split /=/, $line;
    or
    my($str) = $line =~ /=(.*)/s;
    or
    my $str = substr($line index($line, '=')+1);
    or
    my $str = substr($line, 4);


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 4, 2005
    #8
  9. On Tue, 04 Jan 2005 09:09:43 +0100, Petterson Mikael
    <> wrote:

    >Lines looks like this:
    >
    >xdt=a2a
    >zdt=b2b
    >....
    >
    >Is there a simple way in perl to assign a $str the substring after the
    >equal sign?


    (my $str=$_) =~ s/.*=//; # e.g.


    PS: please, do (a favour to yourself and) accept the other advices you
    got notwithstanding the fact that you took them as being rude
    answers...


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Jan 4, 2005
    #9
  10. Leendert Bottelberghs wrote:
    > On Tue, 04 Jan 2005 09:09:43 +0100, Petterson Mikael wrote:
    >> xdt=a2a
    >> zdt=b2b
    >>
    >> Is there a simple way in perl to assign a $str the substring after
    >> the equal sign?

    >
    > It can be done in several ways. The most efficient way is using
    > regular expressions.


    Actually I don't think so.
    While powerful and sometimes convenient to write regular expressions are
    also complex and costly to evaluate at runtime.

    Therefore the humble index() with a substr() is likely to be faster.

    No, I did not benchmark it.

    jue
    Jürgen Exner, Jan 4, 2005
    #10
  11. Petterson Mikael

    Guest

    Petterson Mikael wrote:
    >
    > Lines looks like this:
    >
    > xdt=a2a
    > zdt=b2b
    > ....
    >
    > Is there a simple way in perl to assign a $str the
    > substring after the equal sign?



    Here is one (not necessarily optimal) solution:

    my $string = "xdt=a2a";
    my $substring;
    $substring = $' if $string =~ m/=/;

    Basically, the $' variable holds the part AFTER a successful match,
    so it will be set to everything after the first equal sign of $string.

    Some people don't like using the $' variable because it comes at a
    performance cost. If this performance cost boethers you, you can
    change that last line to:

    $substring = $1 if $string =~ m/=(.*)$/;

    which basically puts the part captured by the first parentheses in $1
    (which is given to $substring).

    If you don't want to do this with regular expressions, I recommend
    you read up on the index() and substr() functions, which you can find
    by looking them up in the Camel book or by typing:

    perldoc -f index
    perldoc -f substr

    I hope this helps.

    -- Jean-Luc
    , Jan 4, 2005
    #11
  12. Petterson Mikael

    Uri Guttman Guest

    >>>>> "jpc" == jl post@hotmail com <> writes:


    jpc> my $string = "xdt=a2a";
    jpc> my $substring;
    jpc> $substring = $' if $string =~ m/=/;

    jpc> Basically, the $' variable holds the part AFTER a successful match,
    jpc> so it will be set to everything after the first equal sign of $string.

    jpc> Some people don't like using the $' variable because it comes at a
    jpc> performance cost. If this performance cost boethers you, you can
    jpc> change that last line to:

    and it is not commonly used as well. so why did you even bother to
    mention it?

    nor did you tell the OP where to learn about regexes or whatever.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Jan 4, 2005
    #12
  13. On Tue, 04 Jan 2005 15:05:43 +0000, Jürgen Exner wrote:
    > Leendert Bottelberghs wrote:
    >> It can be done in several ways. The most efficient way is using
    >> regular expressions.

    >
    > Actually I don't think so.
    > While powerful and sometimes convenient to write regular expressions are
    > also complex and costly to evaluate at runtime.
    >
    > Therefore the humble index() with a substr() is likely to be faster.


    I think you're right. I actually meant it's the most convenient way for a
    lazy programmer.

    -leendert bottelberghs
    Leendert Bottelberghs, Jan 4, 2005
    #13
  14. Uri Guttman wrote:

    > and it is not commonly used as well. so why did you even bother to
    > mention it?


    You heard it here first, folks: TMTOWTDI has been deprecated. The
    Official Perl Motto is now "Uri's Way or the Highway." :)

    Okay, seriously - regex solutions had already been suggested, so where's
    the harm in presenting another alternative? The OP might want to compare
    them to see which is better suited to his situation and/or coding style.

    > nor did you tell the OP where to learn about regexes or whatever.


    Get off your high horse Uri - your own reply wasn't quite the most
    useful comment ever posted here, either.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Jan 4, 2005
    #14
  15. Petterson Mikael

    Uri Guttman Guest

    >>>>> "SP" == Sherm Pendley <> writes:

    >> nor did you tell the OP where to learn about regexes or whatever.


    SP> Get off your high horse Uri - your own reply wasn't quite the most
    SP> useful comment ever posted here, either.

    i didn't want to give an answer to the OP since he obviously didn't even
    lift a finger to try. i help those who help themselves first :)

    notice i like to do code review or answer interesting problems. i tend
    to ignore most newbie questions unless i get peeved by their attitude.

    as for slamming the $' approach, i was annoyed that he fist came up with
    that and then mentioned others didn't like it and then showed a more
    common regex solution. why even show the $' one (regardless of the speed
    issues which are way beyond a newbies scope)? $' is not common because
    of the speed issue and i would think most would prefer explicit grabbing
    as it is usually clearer and you get more control.

    and my horse is only a pony so it isn't that high. :)

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Jan 4, 2005
    #15
  16. >>>>> "SP" == Sherm Pendley <> writes:

    SP> You heard it here first, folks: TMTOWTDI has been deprecated.
    SP> The Official Perl Motto is now "Uri's Way or the Highway." :)

    Well, honestly, the fact that TMTOWTDI does not mean that all WTDI are
    equally good. This ought to be self-evident to anyone with a clue,
    but the TMTOWTDI mantra seems to obscure it for people new to the language.

    Charlton


    --
    cwilbur at chromatico dot net
    cwilbur at mac dot com
    Charlton Wilbur, Jan 4, 2005
    #16
  17. Uri Guttman <> wrote:

    > and my horse is only a pony so it isn't that high. :)



    Keep a careful eye on what type of "grass" it is eating...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 4, 2005
    #17
  18. On Tue, 04 Jan 2005 13:38:38 +0100, Petterson Mikael
    <> wrote:

    >Here is the code I am trying to get working ( see below).


    (no code here!)

    >If the cs_data_file has a line that begins with # it should be treated
    >as comment and the next line should be read. However the if-statement is
    >reached anyway. What am I doing wrong here?


    You failed to paste the code! :)


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Jan 4, 2005
    #18
  19. Uri Guttman wrote:

    > i didn't want to give an answer to the OP since he obviously didn't even
    > lift a finger to try. i help those who help themselves first :)


    I'm entirely with you on that - my own reply was a variation on RTFM too.

    > as for slamming the $' approach, i was annoyed that he fist came up with
    > that and then mentioned others didn't like it and then showed a more
    > common regex solution. why even show the $' one (regardless of the speed
    > issues which are way beyond a newbies scope)?


    Well, obviously I'm neither the poster of the message we're talking
    about nor psychic, so I'm just guessing here. I read it along the lines
    of "other folks have mentioned regexes, in addition here's a less common
    alternative."

    I could just as easily be wrong about that though, so adjust salinity
    accordingly.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Jan 4, 2005
    #19
  20. Petterson Mikael

    Uri Guttman Guest

    >>>>> "SP" == Sherm Pendley <> writes:

    SP> Uri Guttman wrote:

    >> as for slamming the $' approach, i was annoyed that he fist came up with
    >> that and then mentioned others didn't like it and then showed a more
    >> common regex solution. why even show the $' one (regardless of the speed
    >> issues which are way beyond a newbies scope)?


    SP> Well, obviously I'm neither the poster of the message we're talking
    SP> about nor psychic, so I'm just guessing here. I read it along the
    SP> lines of "other folks have mentioned regexes, in addition here's a
    SP> less common alternative."

    i don't mind alternatives but posting one and then saying others
    deprecate that solution is worthless. and below that was a reasonable
    answer. so why even post the poor one and if you do, why put it first?

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Jan 4, 2005
    #20
    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. Jerry C.
    Replies:
    8
    Views:
    229
    Uri Guttman
    Nov 23, 2003
  2. J. Romano
    Replies:
    4
    Views:
    215
  3. Kruno Saho
    Replies:
    0
    Views:
    134
    Kruno Saho
    Apr 7, 2013
  4. Dave Angel
    Replies:
    0
    Views:
    118
    Dave Angel
    Apr 7, 2013
  5. rusi
    Replies:
    0
    Views:
    108
Loading...

Share This Page