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

I am pretty sure that this is correct from looking at the Camel book, however it is a critical piece of code and I would appreciate a quick look. I am passing a name into a hash, I then need to take the name (whatever the length) and split it into words, then join those words into one word with no spaces. Here is what I have:
$joined_name = join ("", split " ", $temp_dcr{$dcr_name}{PromotionName +}); $temp_dcr{$dcr_name}{PromotionName} = $joined_name;
Thanks, Bill

Replies are listed 'Best First'.
Re: Quick Syntax check
by Masem (Monsignor) on May 21, 2001 at 18:54 UTC
    It will be much faster and more consise to do :
    $temp_dcr{$dcr_name}{PromotionName} =~ s/ //g;

    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      Not only is it "faster and more concise", but it's also clear WHAT it is you're doing. In the original example, the next person reading your code thinks "OK, he busts apart the string on spaces, and then rejoins it with nothing. Why is he doing that?"

      With the s/// or tr/// solutions, it's obvious: "Oh, he's deleting all the spaces."

      xoxo,
      Andy

      %_=split/;/,".;;n;u;e;ot;t;her;c; ".   #   Andy Lester
      'Perl ;@; a;a;j;m;er;y;t;p;n;d;s;o;'.  #   http://petdance.com
      "hack";print map delete$_{$_},split//,q<   andy@petdance.com   >
      
Re: Quick Syntax check
by arturo (Vicar) on May 21, 2001 at 18:56 UTC

    Why don't you try it with some test data and see what the results are? (looks OK to me, btw ...)

    Notice, however, that the operation you describe is also known as "getting rid of all the spaces." So you could do the much simpler and faster

    $temp_dcr{$dcr_name}{PromotionName} =~ tr/ //d;

    HTH

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: Quick Syntax check
by Sifmole (Chaplain) on May 21, 2001 at 19:03 UTC
    If you actually want a "Quick Syntax Check", you can always just do "perl -c foo.pl"; It will do a once-over on your code for you.
Re: Quick Syntax check
by Sifmole (Chaplain) on May 21, 2001 at 19:05 UTC
    Couldn't he also use a tr/ //d; ?

    Update: This was supposed to be a reply to Masem, but ended up being just a comment. Sorry.

    Update: Then of course while I was writing it, arturo posted the same suggestion. Blah...

        or just \s

        Update:Whoops, caught me tye. I guess I was just too quick to post.

        The 15 year old, freshman programmer,
        Stephen Rawls

Re: Quick Syntax check
by blue_cowdawg (Monsignor) on May 21, 2001 at 19:35 UTC

    A simpler way to do what you want to do is the following:

    $temp_dcr{$dcr_name}}PromotionName} =~ s/\s+//g;
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    Peter L. Berghold --- Peter@Berghold.Net
    "Those who fail to learn from history are condemned to repeat it."