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

Hi everyone I am trying to use split like this , i have a variable $a = "index.html" how can i use split to remove the .extension and just have $a = "index" , is there a way with out using an array .. please do let me know thanks alot

Replies are listed 'Best First'.
Re: a small help in split
by broquaint (Abbot) on May 06, 2003 at 13:45 UTC
    Array slice a split or a regex capture perhaps
    $_ = 'index.html'; print( (split /\./)[0], $/, /^([a-z]+)\./, $/); __output__ index index
    See. split, perlre and perlop for more info on the behaviour above.
    HTH

    _________
    broquaint

    update: changed to grabbing before the dot (i.e not extension)

Re: a small help in split
by helgi (Hermit) on May 06, 2003 at 14:31 UTC
    Well, the canonical way is to use the standard module:
    use warnings; use strict; use File::Basename; my @extensions = qw/.html .htm .HTML .HTM/; my $a = 'index.html'; my $basename = basename $a,@extensions; print $basename;

    --
    Regards,
    Helgi Briem
    helgi AT decode DOT is

Re: a small help in split
by hmerrill (Friar) on May 06, 2003 at 13:54 UTC
    Easiest and simplest to understand way to me is to use split like this:
    my $a = "index.html"; my ($filename, $extension) = split /\./, $a; print "filename = $filename\n"; print "extension = $extension\n";
    You can accept a list of values on the left by surrounding them with parenthesis, like 'my ($filename, $extension) = ...'.

    HTH.
      BTW, you can see for yourself how split works by reading the excellent documentation included with Perl called 'perldocs'. Do this at a command prompt:
      perldoc -f split
      and more generally, read about perldocs by doing
      perldoc perldoc and perldoc perl
      HTH.
      THANKS A BUNCH FOR THE HELP , I AM HAVING A SLOW BRAIN DAY TODAY , THANK YOU AGAIN
Re: a small help in split
by dragonchild (Archbishop) on May 06, 2003 at 13:44 UTC
    Sounds like homework to me. A few options would include:
    • Using split like a list
    • Using a regex. (I recommend this one.)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.