The stupid question is the question not asked PerlMonks

### checking the matches

by tweetymonk (Initiate)
 on May 22, 2008 at 09:25 UTC Need Help??

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

hii monks... i have 2 different arrays like this :
```@array1=('146.234','15.11','78.3','890',3423.23);
@array2=('15.11#abc','890#def','78.3#ghi');
What i want to do is, 1)split the second array (@array2)as numbers and text. 2)compare the numbers with the elements of the 1st array (@array1). 3)which ever number matches, only its corresponding text and number should be displayd.
```so, the result should look like, in an array,

abc = 15.11,
def = 890,
ghi = 78.3

Replies are listed 'Best First'.
Re: checking the matches
by moritz (Cardinal) on May 22, 2008 at 09:30 UTC
What did you try so far? It's not really complicated, but you won't learn anything unless you try it yourself.

Two small hints: take a look at split, and consider storing one of your arrays in a hash.

Re: checking the matches
by ikegami (Pope) on May 22, 2008 at 09:32 UTC
```my %hash = map { split /#/ } @array2;

for (@array1) {
next if !exists(\$hash{\$_});
print("\$hash{\$_} = \$_\n");
}

Good, if you're sure that @array1 has no duplicated value.

Rule One: Do not act incautiously when confronting a little bald wrinkly smiling man.

A reply falls below the community's threshold of quality. You may see it by logging in.
Re: checking the matches
by mwah (Hermit) on May 22, 2008 at 09:58 UTC

Ikegami already provided a very compact solution using a hash. A more explicit (looped) variant would read like:

``` ...
for my \$element (@array2) {
my (\$num, \$txt) = split /#/, \$element;  # extract parts
print "\$txt = \$num\n" if grep \$num eq \$_, @array1  # use text compa
+rison and print txt part
}
...

Regards

mwa

This is working absolutely fine, but is it possible to match only the numbers before the decimal point???

Whats meant by "match the numbers before the decimal point"?

One way could involve to 'int-ify' all comparisons:

``` ...
for my \$element (@array2) {
my (\$num, \$txt) = split /#/, \$element;  # extract parts
print "\$txt = ". int(\$num) ."\n"
if grep int(\$num) eq int(\$_), @array1  # use text comparison and
+ print txt part
}
...

But I'm not entirely sure what you are trying to do.

Regards

mwa

Re: checking the matches
by poolpi (Hermit) on May 22, 2008 at 10:54 UTC

Efficently?
Not sure ;)

```my %h =
map{ reverse /(\d+).*#(.+)/ } grep "@array1", @array2;
print Dumper \%h;
```Output:
\$VAR1 = {
'def' => '890',
'abc' => '15',
'ghi' => '78'
};

hth,
PooLpi

'Ebry haffa hoe hab im tik a bush'. Jamaican proverb

Create A New User
Node Status?
node history
Node Type: perlquestion [id://687909]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2021-05-18 01:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Perl 7 will be out ...

Results (171 votes). Check out past polls.

Notices?