On Sun, Apr 3, 2011 at 5:05 PM, Wernher Eksteen <[email protected]> wrote:
> Got this to work, but is there a better way to do this?
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my ( $val, @matched, @unmatched, %hash1, %hash2 );
someone else is sure to call you out on defining this stuff at the
beginning of the program before i am able to hit send, but i figured
i'd add to the message - don't do this.
>
> %hash1 = (
> "emcpowera" => "sdbd sddg sdfj sdhm",
> "emcpoweraa" => "sdae sdch sdek sdgn",
> "emcpowerbc" => "sdb sdbe sddh sdfk",
> "emcpowerc" => "sdbb sdde sdfh sdhk",
> "emcpowerd" => "sdba sddd sdfg sdhj",
> "emcpowerz" => "sdba sddd sdfg sdhj"
> );
>
> %hash2 = (
> "emcpowera1" => "/dwpdb006",
> "emcpoweraa1" => "/dwpdb033",
> "emcpowerbc1" => "/s00_11",
> "emcpowerbc2" => "/utl_file_dir",
> "emcpowerc1" => "/odsdb006",
> "emcpowerd1" => "/odsdb005"
> );
>
> foreach my $i (keys(%hash1)) {
>
> foreach my $b (keys(%hash2)) {
>
> if ($b =~ /$i[0-9]+/) {
> $val = $b;
> push @matched, "$i" . " $hash1{$i} " . "$b" . "
> $hash2{$b} " . "\n";
not sure i get this, you get the keys, check if they match and assign
the values, why not just use 'each' if you want to check both keys and
values?
> }
> }
>
> if (not $i =~ /$val*/) {
> push @unmatched, "$i" . " $hash1{$i} " . "\n";
how about elsif?
> }
> }
>
> print " @matched";
> print " @unmatched\n";
you said you want a third hash, but then assign them to an array?
>
> --- RESULT ---
>
> emcpoweraa sdae sdch sdek sdgn emcpoweraa1 /dwpdb033
> emcpowerd sdba sddd sdfg sdhj emcpowerd1 /odsdb005
> emcpowerc sdbb sdde sdfh sdhk emcpowerc1 /odsdb006
> emcpowerbc sdb sdbe sddh sdfk emcpowerbc1 /s00_11
> emcpowerbc sdb sdbe sddh sdfk emcpowerbc2 /utl_file_dir
> emcpowera sdbd sddg sdfj sdhm emcpowera1 /dwpdb006
> emcpowerz sdba sddd sdfg sdhj
>
so, lets do what you asked (i like hashes better anyway :) )
my %hash3, %nothash;
my $found = 0;
while( my( $ikey, $ival ) = each( %hash1 ) ) {
while( my( $jkey, $jval ) = each( %hash2 ) ) {
if( ( $ikey == $jkey ) and ( $ival == $ikey ) ) {
$hash3{ $ikey } = $ival;
$found = 1;
}
}
if( $found == 0 ) {
%nothash{ $ikey } = $ival;
$found = 0;
}
not sure if that is any quicker and i just typed it, so it should work, but...
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/