Danial Haris
Danial Haris

Reputation: 109

How to print $_

How can I print $_ outside using grep? I want to print the data inside %try_1 if it match with %try .........

#!/usr/bin/perl
use strict;
use warnings;
use Tie::Autotie 'Tie::IxHash';

tie my %try, 'Tie::IxHash';
$try{STRONG}{ALLIES}='A';
$try{AGILE}{BOBBY}='B';
$try{AGILE}{HOBBY}='B';
$try{SMART}{CAKRA}='C';
$try{SMART}{PHONE}='C';
$try{SMART}{PEOPLE}='C';
my %try_1;
$try_1{STRONGER}='A';
$try_1{AGILER}='B';
$try_1{SMARTER}='C';

foreach my $temp_0 (keys %try)
{
    print "\n".$_."\n" if (grep {$_ =~ /$temp_0/i} (keys %try_1));
 }

Upvotes: 0

Views: 123

Answers (1)

Chris Charley
Chris Charley

Reputation: 6573

You're almost there. Change:

print "\n".$_."\n" if (grep {$_ =~ /\Q$temp_0/i} (keys %try_1));

To:

print "\n".$_."\n" for grep {/\Q$temp_0/i} keys %try_1;

The \Q handles any special character $temp_0 might contain.

Upvotes: 3

Related Questions