Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

In my perl script one of the variable it is giving an extra dot.

by Anonymous Monk
on May 25, 2022 at 07:05 UTC ( [id://11144177]=perlquestion: print w/replies, xml ) Need Help??

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

How to remove the . in the generated string

my $file = shift;

For the above variable it is giving an output like this "./root/git/common/crt_del/cert/DEFAULT.pem"

I want to remove the "." before the /root for that can i use like this $file =~ s{.}{}g or any other way is there to remove that dot in the string

Replies are listed 'Best First'.
Re: In my perl script one of the variable it is giving an extra dot.
by choroba (Cardinal) on May 25, 2022 at 08:18 UTC
    A dot has a special meaning in regexes, it matches any character except newline. To match a literal dot, you need to backslash it.

    Moreover, you probably don't want to remove a dot from somewhere further inside the string, only the starting one. You can add ^ to the regex, it only matches at the beginning of the string.

    The final g means "global", it will remove all the occurrences of the pattern. As we only want to remove a single occurrence, we don't need it.

    $file =~ s{^\.}{};

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: In my perl script one of the variable it is giving an extra dot.
by Discipulus (Canon) on May 25, 2022 at 08:19 UTC
    Hello Anonymous Monk,

    if you want to use the regex be sure to use an anchor to just remove a dot in the beginning of the string:  /^\./ being the dot a special regex char you need to escape it.

    For a more robust solution use File::Spec file_name_is_absolute and rel2abs

    # something like.. $path = File::Spec->rel2abs( $path ) unless File::Spec->file_name_is_a +bsolute( $path )

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Note that the rel2abs approach will only give the same result if / is the CWD. Otherwise it's a different result:

      #!/usr/bin/env perl use strict; use warnings; use File::Spec; chdir '/tmp'; my $path = './root/git/common/crt_del/cert/DEFAULT.pem'; $path = File::Spec->rel2abs ($path) unless File::Spec->file_name_is_absolute ($path); print "$path\n";

      🦛

Re: In my perl script one of the variable it is giving an extra dot.
by eyepopslikeamosquito (Archbishop) on May 25, 2022 at 10:31 UTC

    You will get better answers if you provide us with more context around your problem. After reading your question, my spidey-sense is going off. Forgive me for making a wild guess, but are you a Perl novice tasked with maintaining a script originally written by an experienced Perl programmer?

    Is the my $file = shift; part of a subroutine? If so, show us the complete subroutine and where it is being called. Do you understand how the unwanted "." got there in the first place? Are you certain it needs to be removed? Instead of removing it in the subroutine, does it make more sense to ensure it isn't passed to the subroutine in the first place?

Re: In my perl script one of the variable it is giving an extra dot.
by soonix (Canon) on May 25, 2022 at 08:55 UTC
    more interesting would be to find out where that dot comes from and have it not being put into your $file from the start. Removing it afterwards is more like a band-aid.
Re: In my perl script one of the variable it is giving an extra dot.
by kcott (Archbishop) on May 25, 2022 at 14:37 UTC

    Firstly, I agree with others that it would be preferable to deal with the problem at the source, rather than fixing it within your code. It's entirely possible that you'll need to repeat exactly the same fix in multiple scripts, modules or subroutines.

    If that's not possible, it may be insufficient to just add a ^ anchor and remove the g option. What about hidden files (e.g. .bashrc) or places where intentionally specifying the current directory is necessary. You may need something closer to

    s{^[.](?=/root(?:/|$))}{}

    Consider this SSCCE:

    #!/usr/bin/env perl use strict; use warnings; use Test::More; my @tests = ( [qw{./root /root}], [qw{./root/ /root/}], [qw{./root/xyz /root/xyz}], [qw{./root_plus ./root_plus}], [qw{./not_root/ ./not_root/}], [qw{./script_not_in_path.pl ./script_not_in_path.pl}], [qw{.hidden .hidden}], ); plan tests => 0+@tests; for my $test (@tests) { my $file = $test->[0]; $file =~ s{^[.](?=/root(?:/|$))}{}; is $file, $test->[1]; }

    Output:

    1..7 ok 1 ok 2 ok 3 ok 4 ok 5 ok 6 ok 7

    That's just an example. Modify the test cases to better reflect your real data.

    — Ken

Re: In my perl script one of the variable it is giving an extra dot.
by ikegami (Patriarch) on May 26, 2022 at 18:18 UTC

    $file =~ s{.}{}g does not do what you say. . means "a character that's not a line feed", so that removes every character that's not a line feed from the string in $file.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11144177]
Approved by marto
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (12)
As of 2024-04-23 14:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found