Visit Our UNIX and Linux User Community


hash map Illegal division by zero problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting hash map Illegal division by zero problem
# 1  
Old 03-29-2010
hash map Illegal division by zero problem

Hi Everyone,

a.txt
Code:
line1;a;33
line1;c;22
line1;b;0
line1;a;55

a.pl
Code:
#!/usr/bin/perl
use strict;
use warnings;

                my @sorted=();
                my @tmp;
                my $FA;
                my @F;
                my %q;
                my %qq;

                open($FA, "/tmp/a.txt") or die "$!";
                while(<$FA>) {
                        chomp;
                        @tmp=split(/\;/, $_);
                        push @sorted, "$_";
                }
                close $FA;


                foreach (@sorted) {
                        @F = split(/\;/, $_);
                        $q{"$F[1]"} += 1;
                        if ($F[2] > 0) {
                                $qq{"$F[1]"} += 1;
                        } else {
                                $qq{"$F[1]"} += 0;
                        }
                }

print map "$_\t$q{$_}\t$qq{$_}\t".sprintf ("%.2f",60/$qq{$_})."\n", sort { $q{$b} <=> $q{$a} } keys %q;

Code:
[root@]# /tmp/a.pl
Illegal division by zero at ./a.pl line 33.

would like to have the output of
Code:
a       2       2       30.00
c       1       1       60.00
b       1       0       0.00

Smilie

Thanks

---------- Post updated at 12:38 PM ---------- Previous update was at 11:54 AM ----------

from Picking Up Perl - Associative Arrays (Hashes)
i can use each method to for loop its value.

Problem solved, but seek for more good way of doing that.

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

                my @sorted=();
                my @tmp;
                my $FA;
                my @F;
                my %q;
                my %qq;
                my %qqq;

                open($FA, "/tmp/a.txt") or die "$!";
                while(<$FA>) {
                        chomp;
                        @tmp=split(/\;/, $_);
                        push @sorted, "$_";
                }
                close $FA;


                foreach (@sorted) {
                        @F = split(/\;/, $_);
                        $q{"$F[1]"} += 1;
                        if ($F[2] > 0) {
                                $qq{"$F[1]"} += 1;
                        } else {
                                $qq{"$F[1]"} += 0;
                        }
                }

foreach my $key (keys %qq) {
        if ($qq{$key} == 0) {
                $qqq{$key} = "0.00";
        } else {
                $qqq{$key} = 60/$qq{$key};
        }
}

print map "$_\t$q{$_}\t$qq{$_}\t".sprintf ("%.2f",$qqq{$_})."\n", sort { $q{$b} <=> $q{$a} } keys %q;


Last edited by jimmy_y; 03-29-2010 at 03:53 PM..

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Is "stat: illegal option -- -" an issue with hyphenated filename or flag problem?

Hi. I'm trying to install VMWare Workstation to run a virtual machine on my Mac OS, but running the bundle from bash(xterm) sh VMware-workstation-Full-11.0.0-2305329.x86_64.bundle (as suggested in install guide) comes up with error:stat: illegal option -- - usage: stat Digging... (5 Replies)
Discussion started by: defeated
5 Replies

2. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

3. Shell Programming and Scripting

How to solve this problem "Illegal Option"

I am just trying to read the password from command prompt that user should type with in 10 seconds, I am getting "read: Illegal option -t" #!/bin/bash ... (1 Reply)
Discussion started by: KarthikPS
1 Replies

4. UNIX for Dummies Questions & Answers

Problem to map VIM cursor moving in InsertMode

Hi all What I want? I want in Insert mode, press Alt-hjkl move cursor, and then back to insert mode. I know ctrl-o, hjkl can do the job. but everytime I want to move, i have to press ctrl-o, or I have to count how many hjkl I will do, do a C-O (n)hjkl. What I tried (example only with 'j')... (2 Replies)
Discussion started by: sk1418
2 Replies

5. Shell Programming and Scripting

Division problem -Awk

input one two three four 0 0 0 10424 0 102 0 15091 1 298 34 11111 0 10 0 1287 scripts awk 'NR>1{print ($1/$2) / ($3/$4)}' awk 'NR>1{ if ($1 ||$3 ||$2|| $4 == 0) print 0; else print (($1/$2)/($3/$4))}' error awk: division by zero input record number 1, file rm source line... (9 Replies)
Discussion started by: quincyjones
9 Replies

6. Shell Programming and Scripting

Making a hash map from a records file

Greets again. Let me first say that this forum and all of you participants are amazing. You give of your time and knowledge to those of us who aspire and have not reached your level (yet). My thanks. I have created a file of records and wish to make a hash map from it. Here is an example of what... (6 Replies)
Discussion started by: rdettwyler
6 Replies

7. Shell Programming and Scripting

Perl hash definition problem

I am trying to generate a report based on an input file with the following layout: COMPANY1--SITE1--PROGRAM1--PRODUCT1--PRICE1 COMPANY1--SITE1--PROGRAM2--PRODUCT1--PRICE1 COMPANY1--SITE1--PROGRAM1--PRODUCT2--PRICE3 COMPANY2--SITE1--PROGRAM1--PRODUCT1--PRICE2 etc.... I am facing some... (2 Replies)
Discussion started by: jgfcoimbra
2 Replies

8. Shell Programming and Scripting

PERL, push to hash of array problem

$key = "a"; $value = "hello"; %myhash = {} ; push @{ myHash{$key} }, $hello; print $myHash{$key}."\n"; this script prints "hello" but has following error message. Reference found where even-sized list expected at ./test line 5. can any one help me to fix this problem?? (3 Replies)
Discussion started by: bonosungho
3 Replies

9. UNIX for Dummies Questions & Answers

Problem in division

hi I am having two variables namely a=7 & b=8. I have to subtract these two variables. I am using the command c=`expr $a / $b` When I check the value of c, it comes out to be zero. Please help. Regards Rochit (9 Replies)
Discussion started by: rochitsharma
9 Replies

10. Shell Programming and Scripting

division problem

how can i show the value when i divide a number where the dividend is greater then the divisor. for example... 3 divided by 15 ---> let x=3/15 when i do this in the shell environment it gives me an output of 0. please help me. thanks. (3 Replies)
Discussion started by: inquirer
3 Replies

Featured Tech Videos