Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Removing whitespace

by Anonymous Monk
on May 07, 2002 at 12:03 UTC ( #164592=perlquestion: print w/replies, xml ) Need Help??

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

I posted this before and got a lot of help. Sadly, I can't find my original post. So here is the question again. If I read in an environment variable such as: $user = $ENV {'Mike', 'Wallace'}; How can I remove the white space and get just the last name Wallace. Thank you in advance.

Replies are listed 'Best First'.
Re: Removing whitespace
by Molt (Chaplain) on May 07, 2002 at 13:12 UTC

    Okay, I'm about to break the habit of a lifetime and answer this. I think your initial post on this matter was here. If it isn't your post it's still the same basic question so your answer should be there.

    Now I've been useful a couple of hints. If you're going to be asking questions it's worth registering a username so you can easily go back and view your own posts, and also it's definitely worth noting down useful tips in a notebook or something (Yes, a paper one. No CPU needed).

    Now for a quick check.. I think you've managed to mangle your question somewhere, or do you really have an enviroment variable called (not 'with the value of', but called) 'Mike Wallace'?

      actually, the poster specified (my code tags for clarity) $user = $ENV {'Mike', 'Wallace'};, which is not a valid way to recover a single key from %ENV.

      either he's looking for an array of environment vars (not likely), in which case he'd specify @ENV, or he's looking for a key named Mike', 'Wallace (which is valid on Win32,) which is referenced by $ENV{'Mike\', \'Wallace'}. NOTE: this is platform specific--i can't find a way to set an environment variable with quotes on solaris or cygwin.

      either way, Anonymous Monk probably meant what you said.

      ~Particle *accelerates*

        It is a valid way, and is Perl's way of doing 'Multidimensional hashes' without hitting hashes of hashes. Admittedly $; isn't normally space, but such is.

        In Programming Perl 2nd Ed this is all on Page 133, my 3rd Ed. is at home at the moment I'm afraid so I can't give page reference in that.

        The following code actually works.. which is somewhat scary when it's simply not What You Mean, as I don't think it is in this case.

        #!/usr/bin/perl -w use strict; $ENV{'Paul', 'Golds'} = 'Molt'; print $ENV{'Paul', 'Golds'};
Re: Removing whitespace
by BUU (Prior) on May 07, 2002 at 12:46 UTC
    s/\s*//g; will remove all the whice space in your var, as to your specfic one, you could do something like (split /\,/,$user)[1] Which would return the word after the second comma and before the third (if there is one). And have you tried super search?

      It can be done like this -

      $data=" abc " # Remove all spaces $data=~ s/ //g;

      Output - abc

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://164592]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2020-09-23 23:10 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (132 votes). Check out past polls.