perl hash ..confused


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl hash ..confused
# 1  
Old 12-14-2007
Question perl hash ..confused

hi
i want to generate standard file from log file. log file containts information about files which are copied to testing region. log files looks as follows
Code:
date           time          label                file                 ver      date 
2007-12-13 20:25        SPR 30417       CI0051.COB      1.35    20071213        /Clients/NCB/BANCS-24/cob/CI0051.COB    /BASE/src/CI0051.COB
2007-12-13 20:35        SPR 30418       060658.xml      1.2     20071213        /Clients/NCB/Frontend/xml/Transactions/060658.xml       /FEBase/xml/Transactions/060658.xml
2007-12-13 20:39        SPR 30417       IN0011.COB      1.7     20071213        /Clients/NCB/BANCS-24/cob/IN0011.COB    /BASE/src/IN0011.COB
2007-12-13 21:17        SPR 30417       032001.htm      1.1     20071213        /Clients/NCB/Frontend/html/032001.htm   /FEBase/html/032001.htm

now there can be many entries for single file but with different versions but i want that only file with latest version should be present in new file.
i am using perl for this there are total 5 scripts to do final job 4 are ready i am stuck with this one to get unique components only

for eg if input file is
Code:
.....IN0000.COB     1.2     ......
.....login.htm         1.1     .....
.....IN0000.COB     1.3     ....
           .
           .

oputfile should be
Code:
.....login.htm         1.1     .....
.....IN0000.COB     1.3     ....
                 .
                 .

i have hash in which i store file name and its key is version no i check entry of file in hash if its there then i comaprare otherwise i add in hash but what i want is that other information on that is importatnt for same file ii may change also like version label so final file should be list of uniq files with all other information intact as shown in first file.
# 2  
Old 12-14-2007
I'm not totally sure what you want to do. But to limit display of lines with identical filename to the highest version only, you can put the line into the hash and after filtering you will get what you want. Here is an example (5 lines into 4):

Code:
@lines = split(/\n/, "2007-12-13 20:25        SPR 30417       CI0051.COB      1.35    20071213        /Clients/NCB/BANCS-24/cob/CI0051.COB    /BASE/src/CI0051.COB
2007-12-13 20:35        SPR 30418       060658.xml      1.2     20071213        /Clients/NCB/Frontend/xml/Transactions/060658.xml       /FEBase/xml/Transactions/060658.xml
2007-12-19 20:35        SPR 30418       060658.xml      1.12     20071219        /Clients/NCB/Frontend/xml/Transactions/060658.xml       /FEBase/xml/Transactions/060658.xml
2007-12-13 20:39        SPR 30417       IN0011.COB      1.7     20071213        /Clients/NCB/BANCS-24/cob/IN0011.COB    /BASE/src/IN0011.COB
2007-12-13 21:17        SPR 30417       032001.htm      1.1     20071213        /Clients/NCB/Frontend/html/032001.htm   /FEBase/html/032001.htm");

sub ver_compare {
my ($a2, $b2) = map { [split(/\./, $_)] } @_;
return ($a2->[0] <=> $b2->[0] || $a2->[1] <=> $b2->[1]);
}

%line_maxvers = ();
foreach $line (@lines) {
($date, $time, $label1, $label2, $fn, $ver) = split(/\s+/, $line);
$line_maxvers{$fn} = [$ver, $line] if (!$line_maxvers{$fn} || ver_compare($ver, $line_maxvers{$fn}) > 0);
}

foreach (values %line_maxvers) {
print $_->[1], "\n";
}

# 3  
Old 12-15-2007
Question

hi
thanks cbkihong for ur reply

got what is the error in my code following link provided good help

Perl Hash Howto


my file which is input to this script :
Code:
2007-12-13 20:25	SPR 30417	CI0051.COB	1.35	20071213	lanthanum	abcdefg	/ABC/efg/BANCS-24/cob/CI0051.COB	/BASE/src/CI0051.COB
2007-12-13 20:35	SPR 30418	060658.xml	1.2	20071213	lanthanum	abcdefg	/ABC/efg/Frontend/xml/Transactions/060658.xml	/FEBase/xml/Transactions/060658.xml
2007-12-13 20:39	SPR 30417	IN0011.COB	1.6	20071213	lanthanum	abcdefg	/ABC/efg/BANCS-24/cob/IN0011.COB	/BASE/src/IN0011.COB
2007-12-13 21:17	SPR 30417	032001.htm	1.1	20071213	lanthanum	abcdefg	/ABC/efg/Frontend/html/032001.htm	/FEBase/html/032001.htm
2007-12-13 23:00	SPR 30251	CommissionFetch.htc	1.5	20071213	Khalid	abcdefg	/ABC/efg/Core/htc/FlowComponents/CommissionFetch.htc	/FEBase/htc/FlowComponents/CommissionFetch.htc
2007-12-13 20:39	SPR 30410	IN0011.COB	1.7	20071213	lanthanum	abcdefg	/ABC/efg/BANCS-24/cob/IN0011.COB	/BASE/src/IN0011.COB

output i am getting with this error :
Code:
# display from subroutine stored indicates version stored in hash and read is one which need to be compared 
# stored : HASH(0x4014f620) 	 read : 1.7
# HASH(0x4014f620)0000000000000000000000000000 :1st sting 
#00010007000000000000000000000000 :2nd sting 
/Clients/NCB/Frontend/html/032001.htm	1.1	20071213	SPR 30417	/FEBase/html/032001.htm
/Clients/NCB/Frontend/xml/Transactions/060658.xml	1.2	20071213	SPR 30418	/FEBase/xml/Transactions/060658.xml
/Clients/NCB/BANCS-24/cob/CI0051.COB	1.35	20071213	SPR 30417	/BASE/src/CI0051.COB
/Clients/NCB/Core/htc/FlowComponents/CommissionFetch.htc	1.5	20071213	SPR 30251	/FEBase/htc/FlowComponents/CommissionFetch.htc
Can't use string ("CommissionFetch.htc") as a HASH ref while "strict refs" in use at test.pl line 56, <PROLOG> line 6.

Now getting the proper output
Code:
/ABC/efg/Frontend/html/032001.htm   1.1     20071213        SPR 30417       /FEBase/html/032001.htm
/ABC/efg/Frontend/xml/Transactions/060658.xml       1.2     20071213        SPR 30418       /FEBase/xml/Transactions/060658.xml
/ABC/efg/BANCS-24/cob/CI0051.COB    1.35    20071213        SPR 30417       /BASE/src/CI0051.COB
/ABC/efg/Core/htc/FlowComponents/CommissionFetch.htc        1.5     20071213        SPR 30251       /FEBase/htc/FlowComponents/CommissionFetch.htc


Last edited by zedex; 04-11-2008 at 02:47 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 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

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

4. Shell Programming and Scripting

Getting confused about passing parameters to perl

I've got a large text file, cleanme, that I want to process for all combinations of words in a second file, commonwords. So I can iterate through commonwords like so: filearray=( `cat commonwords | tr '\n' ' '`) filearray=( `cat commonwords | tr '\n' ' '`) for firstword in ${filearray} ... (2 Replies)
Discussion started by: JamesForeman
2 Replies

5. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

6. Shell Programming and Scripting

perl hash

This is my data 1 0 1 0 1 1 1 2 1 6 1 7 Assume that first field is key and 2nd field is value I want to create a hash in perl, on this data. My hash should having uniq key and all values by , separated. 1,0,0,1,2,6,7 1 will be my key and rest of are should be values. (3 Replies)
Discussion started by: pritish.sas
3 Replies

7. Shell Programming and Scripting

perl hash

i have an hash table in which each value is an array. How can i print for each key the array values??? something like this: thanks (2 Replies)
Discussion started by: littleboyblu
2 Replies

8. Shell Programming and Scripting

Perl Hash

hi i have two hash achi %disk1,%disk2 with( key, value) (key1,value1) How to store it in another hash.. Plz replyyy. Regards Hari (1 Reply)
Discussion started by: Harikrishna
1 Replies

9. Shell Programming and Scripting

Perl Hash

HI I have a hash like this $hashname->{$filesystem}->{'fsname'}=$filesystem; How to get the values from this multilevel hash. Thanks in advance... :) (1 Reply)
Discussion started by: Harikrishna
1 Replies

10. Shell Programming and Scripting

Hash in perl

Hi Help me with some good links of Hash with in Hash .(Multidimensional hash).. Regards Harikrishna (1 Reply)
Discussion started by: Harikrishna
1 Replies
Login or Register to Ask a Question