Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

say my variable conatians
$var = /aasd/aacscsaa/affr4a/a55gaa/gvgaa/5h aa/fga
Now at the beginning were it says /aasd I wanna just delete /aasd.

Replies are listed 'Best First'.
(jeffa) Re: delete something from a variable
by jeffa (Bishop) on Jul 06, 2002 at 20:47 UTC
    $var =~ s/\/[^\/]+//;
    should do the trick - match a forward slash (\/), then match one or more non-forward slashes ([^\/]+).

    If you don't like having to escape those forward slashes, you can choose another token - i prefer braces because they don't bork my syntax highlighter:

    $var =~ s{/[^/]+}{};

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      actualy I need to delete it at the end of $var
        Ahhh, that's not /aasd then, is it? Seriously, just anchor the match - ^ for the beginning, $ for the end:
        $var =~ s{/[^/]+$}{};

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: delete something from a variable
by abstracts (Hermit) on Jul 06, 2002 at 20:49 UTC
    I assume you mean:
    $var = '/aasd/aacscsaa/affr4a/a55gaa/gvgaa/5h aa/fga';
    In that case, you can do one of:
    $var =~ s#/aasd##; # remove '/aasd' $var =~ s#/[^/]+##; # remove a slash followed by any number of # non-slashes substr($var,0,5,'');# replace the first 5-char substr with '' $var = substr($var, 5, length($var)-5); # get a substring $var =~ m#/[^/]+(.*)# and $var = $2; # match what follows # after the non-slashes # and set it to $var
    Hope this helps,,,

    Update: Forgot to mention that there are probably a 100 other ways to do this. And thanks jeffa for the correction.