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

Say I have a data record like this:
a1:a,b,c
After I split the record at the colon, I then assign a,b,c to an array by split(/,\s*/, $value).
I know that the \s* takes care of spaces after the coma, but how do I take out spaces before the first letter. For example:
a1:  a,b,c
When I try to assign a,b,c to the array, my script doesn't work because of the leading spaces between the colon and 'a'. I've tried:
split(/\s*,\s*, $value)
but that doesn't work. Thanks
Chris

Replies are listed 'Best First'.
Re: Eliminating leading spaces
by frag (Hermit) on Jan 04, 2002 at 06:20 UTC
    How about changing the split on the colon to split on /:\s*/ instead?

    -- Frag.
    --
    "Just remember what ol' Jack Burton does when the earth quakes, the poison arrows fall from the sky, and the pillars of Heaven shake. Yeah, Jack Burton just looks that big old storm right in the eye and says, "Give me your best shot. I can take it."

      Many thanks. Some of the most obvious things are oftened over looked. Worked great! Chris
Re: Eliminating leading spaces
by particle (Vicar) on Jan 04, 2002 at 06:32 UTC
    it looks like you've asked this before, and didn't get a correct answer.

    read Death to Dot Star! this should put you on the right track. i believe there are optional spaces before and after commas, no?

    if you still need a hand, reply here with the code you've tried, and we'll have a look at it. good luck!

    ~Particle

      Thanks. I'll take a look at it. The previous question you're referring to dealt with spaces after the coma, not between the colon and the first character. Sorry I didn't make that clearer. Chris
Re: Eliminating leading spaces
by jonjacobmoon (Pilgrim) on Jan 04, 2002 at 07:10 UTC
    Metadoktor's solution certainly will work, but I would like to offer one of my own.

    #! /usr/bin/perl -Tw my ($label, $string); $string = "a1: a,b,c"; $string =~ s/\s+//g; # $string = a1:a,b,c ($label, $string) = split(/:/,$string); my @tokens = split(/,/,$string); foreach (@tokens) { print "$_\n"; }
    This should print: a b c

    However, you MIGHT also be asking exactly how to eliminate leading spaces. The '^' at the beginning of the regex points to the beginning of a line or string such that:

    $string =~ s/^\s*//;

    will get rid of leading spaces

Re: Eliminating leading spaces
by metadoktor (Hermit) on Jan 04, 2002 at 06:31 UTC
    If I understand what you're trying to do, then try this fragment:

    $_="a1:   a, b, c, e";
    
    my @tokens=split / +/;
    
    for my $i (1..(scalar(@tokens)-1))
    {
         print "\"$tokens[$i]\"\n";
    }
    
    should return:
    "a,"
    "b,"
    "c,"
    "d,"
    "e"
    

    metadoktor

    "The doktor is in."

Re: Eliminating leading spaces
by dmmiller2k (Chaplain) on Jan 05, 2002 at 00:11 UTC

    You may be over-complicating this:

    my $s = "a1: a,b,c"; # instead of just a colon, first split on colon preceded # or followed by optional whitespace my ($prefix, $rest) = split /\s*:\s*/, $s; # now, $prefix = "a1" and $rest = "a,b,c" # now do the same for commas my @rest = split /\s*,\s*/, $rest; # now, @rest = ( 'a', 'b', 'c' )

    Simple, symmetrical. Clean.

    dmm