with simple hashs.
What I hope to do is compare hashes and a few different ways.
determine what is in one and not in the other for example.
These exmple hashes are supposed to represent file names.
The hashes are created by making the key the full path and file name,
and the value just the end filename
/some/path/name => name
I'm looking to compare hashes on the last elements
(in square brackets) /x/y/[z] .. not trying to match the full file
name, only the last part.
key value
/some/path/name => name
/some/other/path/name => name
would be a match
The idea is to determine what is one hash but not the other in terms
of values ... as above.
It was recommended to me to reader perldoc perllol.
After about the 4/5 paragraph, I'm already seeing red. A little
further along and I'm hopelessly confused .. and that is with a
simpler data structure... arrays.
Its going to take a while for me to understand these ideas I'm afraid.
I've been shown at least one example of using `exists' for something
like this, but not really understanding it..
So I've devised couple of example hashes and run each through a double
`foreach' loop to get the information I'm after.
But I have the nagging feeling there is some easier and less labor
intensive way to do this.
In the case below... I'm spinning though each hash one full loop for
each line of the other hash.... lots of spinning involved.
It appears to work, in so far as extracting what I wanted to know.
Output looks like:
,----
No match: (%h1) f1 ne anything in (%h2)
No match: (%h1) fa ne anything in (%h2)
Match: (%h1) fb eq (%h2) fb
Match: (%h1) fb eq (%h2) fb
Match: (%h1) f2 eq (%h2) f2
----- ---=--- ----- ---=--- -----
No match: (%h2) fc ne anything in (%h1)
Match: (%h2) fb eq (%h1) fb
No match: (%h2) fd ne anything in (%h1)
Match: (%h2) fb eq (%h1) fb
Match: (%h2) f2 eq (%h1) f2
`----No match: (%h1) fa ne anything in (%h2)
Match: (%h1) fb eq (%h2) fb
Match: (%h1) fb eq (%h2) fb
Match: (%h1) f2 eq (%h2) f2
----- ---=--- ----- ---=--- -----
No match: (%h2) fc ne anything in (%h1)
Match: (%h2) fb eq (%h1) fb
No match: (%h2) fd ne anything in (%h1)
Match: (%h2) fb eq (%h1) fb
Match: (%h2) f2 eq (%h1) f2
But, is there an easier way?
------- --------- ---=--- --------- --------
#!/usr/local/bin/perl
use strict;
use warnings;
my %h1 = (
'./b/f1' => 'f1',
'./b/c/fa' => 'fa',
'./b/l/c/f2' => 'f2',
'./b/g/f/r/fb' => 'fb'
);
my %h2 = (
'./b/fb' => 'fb',
'./b/c/fd' => 'fd',
'./b/l/c/f2' => 'f2',
'./b/g/f/r/fc' => 'fc',
'./b/g/h/r/fb' => 'fb'
);
## Trot all of %h2 through for every line of %h1
foreach my $h1val (values %h1) {
my $hit = 0;
foreach my $h2val (values %h2){
if($h1val eq $h2val){
$hit++;
printf "%-15s %s %s %s\n", "Match: (%h1)",$h1val, " eq (%h2)",$h2val;
}
}
if (!$hit) {
printf "%-15s %s %s %s\n","No match: (%h1)",$h1val, " ne ","anything in h2";
}
}
print "----- ---=--- ----- ---=--- -----\n";
## Trot all of %h1 through for every line of %h2
foreach my $h2val (values %h2) {
my $hit = 0;
foreach my $h1val (values %h1){
if($h2val eq $h1val){
$hit++;
print "Match: (%h2) $h2val eq (%h1) $h1val\n";
}
}
if (!$hit) {
print "No match: (%h2) $h2val ne anything in %h1\n";
}
}