perlsen has asked for the wisdom of the Perl Monks concerning the following question:

Hi, monks.

I have the arrays @num and @address.
arrays @num and @address total no of elements are equal.
remove duplicates from both the arrays and form the hash based on one element in @num array and another from the @address array if number coincides.
for example if the number 1 matches address1
then store in hash like key: 1 and value: address1 the following is the sample arrays which i need to form the hash(sorted) output.

@num array contains: (1, 1, 3, 2, 4, 3, 5) @address array contains:(address1, address1, address3, address2, addre +ss4, address3, address5 ) outputs in hash: ---------------- 1 address1 2 address2 3 address3 4 address4 5 address5

Updated

Thanks for suggestions.

Replies are listed 'Best First'.
Re: Problem regarding storing the values in Hash.
by gopalr (Priest) on Feb 19, 2005 at 10:30 UTC

    Hi,

    @num=('1','1','3','2','4','3','5'); @address=('address1','address1', 'address3','address2', 'address4','ad +dress3', 'address5'); @num = grep (!$uniques{$_}++, @num); # remove the duplicates @address = grep (!$uniques{$_}++, @address); # remove the duplicates foreach $num(@num) { foreach $add(@address) { if ($add=~m#address${num}#) { $hash{$num}=$add; } } } print "\n$_ = $hash{$_}" for sort keys %hash;
Re: Problem regarding storing the values in Hash.
by perl_lover (Chaplain) on Feb 19, 2005 at 10:21 UTC
Re: Problem regarding storing the values in Hash.
by trammell (Priest) on Feb 19, 2005 at 14:08 UTC
    Let the hash take care of duplicate key removal:
    my %hash; foreach my $i (0 .. $#num) { my $key = $num[$i]; my $value = $address[$i]; $hash{$key} = $value; } print "$_ => $hash{$_}\n" for keys %hash;
    It's not clear to me from your problem description that this is correct, but I'll submit it as a "talking point".
      ObGolf:
      @num = (1, 1, 3, 2, 4, 3, 5); @address = (address1, address1, address3, address2, address4, address3 +, address5 ); my %hash; @hash{@num}=@address; print "$_ => $hash{$_}\n" for sort {$a <=> $b} keys %hash;
      I added the sort since it looks like the OP wanted it.
Re: Problem regarding storing the values in Hash.
by saintmike (Vicar) on Feb 19, 2005 at 08:10 UTC
    I have no idea what you're asking.

    You've got two arrays, one that contains elements in some number-like format (but you're not saying how it looks exactly) and another array containing addresses. But are those elements strings? Or references to arrays?

    Can you show the perl code that doesn't work, so that we can figure out what you mean?