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

Trying to split a string on a user defined object scalar. This way depending on the delinter for the file you can parse the flat file into columns

#read in first line which should contain fieldnames $field_line = <FILE>; chomp $field_line; @field_list = split( /$self->{delimiter}/, $field_line);

I get the same affect as calling split with // as the first argument. Any ideas?

Replies are listed 'Best First'.
Re: Splitting on Variable
by tye (Sage) on May 21, 2003 at 22:35 UTC

    I prefer to use \Q$var\E in regular expressions when I want to treat the variable's contents as a string (not interpret it is a regular expression). So I'd write:

    @field_list = split /\Q$self->{delimiter}\E/, $field_line, -1;
    but you could also use quotemeta or structure it in several different ways.

    I'll take a guess that your delimiter is "|".

                    - tye
Re: Splitting on Variable
by halley (Prior) on May 21, 2003 at 20:40 UTC
    If situation /$blahblah/ acts just like //, then $blahblah is probably undefined or otherwise empty.
    print STDERR "My delimiter is [$self->{delimiter}].\n";

    --
    [ e d @ h a l l e y . c c ]

Re: Splitting on Variable
by graff (Chancellor) on May 22, 2003 at 05:51 UTC
    If the user-specified delimiter is expected to always be a single character, then you might want to refer to this excellent node by tilly, his "Text:xSV" module, which does this sort of thing very nicely.

    (On the other hand, if delimiters could include things like "::" or "\s+//\s+", etc, and you're confident that you don't need to worry about embedded/escaped occurrences of the delimiter pattern within data fields, then just make sure that $self->{delimiter} is defined and non-empty, and proceed with your own solution...)