Problems with Arithmetic Operators in a Perl 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

    J. Gleixner Guest

    coolchick wrote:
    > Hi All,


    Yeah, we heard you the first time you posted this same question
    15-minutes ago.

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


    use strict;
    use warnings;

    >
    > %operators = ('+',0,'-',0,'=',0,'*',0,'/',0);
    >
    > $FILE="operator.txt";
    > open(FILE) or die("Could not open $FILE.");


    Why not have it die with why it failed?

    die "Could not open $FILE: $!";

    You should be using the three argument open.

    open( my $file, '<', $FILE ) or die "Could not open $FILE: $!";

    perldoc -f open

    >
    > foreach $line (<FILE>) {
    >
    > while (($key,$value) = each(%operators)){
    > if ($line =~ /\$key/) {

    if ( $line =~ /\Q$key\E/ ) {
    > $operators{$key}=$value+1;
    > }
    > }
    > }
    >


    To learn more about regular expressions, see:

    perldoc perlretut
     
    J. Gleixner, Nov 1, 2007
    #2
    1. Advertising

  3. coolchick <> writes:

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


    > while (($key,$value) = each(%operators)){
    > if ($line =~ /\$key/) {


    I'm not quite sure of the exact rules, but in regular expressions a $
    can be both an sigil for a scalar variable or the special character
    matching the end of the string. In the above I would guess that perl
    will match the literal string '$key'.

    What you need is to interpolate the variable $key but with any special
    characters escaped. This is done by using the \Q escape:

    if ($line =~ /\Q$key/) {

    > $operators{$key}=$value+1;
    > }
    > }


    //Makholm
     
    Peter Makholm, Nov 1, 2007
    #3
  4. coolchick wrote:
    >
    > 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


    use warnings;
    use strict;


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


    my %operators = map { $_ => 0 } qw( + - = * / );


    > $FILE="operator.txt";


    my $FILE = 'operator.txt';


    > open(FILE) or die("Could not open $FILE.");


    open FILE, '<', $FILE or die "Could not open '$FILE' $!";


    > foreach $line (<FILE>) {


    while ( my $line = <FILE> ) {


    > while (($key,$value) = each(%operators)){
    > if ($line =~ /\$key/) {
    > $operators{$key}=$value+1;
    > }


    for my $key ( keys %operators ) {
    $operators{ $key }++ if $line =~ /\Q$key/;
    }


    > }
    > }




    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Nov 1, 2007
    #4
  5. coolchick

    Dave Weaver Guest

    On Thu, 01 Nov 2007 08:40:32 -0700, 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


    You should use the warnings & strict pragmas to allow perl to help you out.

    use warnings;
    use strict;

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


    Now you have enabled strictures, you need to declare your variables using 'my'.

    my %operators;

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


    Use the 3-argument form of open, and include "$!" in the error message so
    you know *why* the open failed.

    open my $f, '<', $FILE or die "Couldn't open '$FILE' : $!";

    > foreach $line (<FILE>) {


    When iterating over the lines of a file, use "while" instead of "for" - "for"
    will read all lines into memory at once; "while" will read them in one at a
    time.

    while ( my $line = <$f> )

    > while (($key,$value) = each(%operators)){
    > if ($line =~ /\$key/) {
    > $operators{$key}=$value+1;
    > }


    Looping over the keys of %operators for each line of the file isn't
    terribly efficient. It might be better to construct a pattern that
    matches any of the operators you're looking for:

    if ( $line =~ m{([-+=*/])} ) {
    ++ $operators{ $1 };
    }

    Note that this will only find the first operator on the line.
    To find all operators:

    while ( $line =~ m{([-+=*/])}g ) {
    ++ $operators{ $1 };
    }

    So, putting it all together:

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

    my %operators;

    my $FILE = 'operator.txt';

    open my $f, '<', $FILE or die "Can't open '$FILE' : $!";

    while ( my $line = <$f> ) {

    while ( $line =~ m{([-+=*/])}g ) {
    ++ $operators{ $1 };
    }
    }
    close $f;

    use Data::Dumper;
    print Dumper \%operators;
     
    Dave Weaver, Nov 5, 2007
    #5
  6. coolchick

    coolchick Guest

    Thanks a bunch for all your help.
     
    coolchick, Nov 5, 2007
    #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. =?ISO-8859-1?Q?Christian_Brechb=FChler?=

    Enhancing valarray with "normal" arithmetic operators

    =?ISO-8859-1?Q?Christian_Brechb=FChler?=, Sep 12, 2003, in forum: C++
    Replies:
    6
    Views:
    991
    =?ISO-8859-1?Q?Christian_Brechb=FChler?=
    Sep 14, 2003
  2. Lionel B
    Replies:
    5
    Views:
    467
    Gianni Mariani
    Mar 3, 2005
  3. Trent Buck

    variadic arithmetic, boolean operators

    Trent Buck, Jan 1, 2005, in forum: C Programming
    Replies:
    3
    Views:
    429
    Mysidia
    Jan 2, 2005
  4. joshc
    Replies:
    5
    Views:
    592
    Keith Thompson
    Mar 31, 2005
  5. rp
    Replies:
    1
    Views:
    595
    red floyd
    Nov 10, 2011
Loading...

Share This Page