in reply to Removing spaces, again.

First I thought "problem with the substr index" - but that doesn't look like it.

Then I thought "problem with the assignment binding tighter than the pattern match operator" - but that doesn't seem to be true, either.

Then I pondered "could it be the list context of the match?" I'm still not sure if that's an issue or not.

So, in the spirit of "give a man a fish..." (or, light him on fire, depending on your persepective), I'll offer the following somewhat generic advice when faced with code like this:

  1. Break the single statement into multiple steps:
    my $bit_o_line = substr ($line, 3, 7); $bit_o_line =~ m/^\s*(.*)\s*$/; $BKSIC_MODEL[$i] = $1;
  2. Use the debugger to see if things are doing what you expect at each step. You can always go back and recombine the lines into one shorter line after you've debugged it, and no one is ever the wiser. :-)

Hope that helps clear it up.

Peace,
-McD

Replies are listed 'Best First'.
Re: Re: Removing spaces, again.
by buckaduck (Chaplain) on Mar 16, 2001 at 03:17 UTC
    my $bit_o_line = substr ($line, 3, 7); $bit_o_line =~ m/^\s*(.*)\s*$/; $BKSIC_MODEL[$i] = $1;
    This is pretty much what he did do, and it doesn't work. It might help to change the regex slightly:
    my $bit_o_line = substr ($line, 3, 7); $bit_o_line =~ m/^\s*(\S+)\s*$/; $BKSIC_MODEL[$i] = $1;
    But it's still cumbersome compared to some of the earlier suggestions. If you must do it this way, why not follow the method in the FAQ:
    my $BKSIC_MODEL[$i] = substr ($line, 3, 7); $BKSIC_MODEL[$i] =~ s/^\s+//; $BKSIC_MODEL[$i] =~ s/\s+$//;

    buckaduck

      buckaduck writes:

      This is pretty much what he did do, and it doesn't work.

      I think it half works. He's getting caught by the greedy *, which explain the trailing space on his match, but I don't see how he got a leading space. Anytime I scratch my head like that on my own code, I fall back to a technique like the one I suggested.

      All your suggestions are great - and as usual, TMTOWTDI - but I was hoping in my response to point akm2 towards ways he could help himself more, now and in the future. Maybe if he came up with something new, he'd post it back here.

      Peace,
      -McD

        I've also been scratching my head over that leading space. The only thing I can think of is that he really wrote something like:
        ($BKSIC_MODEL[$i] = substr ($line, 3, 7)) =~ m/^\s*(.*)\s*$/;
        (which would simply capture the substr result and throw away the result of the pattern match)

        ... instead of the line he posted here:

        ($BKSIC_MODEL[$i]) = substr ($line, 3, 7) =~ m/^\s*(.*)\s*$/;
        (which should not capture the leading space)

        buckaduck