Modifying and printing a string variable

J

JC

I have a hash, with key bodybgcolor pointing to a hexadecimal color
string "#FF9966". Problem is, I'm not allowed to pring the "#" sign, so
I need to print "FEDCBA". How do I do that?
 
J

Josef Moellers

JC said:
I have a hash, with key bodybgcolor pointing to a hexadecimal color
string "#FF9966". Problem is, I'm not allowed to pring the "#" sign, so
I need to print "FEDCBA". How do I do that?

What have you tried and where did it fail to meet your expectations?

As it is a veeeery simple problem with numerous possible solutions (a
truly TMTOWTDI):

my $string = '#FF9966';

my $result1 = substr($string, 1);
my $result2; ($string2 = $string) =~ s/^#//;
my $result3; ($string3 = $string) =~ tr/^#//d;

Pick your choice.

However, as your original string is "#FF9966" and your resulting string
has absolutely no connection with the original string (apart from some
length and character set considerations):

$result = 'FEDCBA';

Josef
 
K

kens

Josef said:
What have you tried and where did it fail to meet your expectations?

As it is a veeeery simple problem with numerous possible solutions (a
truly TMTOWTDI):

my $string = '#FF9966';

my $result1 = substr($string, 1);
my $result2; ($string2 = $string) =~ s/^#//;
my $result3; ($string3 = $string) =~ tr/^#//d;

I assume you meant:

my $result2; ($result2 = $string) =~ s/^#//;
my $result3; ($result3 = $string) =~ tr/^#//d;
 
T

Tad McClellan

JC said:
I have a hash, with key bodybgcolor pointing to a hexadecimal color
string "#FF9966". Problem is, I'm not allowed to pring the "#" sign,


print substr $hash{bodybgcolor}, 1;

so
I need to print "FEDCBA".


Why do you need to print "FEDCBA" when the value is "#FF9966"?

I would have thought you need to print "FF9966" for that value...
 
D

Dave Weaver

Josef Moellers said:
What have you tried and where did it fail to meet your expectations?

my $result2; ($string2 = $string) =~ s/^#//;
my $result3; ($string3 = $string) =~ tr/^#//d;

Pick your choice.

But don't pick the last one! :)

That tr isn't doing what you think it's doing.

#!/usr/bin/perl
use strict;
use warnings;

my $x="#a^b#c^d";
$x =~ tr/^#//d;
print "$x\n";
__END__

Output:
abcd

Granted, in the specific case of the OP it will do the job required,
but in the same way that a gun will cure a cold.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top