Problems with Airthmetic Operators in a hash

Discussion in 'Perl Misc' started by coolchick, Nov 1, 2007.

  1. coolchick

    coolchick Guest

    Hi All,

    I am trying to go through a file and grab all the arithmetic operators
    using a perl hash.
    It is not working for me. What am I doing wrong? I think my issue is
    with the key value that I can't escape. HELP!

    #!/usr/bin/perl

    %operators = ('+',0,'-',0,'=',0,'*',0,'/',0);

    $FILE="operator.txt";
    open(FILE) or die("Could not open $FILE.");

    foreach $line (<FILE>) {

    while (($key,$value) = each(%operators)){
    if ($line =~ /\$key/) {
    $operators{$key}=$value+1;
    }
    }
    }
    coolchick, Nov 1, 2007
    #1
    1. Advertising

  2. coolchick

    Narthring Guest

    On Nov 1, 10:25 am, coolchick <> wrote:
    > Hi All,
    >
    > I am trying to go through a file and grab all the arithmetic operators
    > using a perl hash.
    > It is not working for me. What am I doing wrong? I think my issue is
    > with the key value that I can't escape. HELP!
    >
    > #!/usr/bin/perl
    >
    > %operators = ('+',0,'-',0,'=',0,'*',0,'/',0);
    >
    > $FILE="operator.txt";
    > open(FILE) or die("Could not open $FILE.");
    >
    > foreach $line (<FILE>) {
    >
    > while (($key,$value) = each(%operators)){
    > if ($line =~ /\$key/) {
    > $operators{$key}=$value+1;
    > }
    > }
    >
    > }



    Change /\$key/ to /\Q$key\E/

    \Q quotes pattern metacharacters until \E
    Narthring, Nov 1, 2007
    #2
    1. Advertising

  3. coolchick

    Dr.Ruud Guest

    coolchick schreef:

    > I am trying to go through a file and grab all the arithmetic operators
    > using a perl hash.
    > It is not working for me. What am I doing wrong? I think my issue is
    > with the key value that I can't escape. HELP!
    >
    > #!/usr/bin/perl
    >
    > %operators = ('+',0,'-',0,'=',0,'*',0,'/',0);
    >
    > $FILE="operator.txt";
    > open(FILE) or die("Could not open $FILE.");
    >
    > foreach $line (<FILE>) {
    >
    > while (($key,$value) = each(%operators)){
    > if ($line =~ /\$key/) {
    > $operators{$key}=$value+1;
    > }
    > }
    > }


    perldoc -f quotemeta
    perldoc -f index
    perldoc -f keys

    You are aware that you are only counting maximally one of each operator
    per line?

    You don't need the each(), becausee you can do $operators{$key}++.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Nov 1, 2007
    #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. -Steve-
    Replies:
    2
    Views:
    369
    - Steve -
    Jul 28, 2003
  2. Zero
    Replies:
    20
    Views:
    693
    Dave Thompson
    Sep 8, 2003
  3. rp
    Replies:
    1
    Views:
    512
    red floyd
    Nov 10, 2011
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    608
    David A. Black
    Jul 2, 2008
  5. coolchick
    Replies:
    5
    Views:
    139
    coolchick
    Nov 5, 2007
Loading...

Share This Page