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

I am having a problem with the value in one of my scalar variables. There seems to be a carriage return in it and additional blank spaces, but I can't seem to remove them. I have tried the following lines:
chomp $name; $name =~ s/^(.*)(\b|\W)/$1/;
but it doesn't remove whatever is after the part I can see.

For example, the value in my scalar is '12345 \t \n ' Now of course I cannot see the tab and new line characters so I am not positive what is after the '12345' I am looking for a way to turn this value into a HEX string so I can see it says something like '30 31 32 33 34 35 20 20 20 20 09 20 20 0D 20 20 20'.

Is there an easy way to do this so I can remove the extra stuff at the end?

Replies are listed 'Best First'.
Re: Converting decimal value to hex
by mscharrer (Hermit) on May 16, 2008 at 17:15 UTC
    For your actual problem:
    Did you tried a simple:
    $name =~ s/\s+$//;

    which should do the trick.

    For your display problem:
    In general you can use pack/unpack for conversions.
    Here I would just use:

    use Data::Dumper; $Data::Dumper::Useqq = 1; print Dumper $name;

    which would print $VAR1 = "12345\t\n"; in your example.

Re: Converting decimal value to hex
by roboticus (Chancellor) on May 16, 2008 at 17:09 UTC
Re: Converting decimal value to hex
by ikegami (Patriarch) on May 16, 2008 at 19:19 UTC
    I recommend the previously stated Data::Dumper solution, since it handles not only strings of bytes, but UNICODE strings too.
    >perl -e"use Data::Dumper; $Data::Dumper::Useqq = 1; print Dumper qq{\ +x{400}};" $VAR1 = "\x{400}";

    If you were dealing with bytes, you could use unpack 'H*'

    >perl -le"$h = uc unpack 'H*', qq{123\t\n}; $h = ~ s/(..)/$1 /g; print + $h" 31 32 33 09 0A
Re: Converting decimal value to hex
by ikegami (Patriarch) on May 16, 2008 at 19:30 UTC

    As for your actual question, the .* matches as much as it can, which is "12345\t", since \W matches "\n".

    The simples solution is probably s/^(\w*)\b/$1/, unless your goal is to remove trailing white spaces. In that case, it would be s/\s+$//.

Re: Converting decimal value to hex
by coldmiser (Hermit) on May 16, 2008 at 17:26 UTC
    Thank you, the $name =~ s/\s+$//; did remove the extra characters, I would have liked to know exactly what they were, but that's due more to curiosity then the problem I had.

    Anyway, Thank you both for your help.

      If you want to access the removed part of the string, try:

      $name =~ s/^(.*?)(\s*)$/$1/;

      It is almost the same that you tried but with two little differences: the first quantifier is not greedy (*? instead of *) so it will not eat up the spaces and the second part of the expression now catches all \s characters until the end of line.

      Rule One: Do not act incautiously when confronting a little bald wrinkly smiling man.

Re: Converting decimal value to hex
by coldmiser (Hermit) on May 16, 2008 at 19:27 UTC
    I think I'm going to have to resort to unpack the $name =~ s/\s+$// works 75% of the time and then the script messes up again. I think unpack will give me exactly what I'm looking for so I can see what characters are causing the problem.