Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: How to remove the words before the dot using perl?

by nysus (Parson)
on Mar 16, 2017 at 04:49 UTC ( [id://1184817]=note: print w/replies, xml ) Need Help??


in reply to How to remove the words before the dot using perl?

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

Replies are listed 'Best First'.
Re^2: How to remove the words before the dot using perl?
by mr_ron (Chaplain) on Mar 16, 2017 at 13:56 UTC

    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
Re^2: How to remove the words before the dot using perl?
by finddata (Sexton) on Mar 16, 2017 at 05:01 UTC
    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:  <%-{-{-{-<

        its working fine

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1184817]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-25 05:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found