Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hash problem

Status
Not open for further replies.

greenlakers

Technical User
Jan 19, 2009
3
CA
Hi Guys,
I am getting this output from my code:-

TOTAL BulletinID KBID date
3 958439 ABCDEF MS08-074


But I am trying to get this output.

TOTAL BulletinID KBID 2009-01-08 2009-01-09
3 958439 ABCDEF MS08-074 2 1


but i am unable to get the desired output from the code. Can anyone please help?

This is my code:-

#!/usr/local/bin/perl

my @sorted;
$sorted[0] = 'ABCDEF 958439 MS08-074 Security Update for the 2007 Microsoft Office System (KB958439) 2009-01-08';
$sorted[1] = 'FFFFFF 958439 MS08-074 Security Update for the 2007 Microsoft Office System (KB958439) 2009-01-08';
$sorted[2] = 'FFFFFF 958439 MS08-074 Security Update for the 2007 Microsoft Office System (KB958439) 2009-01-09';

my $current = undef;
my $prev = undef;
my %filtered = ();
foreach my $line (@sorted){
my @split = split(/\s/, $line);

if (defined $filtered{$split[1]})
{
substr($filtered{$split[1]},0,1) = (++substr($filtered{$split[1]},0,1));

print "IF $filtered{$split[1]}\n";

if (defined $filtered{$split[12]})
{
substr($filtered{$split[12]},0,1) = (++substr($filtered{$split[12]},0,1));
print "IFIF $filtered{$split[12]}\n";
}
else
{
$filtered{$split[12]} = "1";
print "IFELSE $filtered{$split[12]}\n";
}
}

else {
$filtered{$split[1]} = "1\t$split[1]\t$split[0]\t$split[2]";
print "ELSE $split[1]\n";
print "ELSE $filtered{$split[1]}\n";
$filtered{$split[12]} = "1";
print "ELSE $split[12]\n";
print "ELSE $filtered{$split[12]}\n";
}
}

foreach my $line (values %filtered{$split[1]}){
print "$line\n";
}
print "\n";
 
But I am trying to get this output.

TOTAL BulletinID KBID 2009-01-08 2009-01-09
3 958439 ABCDEF MS08-074 2 1
Is the BulletinID 958439 ABCDEF? If so, how do you decide it's ABCDEF and not FFFFFF (since they are both present on the same day.)
 
Did you actually try and run the code you posted? This line will obviously return a syntax error:

foreach my $line (values %filtered{$split[1]}){

because %filtered{$split[1]} is not a hash, its a scalar.

These lines make no sense to me:

substr($filtered{$split[1]},0,1) = (++substr($filtered{$split[1]},0,1));

Answer this question: is this school work?






------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
The code you've given us does not give the output you say you're getting.

It looks like an overly complex way of counting the occurrences of certain strings within your data, and one that's flawed too - what happens if a given number occurs more than 9 times?

Perhaps you could take a step back and tell us what you're trying to achieve?

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Hello,

Being currently un-employed means that I have far too much time of my hands, which I enjoy spending programming in Perl hehe! I couldn't ignore greenlaker's issue and simply had to produce a script which fufills the task.

Firstly, the relationship between every line is:
[blue]958439 MS08-074 Security Update for the 2007 Microsoft Office System (KB958439)[/blue]. This is important because I have assumed that you have lots more data you haven't provided which will possibly need to be separated from the data you provided.

Secondly, I can only assume that you only want to use the [blue]1st[/blue] "kbid (part1)" (which is "ABCDEF") for each relationship group.

As you are probably a student (and we don't provide students with in-depth help, especially if you don't respond to comments in the threads you started!), I won't post my script but ill give you a couple of ideas...

>>> Because of the complexity of the final data structure, maybe store the data in a multi-dimensional hash table, which also simplifies the manipulation of parts of the data.

>>> Instead of using the split function, I used a regular expression to check each line (which may or may not be advantageous, especially because its reasonably long/complex). I really only did this so that I could split each line up in a single line of code. This would also help if I only wanted to use lines of a particular format.

>>> Others have notified you that there are syntax errors in your code, its limited in terms of what I think you are trying to achieve, and ultimately looks pretty poorly coded for such a task. I would scrap it and re-think!

Finally I have drawn out the final %hash structure I created.
Code:
#***BREAKDOWN OF FINAL HASH TABLE (%hash):***
#
#958439 MS08-074 Security Update for the 2007 Microsoft Office System (KB958439) {{{
#	
#	kbid {{{
#		scaler >>>>> ABCDEF
#	}}}
#	
#	dates {{{
#		2009-01-08 {{{
#			scaler >>>>> 2
#		}}}
#		2009-01-09 {{{
#			scaler >>>>> 1
#		}}}
#	}}}
#	
#	other_data {{{
#		array >>>>> ("958439", "MS08-074")
#	}}}
#	
#}}}
#
#
#
#***FINAL OUTPUT (not in the format you specified):***
#
#BulletinID = 958439
#KBID = ABCDEF MS08-074
#2009-01-09 = 1
#2009-01-08 = 2
#TOTAL = 3

Goodluck,

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top