Sponsored Content
Full Discussion: Algorthm bug of my code
Top Forums Programming Algorthm bug of my code Post 302562294 by MacMonster on Thursday 6th of October 2011 12:42:38 PM
Old 10-06-2011
Quote:
Originally Posted by yifangt
Thanks Mac!
I seem understanding more of your code now, but not quite sure the exact algorithm. As a learner, I'd like to confirm your logic flow with my comment for the tricky part. If this is too novice to reply, just ignore my post, although I hope to get your correction if I mis-understand it.

Thanks a lot!
Code:
my %combinedfile = ();
my @file_list = ();

foreach my $file (@ARGV)
{
    my $basename = substr($file, rindex($file, '/') + 1);
    my $name = uc(substr($basename, 0, rindex($basename, '.')));

# Get the file to the array @file_list
Code:
 push(@file_list, $name);

    if (open(F, $file))  # Open the first file
    {
        while (my $line = <F>)
        {
            chomp($line);
 
            my @item = split("\t", $line);
            my $id = defined($item[0]) ? $item[0] : '';

# split each row the $id and get the $count
Code:
 my $count = defined($item[1]) ? $item[1] : 0;  $count
            next if ($id eq '');

# If the $id is NOT defined, define it!
Code:
            $combinedfile{$id} = () if (!defined($combinedfile{$id}));

#This is the hash of hash right? using $id as the key for the first layer of hash and $name as key for the second layer. This part is tricky to me.
#But why the second layer of the hash need not be declared at the beginning?

The second layer stores one integer only, not another hash array, so you don't need to initialize it as an empty hash like the first layer.

Code:
$combinedfile{$id}->{$name} = 0 if (!defined($combinedfile{$id}->{$name}));

# Use the hash of hash to get the value of the second layer of hash, right?

Yes.

Code:
$combinedfile{$id}->{$name} = $count;  
        }

        close(F);
    } # Then iterate to the next file 
}print "ID";

foreach my $name (@file_list)
{
    print "\t$name";
}

print "\n";

#sort is not neccessary right?

It is not neccessary, use it or not depends on your output style.

Code:
foreach my $id (sort keys %combinedfile)
{
    print "$id";

    foreach my $name (@file_list)
    {
        my $count = defined($combinedfile{$id}->{$name}) ? $combinedfile{$id}->{$name} : 0;
        print "\t$count";
    }

    print "\n";
}

exit(0);

If my understanding is correct, I have made progress about the data structure with perl. Thanks a lot again!

Yifang
This User Gave Thanks to MacMonster For This Post:
 

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Can anyone find a bug in this code?? shell script

I have done a script and IT WORKS JUST PERFECT from command line...but in cron it has problems executing... nawk -F"|" ' { s=substr($104,2,18)} {b ++s} END { for (i in b) print i, b } ' $1 > /path/to/files/TranId_w$2 q=`cat /path/to/files/TranId_w$2 | wc -l` echo $q >... (1 Reply)
Discussion started by: amon
1 Replies

2. Shell Programming and Scripting

Is it a bug ..?

Hi All, I am using Red Hat Linux on my servers. The problem that I am facing is, sometimes the /opt usage on the server shows used percentage as 100% , when actually it is simply 20%. When I reboot the system, it comes back to 20%.Is this a bug in the system or my settings have gone wrong... (1 Reply)
Discussion started by: nua7
1 Replies

3. AIX

bug in 43 ???

xxxxserver# lsattr -El inet0 | grep 255.240.0.0,32.224.0.0,32.78.120.254 | grep '.40' route net,-hopcount,1,-netmask,255.240.0.0,32.224.0.0,32.78.120.254 How this is possible? (1 Reply)
Discussion started by: itik
1 Replies

4. Shell Programming and Scripting

top's exit code indicates error--is this a bug?

This single line of code in a sh script file top -b -n 1 -U $USER causes the script to prematurely exit with an exit code of 1 (i.e. an error) if the script is run with the -e option (e.g. if set -e is executed near the top of the script file). Alternatively, you can execute it like top... (8 Replies)
Discussion started by: fabulous2
8 Replies

5. UNIX for Dummies Questions & Answers

where's the bug?

#!/bin/bash if then #echo "infinite loop" exit 0 fi when I run this file I get the following error: ./test_infinite_loop: line 5: syntax error near unexpected token `fi' ./test_infinite_loop: line 5: `fi' :confused: (4 Replies)
Discussion started by: jon80
4 Replies

6. Programming

is this a bug of g++?

Hello, Im using the g++(g++ Ubuntu/Linaro 4.4.4-14ubuntu5 4.4.5) and im trying to compile a small snippet code and got into an endless loop.I recompiled that in VS2010 under Windows 7 and the answer is as expected.so i wonder is this a bug of g++?here is my code. #include<iostream> using... (5 Replies)
Discussion started by: homeboy
5 Replies

7. UNIX for Beginners Questions & Answers

Can't find the bug in my code - bombing with rename

Hi Folks - I'm encountering an issue: Scenario: We have automated GL data loads utilizing FDMEE. The problem is that some of our Locations could have multiple files. I think we are running into a situation where the script is trying to name the 2 files the same name and it is bombing out.... (8 Replies)
Discussion started by: SIMMS7400
8 Replies
All times are GMT -4. The time now is 05:05 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy