Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How to remove the words before the dot using perl?

by finddata (Sexton)
on Mar 16, 2017 at 04:28 UTC ( #1184816=perlquestion: print w/replies, xml ) Need Help??

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

my $suffix = $file; print $suffix,"\n";
output:
rev1.config rev2.config checklist_tmp.config new_block2.config
Expected output:
.config .config .config .config

Replies are listed 'Best First'.
Re: How to remove the words before the dot using perl?
by AnomalousMonk (Bishop) on Mar 16, 2017 at 05:02 UTC

    Or with a substitution (and without a module):

    c:\@Work\Perl\monks>perl -wMstrict -le "my @files = qw(blah.config boo.config big.config); print qq{@files}; ;; s{ \A [^.]* }{}xms for @files; print qq{@files}; " blah.config boo.config big.config .config .config .config


    Give a man a fish:  <%-{-{-{-<

      Is it possible to do the same process without storing it in array.

        Please see this.


        Give a man a fish:  <%-{-{-{-<

Re: How to remove the words before the dot using perl?
by nysus (Vicar) on Mar 16, 2017 at 04:49 UTC

    Without a module:

    use strict; use warnings; my @files = ('blah.config', 'boo.config', 'big.config'); for my $file (@files) { print ((split /\./, $file)[-1] . "\n") ; }

    Explanation: The for loop iterates over the @files array, placing each element in the $file variable. Inside the loop, the $file is passed through a split which divides the string into chunks based on the regular expression inside the slanted lines, in this case a period escaped with a backslash. Without the backslash, period would not be a literal period and could be any character. Read up on "regular expressions" if you aren't familiar. The split function returns an array of all the chunks (in your case, two chunks) for each file name. Since we are interested in the last element of the array (everything following the last period), we can access it with the [-1] there outside of the parentheses. Read up on arrays if you aren't familiar with array syntax. All this gets passed to a print function which we add a new line to with the second period you see there (this is called string concatenation).

    See File::Basename for doing it with a module.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      Parts of the OP suggest that File::Basename may be preferable to custom coding.

      > my $suffix = $file; # suggests a file name > ... > rev1.config # looks like a file name
      The desired result may need some care in reading the module documentation
      perldoc File::Basename
      with extra attention near the helpful line:
      fileparse("/foo/bar/baz.txt", qr/\.[^.]*/);
      Ron
      Here i had stored it in scalar vaiable i need to skip that part of the string using scalar variable.

        c:\@Work\Perl\monks>perl -wMstrict -le "my @files = qw(blah.config boo.config big.config); ;; for (@files) { my $scalar_variable = $_; printf qq{'$scalar_variable' -> }; $scalar_variable =~ s{ \A [^.]* }{}xms; print qq{'$scalar_variable'}; } " 'blah.config' -> '.config' 'boo.config' -> '.config' 'big.config' -> '.config'


        Give a man a fish:  <%-{-{-{-<

        I'm not sure what you are saying but if you are just looking to do this on one scalar variable, get rid of the loop and replace $file with your variable:

        print ((split /\./, $your_variable)[-1] . "\n") ;

        $PM = "Perl Monk's";
        $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
        $nysus = $PM . ' ' . $MCF;
        Click here if you love Perl Monks

Re: How to remove the words before the dot using perl?
by madtoperl (Friar) on Mar 16, 2017 at 10:55 UTC
    Hi finddata
    Using regular expression,
    #!/usr/local/bin/perl use strict; use warnings; #my $file = 'rev1.config'; my $file = 'checklist_tmp.config'; $file =~ s/\w+\././; print "$file\n"; Output: perl test.pl .config

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1184816]
Approved by beech
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2020-01-18 11:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?