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

I working on a script that involves comparing strings in the form of "1-2-3" (ss7 point codes). However whenever I store them in a hash, but then later retrieve them the "-" dashes are converted to "." dots or decimal places. Which in turn causes my string compare to fail. Is there something I'm overlooking, i.e. "use module" that will allow number strings to have "-" hyphens in them? Thanks
  • Comment on Why are my hyphens in number strings stored as dots?

Replies are listed 'Best First'.
Re: Why are my hyphens in number strings stored as dots?
by GrandFather (Saint) on Feb 14, 2012 at 02:29 UTC

    How about you show us sample code that performs that trick?

    True laziness is hard work
Re: Why are my hyphens in number strings stored as dots?
by Marshall (Canon) on Feb 14, 2012 at 02:51 UTC
    You can use pretty much any string as a hash key or a hash value. If you mean a string, and it contains a "-", you need to put quotes around it otherwise Perl will try to do math on it.
    #!/usr/bin/perl -w use strict; use Data::Dumper; my %hash; $hash{1-2-3} = 23; print Dumper \%hash; %hash=(); $hash{"1-2-3"} = 23; print Dumper \%hash; #$hash{HOMETOWN-HOUSTON} = 1; #Bareword "HOMETOWN" not allowed while "strict subs" in use .. #Bareword "HOUSTON" not allowed while "strict subs" in use ... $hash{"HOMETOWN-HOUSTON"} = 1; #this is ok __END__ $VAR1 = { '-4' => 23 }; $VAR1 = { '1-2-3' => 23 };
    show a very short example of your problem.
Re: Why are my hyphens in number strings stored as dots?
by oko1 (Deacon) on Feb 14, 2012 at 03:07 UTC

    I'll agree with GrandFather, and add one: how about you show us the data, too - maybe by running it through 'hexdump -C' or some equivalent?

    #!/usr/bin/perl -w use strict; my $s1 = "1\xe2\x80\x932\xe2\x80\x933"; my $s2 = "1-2-3"; print "$s1: not $s2, but a brilliant imitation!\n" unless $s1 eq $s2;

    Output:

    1𣇽: not 1-2-3, but a brilliant imitation!

    Depending on your character set, those n-dashes could well end up looking like dots, etc.

    -- 
    I hate storms, but calms undermine my spirits.
     -- Bernard Moitessier, "The Long Way"