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

Hi there, could someone please help me understand how to chop off a leading space from my line parsing. I've been researching it and have not found a satisfactory answer yet. My output is correct apart from a leading space. I can process the data again against "s/ //g" but felt that I should understand what I'm missing in my understanding of the code line and how to further manipulate the output. Thanks

my @cnfcputypes = (split /,/, ((split /\:/, ((grep /types/, @emscpu)[0]))[1]));

Replies are listed 'Best First'.
Re: Manipulation of output of multiple parse line.
by kennethk (Abbot) on Jun 12, 2013 at 14:23 UTC
    To use regular expressions to chop leading spaces, use the ^ anchor to specify 'start at the beginning'. Also, rather than the g modifier (since you really only need to substitute once), use + to indicate 'one or more'. I prefer the character class \s to an explicit space (' '), but YMMV.
    s/^\s+//
    YAPE::Regex::Explain describes this as:
    The regular expression: (?-imsx:^\s+) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- \s+ whitespace (\n, \r, \t, \f, and " ") (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
    See Metacharacters and Quantifiers in perlre.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Manipulation of output of multiple parse line.
by space_monk (Chaplain) on Jun 12, 2013 at 15:43 UTC

    This article seems to most clearly explain various methods of removing whitespace from the start and end of strings.

    If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)
Re: Manipulation of output of multiple parse line.
by JockoHelios (Scribe) on Jun 12, 2013 at 21:08 UTC
    Is the space actually in the array rows, or is it just being added when you print the array ?

    I ran into this recently, and found out that the spaces weren't actually in the array.

    If you're printing the array with something like:

    print "@TheArray";

    and you don't have an extra space in front of the first line, but you do have an extra space in front of all other lines, the spaces probably aren't in the array.

    Perl by default seperates the array with spaces in an interpolative context (meaning if you have the array inside double quotes). That's why the first row doesn't have a space in front of it - the first row doesn't need to be seperated from another row in front of it, because it's the first row.

    If you print with something like this, the extra spaces won't show up:

    print @TheArray;

    Without the "", Perl doesn't interpolate the array, meaning it doesn't seperate the rows with spaces. The following script line also works, but is more verbose. I tend to write scripts that are more verbose, because I'm not a Perl Master yet :)

    foreach $ArrayRow( @TestArray ) { print $ArrayRow; }

    Here's a demo script:
    #!C:\Perl\bin #use warnings; #use strict; my $ArrayRow = ""; splice( my @TestArray ); $ArrayRow = "20130516,530,730\n"; push( @TestArray, $ArrayRow ); $ArrayRow = "20130516,731,1100\n"; push( @TestArray, $ArrayRow ); $ArrayRow = "20130516,1101,1200\n"; push( @TestArray, $ArrayRow ); print "@TestArray"; print "\n\n"; print @TestArray; print "\n\n"; foreach $ArrayRow( @TestArray ) { print $ArrayRow; } print "\n\n";
    Dyslexics Untie !!!

      Hi and thanks for the replies. My question was rather how I could combine the white space delete in the same code line as I pasted rather than reprocessing the same one more time or catching it when I next consumed the array data. I was interested if there was a way to string the commands along indefinitely. Thanks