Modifying and printing a string variable

Discussion in 'Perl Misc' started by JC, Sep 12, 2006.

  1. JC

    JC Guest

    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?
    JC, Sep 12, 2006
    #1
    1. Advertising

  2. JC wrote:
    > 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
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Sep 12, 2006
    #2
    1. Advertising

  3. JC

    kens Guest

    Josef Moellers wrote:
    > JC wrote:
    > > 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;
    >


    I assume you meant:

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

    > ...
    >
    > Josef
    > --
    > Josef Möllers (Pinguinpfleger bei FSC)
    > If failure had no penalty success would not be a prize
    > -- T. Pratchett
    kens, Sep 12, 2006
    #3
  4. JC <> wrote:

    > 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...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 12, 2006
    #4
  5. kens wrote:
    > Josef Moellers wrote:
    >
    >>JC wrote:
    >>
    >>>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;
    >>

    >
    >
    > I assume you meant:
    >
    > my $result2; ($result2 = $string) =~ s/^#//;
    > my $result3; ($result3 = $string) =~ tr/^#//d;


    Yesindeedsir ;-)

    Josef
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Sep 12, 2006
    #5
  6. JC

    Dave Weaver Guest

    Josef Moellers <> wrote:
    > JC wrote:
    > > 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?
    >
    > 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.
    Dave Weaver, Sep 13, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vincent ngai
    Replies:
    1
    Views:
    430
    David White
    Nov 24, 2004
  2. JustSomeGuy
    Replies:
    13
    Views:
    508
    msalters
    Dec 9, 2004
  3. Uenal Mutlu

    modifying stack variable

    Uenal Mutlu, Mar 29, 2005, in forum: C++
    Replies:
    6
    Views:
    474
    Malte Starostik
    Mar 29, 2005
  4. Stefan Mueller
    Replies:
    9
    Views:
    129
    Stefan Mueller
    Nov 1, 2005
  5. mikie
    Replies:
    2
    Views:
    76
    Steven D'Aprano
    Mar 24, 2014
Loading...

Share This Page