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

Hi monks!

I was wondering if anyone could give me some quick help with a split problem I'm having. I actually don't even know if what I'm wanting to do will work but I hope someone can help.

What I'm wanting to do is to split by a variable that is a string that changes with each new read through of a file. I've tried:

@line=split(/$variable/,$_);

but this doesn't seem to be giving me what I'm needing. Is there another way to specify a split based on a variable that is not constant?

Replies are listed 'Best First'.
Re: Split Help
by ikegami (Patriarch) on Mar 16, 2009 at 20:47 UTC
    Could it be that $variable contains text and not a regexp pattern? Fix:
    my $sep_re = quotemeta($sep); my @fields = split(/$sep_re/, $_);
    or
    my @fields = split(/\Q$sep/, $_);

    For example,

    $\ = "\n"; $, = ";"; for my $sep (",", "\t", "|") { my $line = join($sep, qw( a b c )); print $line; my @fields = split(/\Q$sep/, $line); print @fields; print ''; }
    a,b,c a;b;c a b c a;b;c a|b|c a;b;c

    Or maybe you are trying to split on the two characters "\" and "t" instead of an actual tab character?

    $\ = "\n"; $, = ";"; for my $sep ('\t', "\t") { print split(/\Q$sep/, "foo\tbar\\tbaz"); }
    foo bar;baz foo;bar\tbaz
Re: Split Help
by Sandy (Curate) on Mar 16, 2009 at 20:32 UTC
    Hi

    works for me.

    [sandy]perl -e '$x="hello"; $y="thishelloyouhellotoday"; @a=split /$x/ +,$y; print "@a\n";' this you today [sandy]
    could you give us an example of what doesn't work?

      Thanks for the super quick response! What I'm trying to do is to use the split to count the occurrences of the filename with the file. I just checked with a print statement and I am getting a split but for some reason, I cannot get the correct number of splits. I can visually count them but I can't seem to get any goot results from something like:

      print "$#lines\n";

        Hmm.. here you are saying

        print "$#lines\n";

        but in the OP

        @line=split(/$variable/,$_);

        Maybe you mean @lines? or perhaps $#line?

        If you include

        use warnings;

        at the top of your program, perl helps you emitting a warning if, by typo, you are using a variable only once.

        $#lines contains the last index of the array @lines which may or may not be one less than the actual number of elements in the array @lines.   To get the actual number of elements of an array you must use the array in scalar context:

        print scalar @lines, "\n";
Re: Split Help
by bellaire (Hermit) on Mar 16, 2009 at 20:33 UTC
    That should work just fine. We're going to need more context to judge what's wrong. For example, this code works perfectly:
    my $variable = "x"; my @line = split(/$variable/,"aaaxbbbxccc"); print @line; $variable = "y"; @line = split(/$variable/,"aaaybbbyccc"); print @line; $variable = "z"; @line = split(/$variable/,"aaazbbbzccc"); print @line;
    It prints aaabbbccc each time.
Re: Split Help
by Marshall (Canon) on Mar 17, 2009 at 01:03 UTC
    There is some tricky stuff with split.
    #!/usr/bin/perl -w use strict; my $a = "124 abc78980 abc 9872345987abcbca4321"; my $variable = "abc"; my @line=split(/$variable/,$a); print join("\n",@line),"\n"; __END__ prints: 124 78980 9872345987 #interesting bca4321 #also interesting
    It would be helpful if you could provide an input string and what you expect the output to be.