Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Removing spaces, again.

by akm2 (Scribe)
on Mar 16, 2001 at 01:24 UTC ( [id://64788]=perlquestion: print w/replies, xml ) Need Help??

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

My fellow monks, I sit before my window to the all-mighty monastery (thats means my monitor) in a state of confussion. I seek to be enlightened by those of greater wisdom.

I am trying to use a regular expression to convert the string (without the quotation marks, of course) below.

"AGC LCD-S                                                   8  12X12               WHITE"

I run the following expression on the string:

($BKSIC_MODEL[$i]) = substr ($line, 3, 7) =~ m/^\s*(.*)\s*$/;

You would expect $BKSIC_MODEL[$i]'s content to equal "LCD-S" (without the quotation marks, of course) right?

Well the result I get is: " LCD-S " (without the quotation marks, of course).

Please help me slove this strange problem.

Replies are listed 'Best First'.
Re: Removing spaces, again.
by larryl (Monk) on Mar 16, 2001 at 01:40 UTC

    Your pattern is doing a greedy match, so (.*) matches everything from "LCD" to the end, including trailing whitespace. The regex is still happy because the \s*$ at the end is satisfied by matching nothing.

    Try m/^\s*(\S*)/ instead. No need to add the optional whitespace match at the end of the pattern as long as you know you only want to match up to (but not including) the first (optional) whitespace.

Re: Removing spaces, again.
by buckaduck (Chaplain) on Mar 16, 2001 at 02:37 UTC
    Why fix this line when you should be doing it a different way?

    If all you want to do is grab the second field LCD-S you should try this: $BKSIC_MODEL[$i] = (split(' ', $line))[1]; If you're actually trying to grab all of the fields and use them to populate the array @BKSIC_MODEL then try this: @BKSIC_MODEL = split(' ', $line); see perlfunc:split

    buckaduck

Re: Removing spaces, again.
by dfog (Scribe) on Mar 16, 2001 at 01:33 UTC
    It looks like you are just matching, rather than substituting. Try
    ($BKSIC_MODEL[$i] = substr ($line, 3, 7)) =~ s/^\s*(\S*)\s*$/$1/;

    Dave

    UPDATE
    My mistake. Forgot my parens around $BKSIC_MODEL[$i] = substr ($line, 3, 7). Fixed now.
        I think s/// return the number of modifications issued. Using s/// whitout g modificator, result to return true/false but... saying 0/1 is more sharp ( scuse me for this effrontery master ;)

        _______________________
        Please, don't hurt me !
Re: Removing spaces, again.
by McD (Chaplain) on Mar 16, 2001 at 02:43 UTC
    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

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://64788]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-19 19:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found