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

I've been tring to figure out how to split apart a string and put it back together using "=~". For instance, how do I take apart the string "/Text_Files/Cake_Rec.txt" to "Cake Rec"? I was thinking of using something like
$Title= "/Text_Files/Cake_Rec.txt"; $Title =~/(\/\_\.)/;
But I don't think that will work. Can any of you help me?

Replies are listed 'Best First'.
Re: Making a title
by rchiav (Deacon) on Nov 12, 2001 at 03:42 UTC
    You're going to want to use split. Your regex is only trying to match.. and it's trying to match something that looks like "/_.". Look at the following..
    # get into the habit of using these. # they will save you a lot of headaches. use strict; use warnings; my $title = "/Text_Files/Cake_Rec.txt"; my @tokens = split /[\/_\.]/, $title; foreach (@tokens) { print "$_\n"; }
    split uses a regex to split apart a string. Within that regex we have a character class. The character class basically means it will use any one of the things in there to split on.

    You should probably also read perlre. There's a lot of great info on how regex's work.

    Hope this helps
    Rich

Re: Making a title
by astaines (Curate) on Nov 12, 2001 at 03:46 UTC

    If what you're splitting is files, try File::Basename

    Otherwise try splitting it up into words, using a regex. The superlative Perl cookbook suggests

    /\b([A-Za-z]+)\b/

    -- Anthony Staines
Re: Making a title
by CharlesClarkson (Curate) on Nov 12, 2001 at 05:02 UTC

    Well, if you flat out refuse to use 'split' or 'basename' as mentioned in the replies above, you could use this.

    my $title= '/Text_Files/Cake_Rec.txt'; ($title) = ($title =~ m|([^/]*)\.|); $title =~ tr/_/ /; print $title;



    HTH,
    Charles K. Clarkson