Newbie question: "Get substring of line"

  • Thread starter Petterson Mikael
  • Start date
P

Petterson Mikael

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
 
U

Uri Guttman

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
 
P

Petterson Mikael

Uri said:
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
 
S

Sherm Pendley

Petterson said:
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--
 
A

Anno Siegel

Petterson Mikael said:
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
 
P

Petterson Mikael

Sherm said:
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
**************
 
T

Tad McClellan

Petterson Mikael said:
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);
 
M

Michele Dondi

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
 
J

Jürgen Exner

Leendert said:
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

jl_post

Petterson said:
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
 
U

Uri Guttman

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
 
L

Leendert Bottelberghs

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
 
S

Sherm Pendley

Uri said:
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--
 
U

Uri Guttman

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
 
C

Charlton Wilbur

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
 
M

Michele Dondi

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
 
S

Sherm Pendley

Uri said:
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--
 
U

Uri Guttman

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
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top