tags:

views:

119

answers:

2

Hello, Please kindly help me in the following,
I have

    $sub = C:\views\sght\gzad\text\hksdk\akldls\hool.java
         = C:\views\sght\bdsk\text\hksdfg\sdjks\same.java
         = C:\views\jdjk\jhah\fjd\afhlad\sitklds\hgls.jsp

I need to replace every "\" with a "."
I need to split the $sub such a way that if $sub contains the "text" then split and one variable contains the later part after text like-

$var1 =text.hksdk.akldls.hool.java
       text.hksdfg.sdjks.same.java

else

$var2= views.jdjk.jhah.fjd.afhlad.sitklds.hgls.jsp
A: 

Your '$sub =' assignment statement is confusing. Is that a scalar of space-separated paths?

Am I correct in thinking that you want to take the paths listed in $sub and put ones with "text" in to one list and ones without in to another?

Assuming all of the above:

my $sub = ""; # initialize this however you will.

my @paths = split(/ /, $sub); # space-separated paths to list

my (@with_text, @without);
foreach my $p (@paths){
    $p =~ s/\\/./g;              # replace \ with .
    if($p =~ /text/){
        push(@with_text, $p);
    } else{
        push(@without, $p);
    }
}

my $var1 = join(' ', @with_text); # a space-separated list of paths containing 'text'
my $var2 = join(' ', @without); # a space separated list of paths not containing 'text'
Sorpigal
A: 

I need to replace every "\" with a "."

You can do this on a variable called $text with: $text =~ s/\\/./g -- see perldoc perlop.

I need to split the $sub such a way that if $sub contains the "text" then split and one variable contains the later part after text

This is a straight-forward application of the split() function (see perldoc -f split):

my $sub = "foobarbaz";
my @split = split /(bar)/, $sub;
# $split[0] will contain "foobar"
# split[1] will contain "baz"
Ether