Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl - compares array elements sentences with a variable

Tags:

arrays

grep

perl

I use grep to return unmatched array between temporaryF file and arrayWarning

my @c =grep!${{map{$_,1}@temporaryF}{$_},@arrayWarning;

Inside @c there are alot of lines for example:

Sun Sep 30 00:05:55 fibre channel DENY forever
Sun Sep 30 00:06:55 fibre channel ROOT cause
Sun Sep 30 00:08:55 fibre channel ROOT cause 
Sun Sep 30 00:10:55 fibre channel ROOT cause  
Sun Sep 30 00:20:55 fibre channel DANN 
Sun Sep 30 00:30:55 fibre channel DANN  

as you can see ROOT occurs 3 times in @c. How can I iterate through @c to output only the latest occurrence of ROOT -> Sun Sep 30 00:10:55 fibre channel ROOT and not the other repeated lines.

so it will become:

Sun Sep 30 00:05:55 fibre channel DENY forever  
Sun Sep 30 00:10:55 fibre channel ROOT cause  
Sun Sep 30 00:30:55 fibre channel DANN
like image 300
momokjaaaaa Avatar asked Oct 28 '12 18:10

momokjaaaaa


People also ask

What is the difference between list and array in Perl?

In Perl, List and Array terms are often used as if they're interchangeable. But the list is the data, and the array is the variable. Array variables are prefixed with the @ sign and are populated using either parentheses or the qw operator.

What is the Perl compare string?

The Perl compare string is useful for authentication and matching the two string variable and their value. It is useful for sorting, searching, and filtering more than one string value. This is a guide to Perl compare strings.

What is the use of $[ variable in Perl?

Because Perl arrays have zero-based indexing, $ [ will almost always be 0. But if you set $ [ to 1 then all your arrays will use on-based indexing. It is recommended not to use any other indexing other than zero. However, let's take one example to show the usage of $ [ variable −

How to initialize an array variable in Perl?

Initialization of an array variable we need to use a $ sign to display a single element of array variables. Below is the example of an array variable in perl is as follows. @ages = (20, 25, 30, 35, 40); # integer type variable array declaration.


1 Answers

Note: this is an extension of the answer of @RobEarl - so if you like it, please make sure to give him credits, too!

The point here is to store the line count too, to make sure the output can be ordered.

Long version

#!/usr/bin/perl

use strict;
use warnings;

# store (with count)
my $count  = 0;
my %latest = map {
    my $source = (split /\s+/ => $_)[6];
    $source => {count => $count++, string => $_};
} <DATA>;

# output
print $_->{string} for sort {$a->{count} <=> $b->{count}} values %latest;

__DATA__
Sun Sep 30 00:05:55 fibre channel DENY forever
Sun Sep 30 00:06:55 fibre channel ROOT cause
Sun Sep 30 00:08:55 fibre channel ROOT cause 
Sun Sep 30 00:10:55 fibre channel ROOT cause  
Sun Sep 30 00:20:55 fibre channel DANN 
Sun Sep 30 00:30:55 fibre channel DANN  

Output:

Sun Sep 30 00:05:55 fibre channel DENY forever
Sun Sep 30 00:10:55 fibre channel ROOT cause  
Sun Sep 30 00:30:55 fibre channel DANN  

Feels a little bit like the Schwartzian transform.

One-liner version

This is an excellent example for a task that can be accomplished by a simple oneliner with perl's powerful interpreter switches:

$ perl -nale '$l{$F[6]}={c=>$c++,s=>$_};END{print$_->{s}for sort{$a->{c}<=>$b->{c}}values%l}'
Sun Sep 30 00:05:55 fibre channel DENY forever
Sun Sep 30 00:06:55 fibre channel ROOT cause
Sun Sep 30 00:08:55 fibre channel ROOT cause 
Sun Sep 30 00:10:55 fibre channel ROOT cause  
Sun Sep 30 00:20:55 fibre channel DANN 
Sun Sep 30 00:30:55 fibre channel DANN  

Output:

Sun Sep 30 00:05:55 fibre channel DENY forever
Sun Sep 30 00:10:55 fibre channel ROOT cause  
Sun Sep 30 00:30:55 fibre channel DANN  
like image 175
memowe Avatar answered Nov 15 '22 04:11

memowe