Trouble with $key to HASH when Numeric

Discussion in 'Perl' started by CowBoyCraig, Mar 4, 2005.

  1. CowBoyCraig

    CowBoyCraig Guest

    It seems if I "Change" the $key going to if ""(exists($GREEN{$key}))""
    at all It hoses. The keys look like "19973|3.1.A.4" without the quotes.


    If I print like this "print GREEN{$key}\n"; to see what the Hash is
    getting it looks like this:
    }REEN{12483|3.1.B.2

    That is odd? Is there a way to send my data to the hash without it
    getting hosed???

    Code below:


    foreach (keys %HOT) {

    # 6AE-23945: 4.1.B.6 # Fix this
    # Make a Key for %GREEN from %HOT that works
    my $i=$_;
    $i=~/(\d{5}?)$/g;
    $i=$1;

    my $key="$i|$HOT{$_}";
    #print "$GREEN{$key}\n";

    if (exists($GREEN{$key})) {
    # Remove from %GREEN
    # remove $KEY and its value from %HASH
    print "Removed $GREEN{$key}\n";
    delete($GREEN{$key});
    } else { # print "did not see\n";
    }
    }


    Craig
     
    CowBoyCraig, Mar 4, 2005
    #1
    1. Advertising

  2. CowBoyCraig

    Joe Smith Guest

    CowBoyCraig wrote:
    > It seems if I "Change" the $key going to if ""(exists($GREEN{$key}))""
    > at all It hoses. The keys look like "19973|3.1.A.4" without the quotes.


    Based on your problem, I can see that they are not what you think.

    > If I print like this "print GREEN{$key}\n"; to see what the Hash is
    > getting it looks like this:
    > }REEN{12483|3.1.B.2


    That is to be expected when you print a value like "12483|3.1.B.2\r\n".

    You did not use chomp() or other means to get rid of the
    unwanted carriage return at the end of the line.

    Next time, post to comp.lang.perl.misc instead of comp.lang.perl .

    -Joe
     
    Joe Smith, Mar 4, 2005
    #2
    1. Advertising

  3. CowBoyCraig

    Jim Gibson Guest

    In article <>,
    CowBoyCraig <> wrote:

    > It seems if I "Change" the $key going to if ""(exists($GREEN{$key}))""
    > at all It hoses. The keys look like "19973|3.1.A.4" without the quotes.


    Can you be more specific? "hoses" is not a good description of an error
    condition.

    >
    >
    > If I print like this "print GREEN{$key}\n"; to see what the Hash is
    > getting it looks like this:
    > }REEN{12483|3.1.B.2


    Looks like your key has a carriage return ('\r') at the end.

    >
    > That is odd? Is there a way to send my data to the hash without it
    > getting hosed???


    One does not "send data" to a hash. Can you be more specific about what
    you are trying to do?

    >
    > Code below:
    >

    use strict;
    use warnings;

    >
    > foreach (keys %HOT) {
    >
    > # 6AE-23945: 4.1.B.6 # Fix this
    > # Make a Key for %GREEN from %HOT that works
    > my $i=$_;
    > $i=~/(\d{5}?)$/g;


    What do you think this statement is doing? It has a couple of oddities:
    1) the pattern /(\d{5}?)$/ will match zero or one group of 5 digits at
    the end of a string -- in other words, this pattern will always match
    and capture either 5 digits or nothing; 2) the pattern is anchored to
    the end of the string, yet you are asking for all matches with the g
    modifier (there can only be one end of string unless you specify the m
    modifier, which you do not).

    > $i=$1;


    You may have 5 digits in $1, or you may have nothing. You should check
    to see which it is (or fix your regex).

    >
    > my $key="$i|$HOT{$_}";
    > #print "$GREEN{$key}\n";
    >
    > if (exists($GREEN{$key})) {
    > # Remove from %GREEN
    > # remove $KEY and its value from %HASH
    > print "Removed $GREEN{$key}\n";
    > delete($GREEN{$key});
    > } else { # print "did not see\n";
    > }
    > }


    Please post a complete, working program and tell us what you think it
    is doing that it is not. But post it to comp.lang.perl.misc, because
    this newsgroup is defunct.


    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= East/West-Coast Server Farms - Total Privacy via Encryption =---
     
    Jim Gibson, Mar 4, 2005
    #3
    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. Replies:
    5
    Views:
    946
    X-Centric
    Jun 30, 2005
  2. darrel
    Replies:
    4
    Views:
    827
    darrel
    Jul 19, 2007
  3. jobs

    int to numeric numeric(18,2) ?

    jobs, Jul 21, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    970
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Jul 22, 2007
  4. rp
    Replies:
    1
    Views:
    539
    red floyd
    Nov 10, 2011
  5. Une bévue
    Replies:
    5
    Views:
    153
    Une bévue
    Aug 10, 2006
Loading...

Share This Page