how to distill this par of string from whole string

J

jeanwelly

$mystring = "andgjdkj..dlodjghghdhhdghhd\ndkkdkd\n/usr/local/ddd\n";

I want to distill "/usr/loca/add" from the string and assign to a
variable, thanks!
 
M

Mirco Wahab

jeanwelly said:
$mystring = "andgjdkj..dlodjghghdhhdghhd\ndkkdkd\n/usr/local/ddd\n";

I want to distill "/usr/loca/add" from the string and assign to a
variable, thanks!

if you split the "mystring" on \n, your wanted string
will be at the third place (index = 2):

...
my ($mystring, $distilled);

$mystring = "andgjdkj..dlodjghghdhhdghhd\ndkkdkd\n/usr/local/ddd\n";

$distilled = (split /\n/, $mystring )[2];
...

Regards

M.
 
T

Tad McClellan

jeanwelly said:
$mystring = "andgjdkj..dlodjghghdhhdghhd\ndkkdkd\n/usr/local/ddd\n";
^ ^
^ ^
I want to distill "/usr/loca/add"


What is it about that string that indicates that that is what
should be extracted?

Where did the missing "l" go?

What changed "ddd" to "add"?

from the string and assign to a
variable, thanks!


(my $variable = $mystring) =~ s/^(.*?\/)|\n//sg;
or
my $variable = substr $mystring, rindex($mystring, '/usr/'), -1;
or
my $variable = substr $mystring, 35, 14;
or
my($variable) = $mystring =~ /^(\/.*)/m;
or
my($variable) = $mystring =~ m#(/.*)#;
or
my($variable) = grep /\//, split /\n/, $mystring;
or
my $variable = (split /\n/, $mystring)[2];
 
J

jeanwelly

If I can't know how many "\n" in the string, howthe script can be
written to find all characters before /, and cut them, so I can get /
usr/local/.../?


andgjdkj..dlodjghghdhhdghhd\ndkkdkd\n

jeanwelly said:
$mystring = "andgjdkj..dlodjghghdhhdghhd\ndkkdkd\n/usr/local/ddd\n";

^ ^
^ ^
I want to distill "/usr/loca/add"

What is it about that string that indicates that that is what
should be extracted?

Where did the missing "l" go?

What changed "ddd" to "add"?
from the string and assign to a
variable, thanks!

(my $variable = $mystring) =~ s/^(.*?\/)|\n//sg;
or
my $variable = substr $mystring, rindex($mystring, '/usr/'), -1;
or
my $variable = substr $mystring, 35, 14;
or
my($variable) = $mystring =~ /^(\/.*)/m;
or
my($variable) = $mystring =~ m#(/.*)#;
or
my($variable) = grep /\//, split /\n/, $mystring;
or
my $variable = (split /\n/, $mystring)[2];
 
K

Klaus

$mystring = "andgjdkj..dlodjghghdhhdghhd\ndkkdkd\n/usr/local/ddd\n";

I want to distill "/usr/loca/add" from the string and assign to a
variable

I could guess what you want, but you should first describe the general
rule more precisely, then, after you have described the rule precisely
and completely, you can give an example of what you want to achieve by
concrete data, the data should be consistent and in line with the
general rule.

Even in the absence of any rule, the concrete data you have given in
your post does not seem to be consistent (the letter "l" is missing
after '[...] distill "/usr/loca [...]' and "/ddd" is not the same as "/
add".
$mystring = " [...] /usr/local/ddd [...] ";
[...] I want to distill "/usr/loca/add [...] "

-- Klaus
 
T

Tad McClellan

[ Please learn the correct way of composing a followup message.
Please do this soon.
Please see the Posting Guidelines that are posted here frequently.
]


jeanwelly said:
andgjdkj..dlodjghghdhhdghhd\ndkkdkd\n
What is it about that string that indicates that that is what
should be extracted?
(my $variable = $mystring) =~ s/^(.*?\/)|\n//sg;
or
my $variable = substr $mystring, rindex($mystring, '/usr/'), -1;
or
my $variable = substr $mystring, 35, 14;
or
my($variable) = $mystring =~ /^(\/.*)/m;
or
my($variable) = $mystring =~ m#(/.*)#;
or
my($variable) = grep /\//, split /\n/, $mystring;
or
my $variable = (split /\n/, $mystring)[2];

If I can't know how many "\n" in the string, howthe script can be
written to find all characters before /, and cut them, so I can get /
usr/local/.../?


One of the ways shown above already does that.

Did you try any of them?
 

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

Forum statistics

Threads
474,204
Messages
2,571,063
Members
47,670
Latest member
micheljon

Latest Threads

Top