in reply to Re: Regex - counting number of '|'
in thread Regex - counting number of '|'

Hi, I had one more question -

I have to use the split command on the same file to split each input record on its delimiters and and assign it to an array. I am trying to use eval here also (as specified above), but, it is throwing an error. One of the error msg is
"Bareword "per" not allowed while "strict subs" in use at (eval 2) line 1."

#!/usr/bin/perl use Fcntl; use strict; print "starting script..."; my $inp_row = "per|l|mo|nks|rock|s"; my $de = '|'; my @cl_data_row; my $count = (eval "\$inp_row =~ tr/$de//"); die $@ if $@; print "count is $count\n"; chop($inp_row); #@cl_data_row=split(/\|/,$inp_row); @cl_data_row=(eval"\split(/\$de/,$inp_row)"); for (my $x=0;$x<$count;$x++) { print "row element is $cl_data_row[$x]\n"; }
Thanks for your help!!!

Replies are listed 'Best First'.
Re^3: Regex - counting number of '|'
by ww (Archbishop) on Mar 18, 2008 at 00:57 UTC

    $cl_data_row[$x] is unpopulated but no error here under Ubuntu & 5.8.7 with your script:

    ww@GIG:~/pl_test$ perl -c countde.pl countde.pl syntax OK ww@GIG:~/pl_test$ perl countde.pl starting script...count is 5 row element is row element is row element is row element is row element is ww@GIG:~/pl_test$

    Your line 17 isn't doing what you want/expect. @cl_data_row remains undef Compare:

    (Replaces your 17) my $cl_data_row=(\split(/\$de/,$inp_row)); (added) print "\$cl_data_row is $$cl_data_row\n";

    from which the output is:

    ww@GIG:~/pl_test$ perl countde.pl starting script...count is 5 $cl_data_row is per|l|mo|nks|rock|s

    BTW, your chop in line 15 isn't doing what I suspect you think: it's removing the last character, the "s" in "rock|s"

Re^3: Regex - counting number of '|'
by ikegami (Patriarch) on Mar 18, 2008 at 03:10 UTC
    @cl_data_row=(eval"\split(/\$de/,$inp_row)");

    has four errors in it!

    • \s should be just s.
    • Shouldn't be interpolating $inp_row. (Escape the $.)
    • That leaves no reason to use eval.
    • You didn't convert the contents of $de from text to a regexp.

    Fixed:

    @cl_data_row = split(/\Q$de/, $inp_row);

    Why aren't you using Text::CSV anyway?

Re^3: Regex - counting number of '|'
by ikegami (Patriarch) on Mar 18, 2008 at 03:13 UTC

    Oh and the tr/// line

    my $count = (eval "\$inp_row =~ tr/$de//");

    has a problem too. The contents of $de aren't properly converted from text to a tr/// search list. It happens to work for "|", but it won't work for arbitrary seperator characters which seems to be your goal. Fixed:

    my $count = (eval "\$inp_row =~ tr/\Q$de\E//");

    The following is much simpler, though:

    my $count =()= $inp_row =~ /\Q$de\E/g;
Re^3: Regex - counting number of '|'
by poolpi (Hermit) on Mar 18, 2008 at 09:23 UTC
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; # No need to double quoted my $inp_row = q{per|l|mo|nks|ro|ck|s}; my $de = q{|}; my $count = 0; my $row; $count++ for $inp_row =~ /\Q$de/g; print "Delim: $de\nCount: $count\n"; push @{$row}, split /\Q$de/, $inp_row; print Dumper $row;
    Output: Delim: | Count: 6 $VAR1 = [ 'per', 'l', 'mo', 'nks', 'ro', 'ck', 's' ];

    hth,

    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb