user2256825 user2256825 - 4 months ago 7
Perl Question

Grepping a word/phrase in array's index and return the whole array in perl

I read an input file and record is seperated by double new line and
Storing a chunk (seperated by \n) in array
And while i try to grep a pattern "FEILD BEING REMOVED" , it is actually not picking it
Any issues with the way i am grepping,

my @msg = [split /\n/, $input_line ];
my @index_containing_FBR = grep { /FIELD BEING REMOVED/ } @msg;
print Dumper @index_containing_o;

__DATA__
<ALERT> ID=7 Loss with count of 10 for
Field Identifier:780222304404
Address:up.v.000654
Time: 1468581106

<ALERT> ID=8 Link Loss with count of 15 for
Field Identifier:780222304404
Address:up.v.000654
Time: 1468581114

<INFO> FIELD BEING REMOVED: an extended period of time - being removed from monitoring
FIELD Identifier:780222304203
Time: 146858308


Below data has 3 records and each record is seperated by double newline.
And fields in each record are seperated by a single newline, which i am pushing to an array @msg.
And finally iam trying to grep the "FIELD BEING REMOVED" pattern and would like the whole array to be returned.

The above code is not really fetching required stuff.

Please advice

Thanks
Tejas

Answer
use strict;
use warnings;
my $file = "d:/test.txt";
my $var;
{
    local $/;
    open my $fh, '<', $file or die "can't open $file: $!";
    $var = <$fh>;
}
my @array = split ( /\n\n/, $var); // you said the information is separated by double \n
use Data::Dumper;
print Dumper @array;

This is the content of the @array

my @index_containing_FBR = grep { /FIELD BEING REMOVED/ } @array; 
print Dumper @index_containing_FBR;

This is the content of @index_containing_FBR

Comments