Comparing hash tables

R

Rem-8

Hello

I've been sitting onto it for some time and I didn't have any clue how
to work it out.
Here's the idea.
I got two tables declared from on the beginning and need to generate a
hash table from such thing:

# These are declared tables:
my @xx = ('01', '02', '03', '04', '05', '06', '07', '08', '09', '10',
'11', '12', '13');
my @y = ('0', '1', '2', '3');

# From this I heed to create hash:
if ($line =~ /^.*-(\S+)\s+\S+\s+-f(\S+)\s+\S+.*$/) {
$1 will stand for @bcxx and $2 will stand for @bcy

Now I need to create hash tables from those values and compare them.
Finally I need to show values which do not fit. A clue is that @y
values should exist in each @xx, so there has to be 01 with values 0,
1, 2, 3.

I thought to do it like this:

$hash_xx_y{$xx}{$y} = 1
$hash_bcxx_bcy{$bcxx}{$y} = undef/1 - table for read values (may not
exist = undef; or they are ok = 1)

Then compare values from those two tables and print undefined values.
Can somebody give me a clue? Don't need whole code, just a clue how to
do it...

Thanks
 
M

Mirco Wahab

Rem-8 said:
I've been sitting onto it for some time and I didn't have any clue how
to work it out.
Here's the idea.
I got two tables declared from on the beginning and need to generate a
hash table from such thing:

# These are declared tables:
my @xx = ('01', '02', '03', '04', '05', '06', '07', '08', '09', '10',
'11', '12', '13');
my @y = ('0', '1', '2', '3');

OK, so we have two tables (thats a good starting point):
my @xx = qw'01 02 03 04 05 06 07 08 09 10 11 12 13';
my @y =qw'0 1 2 3';
# From this I heed to create hash:
if ($line =~ /^.*-(\S+)\s+\S+\s+-f(\S+)\s+\S+.*$/) {
$1 will stand for @bcxx and $2 will stand for @bcy

This would mean, your data looks like (?):

my $line = "jkedr3jf -AD70 TtET4 -fK5621 TE43D jdjdj";
Now I need ...
Finally I need ...
I thought to do ...
Can somebody give me a clue?

I think you didn't manage to explain your case properly.

In the first place, you should start with a paragraph
stating what your input data is (exact example) and what
your results should look like (and whats' the purpose of it).

Without this information, only the very best Perl magicians
of the word would ever be able to guess your intention
and come up with the code.

Regards

Mirco
 
R

Rem-8

Mirco Wahab napisal(a):
OK, so we have two tables (thats a good starting point):
my @xx = qw'01 02 03 04 05 06 07 08 09 10 11 12 13';
my @y =qw'0 1 2 3';


This would mean, your data looks like (?):

my $line = "jkedr3jf -AD70 TtET4 -fK5621 TE43D jdjdj";


I think you didn't manage to explain your case properly.

In the first place, you should start with a paragraph
stating what your input data is (exact example) and what
your results should look like (and whats' the purpose of it).

Without this information, only the very best Perl magicians
of the word would ever be able to guess your intention
and come up with the code.

Regards

Mirco



OK, so here we go:

The table on the top is right

Now the read data has to be the same as the table because we have text
like this:

instance -01 copy -f0 size 3441
instance -01 copy -f1 size 4321
instance -01 copy -f2 size 3441
instance -01 copy -f3 size 4321
instance -02 copy -f0 size 3441
instance -02 copy -f1 size 4321
instance -02 copy -f2 size 3441
instance -03 copy -f0 size 4321

Now $1 will be 01, 02, 03 and $2 would be 0, 1, 2, 3.
As you see, the copy 3 is missing for instance 02 - that will be a
mistake, because every instance from 01 to 13 has to have copies from 0
to 3. This why I need to compare those values
 
M

Mirco Wahab

Rem-8 said:
Now $1 will be 01, 02, 03 and $2 would be 0, 1, 2, 3.
As you see, the copy 3 is missing for instance 02 - that will be a
mistake, because every instance from 01 to 13 has to have copies from 0
to 3. This why I need to compare those values

OK, I see it more clearly now. But if you know
you have the instances you have to have, you
can imply them,

use strict;
use warnings;

my (%inst, @copy, %h); # inst not needed
# @inst{ q(01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13) } = 1;
@copy =qw'0 1 2 3';

# collect
while(<>) {
push @{ $h{$1} }, $2 if (/[^-]+-(\S+).+?-f(\S+)/)
}
# compare
for my $i (sort keys %h) { # extended somehow for clarity
my $found = join '', sort grep defined, @{ $h{$i} }[0 .. @copy-1];
my $need = join '', @copy;
print $found eq $need ? "OK: $i\n" : "ERROR: $i ($found)\n";
}


this expects your data fed via STDIN '<>'

Regards

M.
 
J

John W. Krahn

Rem-8 said:
OK, so here we go:

The table on the top is right

Now the read data has to be the same as the table because we have text
like this:

instance -01 copy -f0 size 3441
instance -01 copy -f1 size 4321
instance -01 copy -f2 size 3441
instance -01 copy -f3 size 4321
instance -02 copy -f0 size 3441
instance -02 copy -f1 size 4321
instance -02 copy -f2 size 3441
instance -03 copy -f0 size 4321

Now $1 will be 01, 02, 03 and $2 would be 0, 1, 2, 3.
As you see, the copy 3 is missing for instance 02 - that will be a
mistake, because every instance from 01 to 13 has to have copies from 0
to 3. This why I need to compare those values

This should give you some ideas to get you started:


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


my @xx = '01' .. '13';
my @y = '0' .. '3';

my $xx_index = $#xx;
my $y_index = $#y;

while ( <DATA> ) {
next unless /-(\d+)\s+\S+\s+-f(\d+)\s+\S+/;

$y_index = ( $y_index + 1 ) % @y;
$xx_index = ( $xx_index + 1 ) % @xx unless $y_index;

if ( $1 ne $xx[ $xx_index ] || $2 ne $y[ $y_index ] ) {
print "Error: missing entry for xx=$xx[$xx_index] and y=$y[$y_index]\n";
redo;
}
}


__DATA__
instance -01 copy -f0 size 3441
instance -01 copy -f1 size 4321
instance -01 copy -f2 size 3441
instance -01 copy -f3 size 4321
instance -02 copy -f0 size 3441
instance -02 copy -f1 size 4321
instance -02 copy -f2 size 3441
instance -03 copy -f0 size 4321




John
 
R

Rem-8

John W. Krahn napisal(a):
Rem-8 said:
OK, so here we go:

The table on the top is right

Now the read data has to be the same as the table because we have text
like this:

instance -01 copy -f0 size 3441
instance -01 copy -f1 size 4321
instance -01 copy -f2 size 3441
instance -01 copy -f3 size 4321
instance -02 copy -f0 size 3441
instance -02 copy -f1 size 4321
instance -02 copy -f2 size 3441
instance -03 copy -f0 size 4321

Now $1 will be 01, 02, 03 and $2 would be 0, 1, 2, 3.
As you see, the copy 3 is missing for instance 02 - that will be a
mistake, because every instance from 01 to 13 has to have copies from 0
to 3. This why I need to compare those values

This should give you some ideas to get you started:


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


my @xx = '01' .. '13';
my @y = '0' .. '3';

my $xx_index = $#xx;
my $y_index = $#y;

while ( <DATA> ) {
next unless /-(\d+)\s+\S+\s+-f(\d+)\s+\S+/;

$y_index = ( $y_index + 1 ) % @y;
$xx_index = ( $xx_index + 1 ) % @xx unless $y_index;

if ( $1 ne $xx[ $xx_index ] || $2 ne $y[ $y_index ] ) {
print "Error: missing entry for xx=$xx[$xx_index] and y=$y[$y_index]\n";
redo;
}
}


__DATA__
instance -01 copy -f0 size 3441
instance -01 copy -f1 size 4321
instance -01 copy -f2 size 3441
instance -01 copy -f3 size 4321
instance -02 copy -f0 size 3441
instance -02 copy -f1 size 4321
instance -02 copy -f2 size 3441
instance -03 copy -f0 size 4321




John


Thanks for the replies! It realy helped me!

Cheers!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top