special characters in a hash variable

Discussion in 'Perl Misc' started by Michael Rasmussen, Jul 31, 2011.

  1. On Sun, 31 Jul 2011 17:26:33 -0700
    "ela" <> wrote:

    >
    > Any suggestions?
    >

    Use single quotes when assigning. Use keys as string constants or
    single quotes:

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

    my @names = ('','Hello! World?','Who loves $?');

    my (%hash,$key);

    foreach (@names) {
    $hash{$_}++;
    }

    foreach $key(keys(%hash)) {
    print "$key: $hash{$key}\n";
    }




    --
    Hilsen/Regards
    Michael Rasmussen
    http://pgp.mit.edu:11371/pks/lookup?op=get&search=0xE3E80917
    A computer is like air conditioning: it becomes useless when you open
    windows.
     
    Michael Rasmussen, Jul 31, 2011
    #1
    1. Advertising

  2. Michael Rasmussen

    Willem Guest

    ela wrote:
    ) This works when the exact names are known in advance. How about retrieving
    ) lines from a file, e.g.
    )
    ) while (<FilePointer>) {
    ) $variable = $_;
    ) $hash{$variable}++;
    ) }

    Have you tried it ?


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Jul 31, 2011
    #2
    1. Advertising

  3. Michael Rasmussen

    Willem Guest

    ela wrote:
    ) Thanks for correcting this mistake. The pseudocodes should be:
    )
    ) while ($line = <FilePointer>) {
    ) chomp $line;
    ) $variable = $line;
    ) $hash{"$variable"}{freq}++;
    ) if (<some criteria fulfilled>) {
    ) $hash{"$variable"}{type} = "A";
    ) } else {
    ) $hash{"$variable"}{type} = "B";
    ) }
    ) }

    What does it do ? What do you want it to do ? Where does it go wrong ?


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Jul 31, 2011
    #3
  4. Michael Rasmussen

    Willem Guest

    ela wrote:
    ) "Willem" <> wrote in message
    ) news:...
    )> ela wrote:
    )> ) Thanks for correcting this mistake. The pseudocodes should be:
    )> )
    )> ) while ($line = <FilePointer>) {
    )> ) chomp $line;
    )> ) $variable = $line;
    )> ) $hash{"$variable"}{freq}++;
    )> ) if (<some criteria fulfilled>) {
    )> ) $hash{"$variable"}{type} = "A";
    )> ) } else {
    )> ) $hash{"$variable"}{type} = "B";
    )> ) }
    )> ) }
    )>
    )> What does it do ? What do you want it to do ? Where does it go wrong ?
    )
    ) I make use of hash variable to take statistics of the frequency of different
    ) names stored in a file. However, I find some of the names contain special
    ) characters, for
    ) example,
    )
    ) Normal
    ) I have 30% {abc} shares.
    )
    ) Hello! World?
    ) Who loves $?
    )
    ) So except that "Normal", the others have abnormal counts, e.g.
    ) I expect there should be a key , but then I obtain two keys,
    ) one is
    ) ,
    ) and the other is
    ) abc@yahoo

    The code you posted above doesn't do that.
    The mistake must be in some code you didn't post.
    Please post a complete program that can be run and shows the problem.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Jul 31, 2011
    #4
  5. "ela" <> wrote:
    >Thanks for correcting this mistake. The pseudocodes should be:
    >
    >while ($line = <FilePointer>) {
    > chomp $line;
    > $variable = $line;
    > $hash{"$variable"}{freq}++;
    > if (<some criteria fulfilled>) {
    > $hash{"$variable"}{type} = "A";
    > } else {
    > $hash{"$variable"}{type} = "B";


    Why are you stringifying the hash key? Please see "perldoc -q quoting"
    "What's wrong with always quoting "$vars"?"

    jue
     
    Jürgen Exner, Jul 31, 2011
    #5
  6. "ela" <> wrote:
    >"Willem" <> wrote in message
    >news:...
    >> ela wrote:
    >> The code you posted above doesn't do that.
    >> The mistake must be in some code you didn't post.
    >> Please post a complete program that can be run and shows the problem.

    >
    >Marvellous! You pointed out the problem correctly.
    >Now I have to use special characters as a separator in the hash name
    >instead, e.g.


    What is a "hash name"? Do you mean the name of the hash, i.e. the
    variable name like 'foobar' in '%foobar'? Or are you talking about the
    keys in the hash? Either way, neither of them has a seperator.

    Furthermore your code doesn't even pass the perl syntax checker.

    Not to mention that you should always, yes always, add the obligatory
    "use strict; use warnings;" !!! If you had done so then perl would have
    given your a very crucial hint.

    >$cell1="It's a good day!";
    >$cell2="What?";
    >
    >hash{"$cell1\%$cells2"}{freq}++;


    Global symbol "$cells2" requires explicit package name at [...]
    syntax error at [...], near "}{"

    >to result in:
    >
    >hash{It's a good day!%What?}{freq}++;


    Global symbol "%What" requires explicit package name at [...]
    syntax error at [...], near "?}"

    Now think about why the compiler may be giving you this specific error
    (about the missing symbol, not the syntax error). Why would the compiler
    try to interpret %What as a variable name? Think about it!

    >foreach key (sort (keys(%hash))) {
    > @tmp = split /\%/, $key
    > print "$tmp[0]\t$tmp[1]\n";
    >}
    >
    >Well, it failed so what's the problem then?


    "It failed" is the worst possible error description, along with "it
    doesn't work".
    I am sure you posted the lasted and most complete code sample you got.
    And because you didn't say I must guess that you are stuck right there
    with whatever perl is telling you about this piece of code.

    Well, the error messages are pretty clear:
    syntax error at t.pl line 4, near "}{"
    syntax error at t.pl line 6, near "?}"
    Missing $ on loop variable at t.pl line 8.
    In each case you are missing the '$' sign (the sigil) in front of the
    variable name '$hash' resp. '$key'.

    Furthermore you have to enclose the key
    It's a good day!%What?
    in quotes(*) because it is not a single bareword.

    And there is the statement separator (i.e. the ';') missing at the end
    of
    @tmp = split /\%/, $key

    HTH

    jue
     
    Jürgen Exner, Jul 31, 2011
    #6
  7. "ela" <> writes:

    [...]

    > while ($line = <FilePointer>) {
    > chomp $line;
    > $variable = $line;
    > $hash{"$variable"}{freq}++;
    > if (<some criteria fulfilled>) {
    > $hash{"$variable"}{type} = "A";
    > } else {
    > $hash{"$variable"}{type} = "B";
    > }
    > }


    There is no point in interpolating these variables into strings before
    using them as hash keys. Whatever their value happens to be, it will
    be stringified if it doesn't already have a string value in order to
    turn it into something usable as hash key.
     
    Rainer Weikusat, Jul 31, 2011
    #7
  8. Michael Rasmussen

    Willem Guest

    ela wrote:
    )
    ) "J?rgen Exner" <> wrote in message
    ) news:...
    )
    )> Why are you stringifying the hash key? Please see "perldoc -q quoting"
    )> "What's wrong with always quoting "$vars"?"
    )>
    )> jue
    )
    ) Thanks a lot. Removing the quotes solves the problem.

    That's very unusual, because the quoted vars were not causing any
    actual problems in this case. Hash keys get stringified anyways.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Jul 31, 2011
    #8
  9. Michael Rasmussen

    ela Guest

    I make use of hash variable to take statistics of the frequency of different
    names. However, I find the names can contain special characters, for
    example,

    I have 30% {abc} shares.

    Hello! World?
    Who loves $?

    that makes my variable, for example,

    $hash{"$name"}++;

    goes crazy.

    Any suggestions?
     
    ela, Aug 1, 2011
    #9
  10. Michael Rasmussen

    ela Guest

    "Michael Rasmussen" <> wrote in message
    news:...

    > Use single quotes when assigning. Use keys as string constants or
    > single quotes:
    >
    > my @names = ('','Hello! World?','Who loves $?');


    This works when the exact names are known in advance. How about retrieving
    lines from a file, e.g.

    while (<FilePointer>) {
    $variable = $_;
    $hash{$variable}++;
    }
     
    ela, Aug 1, 2011
    #10
  11. Michael Rasmussen

    ela Guest

    "Willem" <> wrote in message
    news:...
    > ela wrote:
    > ) This works when the exact names are known in advance. How about
    > retrieving
    > ) lines from a file, e.g.
    > )
    > ) while (<FilePointer>) {
    > ) $variable = $_;
    > ) $hash{$variable}++;
    > ) }
    >
    > Have you tried it ?


    Thanks for correcting this mistake. The pseudocodes should be:

    while ($line = <FilePointer>) {
    chomp $line;
    $variable = $line;
    $hash{"$variable"}{freq}++;
    if (<some criteria fulfilled>) {
    $hash{"$variable"}{type} = "A";
    } else {
    $hash{"$variable"}{type} = "B";
    }
    }
     
    ela, Aug 1, 2011
    #11
  12. Michael Rasmussen

    ela Guest

    "Willem" <> wrote in message
    news:...
    > ela wrote:
    > ) Thanks for correcting this mistake. The pseudocodes should be:
    > )
    > ) while ($line = <FilePointer>) {
    > ) chomp $line;
    > ) $variable = $line;
    > ) $hash{"$variable"}{freq}++;
    > ) if (<some criteria fulfilled>) {
    > ) $hash{"$variable"}{type} = "A";
    > ) } else {
    > ) $hash{"$variable"}{type} = "B";
    > ) }
    > ) }
    >
    > What does it do ? What do you want it to do ? Where does it go wrong ?


    I make use of hash variable to take statistics of the frequency of different
    names stored in a file. However, I find some of the names contain special
    characters, for
    example,

    Normal
    I have 30% {abc} shares.

    Hello! World?
    Who loves $?

    So except that "Normal", the others have abnormal counts, e.g.
    I expect there should be a key , but then I obtain two keys,
    one is
    ,
    and the other is
    abc@yahoo
     
    ela, Aug 1, 2011
    #12
  13. Michael Rasmussen

    ela Guest

    "Willem" <> wrote in message
    news:...
    > ela wrote:
    > The code you posted above doesn't do that.
    > The mistake must be in some code you didn't post.
    > Please post a complete program that can be run and shows the problem.


    Marvellous! You pointed out the problem correctly.
    Now I have to use special characters as a separator in the hash name
    instead, e.g.

    $cell1="It's a good day!";
    $cell2="What?";

    hash{"$cell1\%$cells2"}{freq}++;

    to result in:

    hash{It's a good day!%What?}{freq}++;

    foreach key (sort (keys(%hash))) {
    @tmp = split /\%/, $key
    print "$tmp[0]\t$tmp[1]\n";
    }

    Well, it failed so what's the problem then?
     
    ela, Aug 1, 2011
    #13
  14. Michael Rasmussen

    ela Guest

    "Jürgen Exner" <> wrote in message
    news:...

    > Why are you stringifying the hash key? Please see "perldoc -q quoting"
    > "What's wrong with always quoting "$vars"?"
    >
    > jue


    Thanks a lot. Removing the quotes solves the problem.
     
    ela, Aug 1, 2011
    #14
  15. Michael Rasmussen

    ela Guest

    "Jürgen Exner" <> wrote in message
    news:...
    > "ela" <> wrote:
    >>"Willem" <> wrote in message
    >>news:...
    >>> ela wrote:
    >>> The code you posted above doesn't do that.
    >>> The mistake must be in some code you didn't post.
    >>> Please post a complete program that can be run and shows the problem.

    >>
    >>Marvellous! You pointed out the problem correctly.
    >>Now I have to use special characters as a separator in the hash name
    >>instead, e.g.

    >
    > What is a "hash name"? Do you mean the name of the hash, i.e. the
    > variable name like 'foobar' in '%foobar'? Or are you talking about the
    > keys in the hash? Either way, neither of them has a seperator.
    >
    > Furthermore your code doesn't even pass the perl syntax checker.
    >
    > Not to mention that you should always, yes always, add the obligatory
    > "use strict; use warnings;" !!! If you had done so then perl would have
    > given your a very crucial hint.
    >
    >>$cell1="It's a good day!";
    >>$cell2="What?";
    >>
    >>hash{"$cell1\%$cells2"}{freq}++;

    >
    > Global symbol "$cells2" requires explicit package name at [...]
    > syntax error at [...], near "}{"
    >
    >>to result in:
    >>
    >>hash{It's a good day!%What?}{freq}++;

    >
    > Global symbol "%What" requires explicit package name at [...]
    > syntax error at [...], near "?}"
    >
    > Now think about why the compiler may be giving you this specific error
    > (about the missing symbol, not the syntax error). Why would the compiler
    > try to interpret %What as a variable name? Think about it!
    >
    >>foreach key (sort (keys(%hash))) {
    >> @tmp = split /\%/, $key
    >> print "$tmp[0]\t$tmp[1]\n";
    >>}
    >>
    >>Well, it failed so what's the problem then?

    >
    > "It failed" is the worst possible error description, along with "it
    > doesn't work".
    > I am sure you posted the lasted and most complete code sample you got.
    > And because you didn't say I must guess that you are stuck right there
    > with whatever perl is telling you about this piece of code.
    >
    > Well, the error messages are pretty clear:
    > syntax error at t.pl line 4, near "}{"
    > syntax error at t.pl line 6, near "?}"
    > Missing $ on loop variable at t.pl line 8.
    > In each case you are missing the '$' sign (the sigil) in front of the
    > variable name '$hash' resp. '$key'.
    >
    > Furthermore you have to enclose the key
    > It's a good day!%What?
    > in quotes(*) because it is not a single bareword.
    >
    > And there is the statement separator (i.e. the ';') missing at the end
    > of
    > @tmp = split /\%/, $key
    >
    > HTH
    >
    > jue


    Sorry for making this misunderstanding. In the real codes, I do add use
    strict and use warnings. Here I haven't copied all the codes directly but
    post pseudocodes. I will learn this lesson again and to prevent lousy
    pseudocodes.
     
    ela, Aug 1, 2011
    #15
    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. Stefan Mueller
    Replies:
    3
    Views:
    33,033
    Stefan Mueller
    Jul 23, 2006
  2. Replies:
    2
    Views:
    1,093
    Ingo Menger
    May 31, 2007
  3. rvino
    Replies:
    0
    Views:
    4,656
    rvino
    Aug 14, 2007
  4. rp
    Replies:
    1
    Views:
    530
    red floyd
    Nov 10, 2011
  5. majna
    Replies:
    4
    Views:
    673
    Thomas 'PointedEars' Lahn
    Sep 19, 2007
Loading...

Share This Page