Regex - assign to new variable and replace in one line?

B

Brad Walton

This is probably simple for the experienced Perl programmer, but as I
am learning, and hours of searching has led to nothing, this one has
tied me up long enough... So I could use a little assistance.

I am trying to consolidate the following 2 lines, into 1 line.

$missionname = $check[1];
$missionname =~ s/.mis//;

I am trying to find a way to assign $missionname the same value as
$check[1], and remove '.mis' from the variable, in one expression.
This is really an efficiency issue, and also a learning experience for
me.

Thanks for any help,
Brad
 
G

Gunnar Hjalmarsson

Brad said:
This is probably simple for the experienced Perl programmer, but as
I am learning, and hours of searching has led to nothing, this one
has tied me up long enough... So I could use a little assistance.

I am trying to consolidate the following 2 lines, into 1 line.

$missionname = $check[1];
$missionname =~ s/.mis//;

I am trying to find a way to assign $missionname the same value as
$check[1], and remove '.mis' from the variable, in one expression.

($missionname = $check[1]) =~ s/\.mis//;
 
M

Matija Papec

X-Ftn-To: Brad Walton

This is probably simple for the experienced Perl programmer, but as I
am learning, and hours of searching has led to nothing, this one has
tied me up long enough... So I could use a little assistance.

I am trying to consolidate the following 2 lines, into 1 line.

$missionname = $check[1];
$missionname =~ s/.mis//;

s/.mis// for $missionname = $check[1];

this one is most common and recommended:
($missionname = $check[1]) =~ s/.mis//;
 
B

Bob Walton

Brad Walton wrote:

....
$missionname = $check[1];
$missionname =~ s/.mis//;

I am trying to find a way to assign $missionname the same value as
$check[1], and remove '.mis' from the variable, in one expression.
This is really an efficiency issue, and also a learning experience for
me. ....


Brad

You indicate you are trying to remove the string '.mis' from the
variable. To do that, you should escape the . as it is a metacharacter.
If that is what you intend, something like:

($missionname = $check[1]) =~ s/\.mis//;

should work.
 
D

David K. Wall

Matija Papec said:
(e-mail address removed) (Brad Walton) wrote:
I am trying to consolidate the following 2 lines, into 1 line.

$missionname = $check[1];
$missionname =~ s/.mis//;

s/.mis// for $missionname = $check[1];

I hadn't seen that before -- and now that I have, I think I'll try to forget
it. :)
this one is most common and recommended:
($missionname = $check[1]) =~ s/.mis//;
 
B

Brad Walton

($missionname = $check[1]) =~ s/\.mis//;

Thank you everyone for your help. Works great!

Brad
 
M

Matija Papec

X-Ftn-To: Purl Gurl

Purl Gurl said:
$missionname = $check[1];
$missionname =~ s/.mis//;


Below. This method of mine takes advantage of a
recent topic, precedence order.

Purl Gurl
--

#!perl

@Check = ("zero", "Purl Gurl.mis Rocks!");

substr (($missionname = $Check[1]), index ($missionname, ".mis"), 4, "");

Beside strange expression, this code doesn't do what OP wanted.
 
M

Matija Papec

(topic is to make a "one liner" for following)
$missionname = $check[1];
$missionname =~ s/.mis//;
Below. This method of mine takes advantage of a
recent topic, precedence order.
@Check = ("zero", "Purl Gurl.mis Rocks!");
substr (($missionname = $Check[1]), index ($missionname, ".mis"), 4, "");
Beside strange expression, this code doesn't do what OP wanted.

Your statement is untrue.

/.mis/ is regex and Brad didn't wrote s/\.mis//
 
B

Brad Walton

Brad Walton said:
Below. This method of mine takes advantage of a
recent topic, precedence order.
@Check = ("zero", "Purl Gurl.mis Rocks!");
substr (($missionname = $Check[1]), index ($missionname, ".mis"), 4,
"");
Beside strange expression, this code doesn't do what OP wanted.


Your statement is untrue.


I'll give it a test run and let you both know...

This method seems to work fine also. I don't fully understand (most of
it) however, so I will go with:

($missionname = $check[1]) =~ s/\.mis//;

until I can better understand Gurl's method.

Thanks again,
Brad
 

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
474,129
Messages
2,570,770
Members
47,329
Latest member
FidelRauch

Latest Threads

Top