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

Hello monk, I'm trying to split a filename (Hena.Al-pad.0.0.0 ) I'm using  @file = split(/./, $file_name); to split the filename but when i try to print it out print "Split file name: $file[1]"; nothing show up...is my syntax wrong? i double check and it look right

Replies are listed 'Best First'.
Re: Split FileName
by FunkyMonk (Bishop) on Jul 25, 2007 at 22:32 UTC
    . is special in regular expressions. You need to escape it:

    @file = split(/\./, $file_name);

    Spend some time reading this tutorial and then this for all the gory details.

Re: Split FileName
by swampyankee (Parson) on Jul 26, 2007 at 03:25 UTC

    To elaborate a bit on FunkyMonk's response, the period (.), when used in regular expressions means "any character except1 the newline." The way split is to break a string into fields, throwing out the specified separator2, which is a regular expression. When I try splitting a string with /./, I get an empty array.

    If you want to split a string on periods (.), you've got to escape it, with the backslash (\), so your split should look something like this:

    @file = split(/\./, $file_name);
    (which is exactly what FunkyMonk wrote).

    Regex are fun, and an incredibly powerful part of Perl.


    1: Usually.

    2: Although you can keep it if you want, by surrounding the regex with parentheses.


    emc

    Information about American English usage here and here.

    Any New York City or Connecticut area jobs? I'm currently unemployed.