Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: Using an array list in order to select specific columns

Tags:

arrays

perl

I have a python script which is reading data and output only specific columns into a file.

I would like to do the same in Perl. I've done this in Perl (script below) but I can't used an array like I do in Python with the selector.

Here the script with the DATA embedded

#!/usr/bin/python

from StringIO import StringIO

path = "/tmp/log.csv"

__DATA__ = '''
20180704150930|rtsp|645645643|30193|211|KLM|KLM00SD624817.ts|172.30.16.34|127299264|VERB|01780000|21103|277|server01|OK
20180704150931|api|456456546|30130|234|VC3|VC300179201139.ts|172.30.16.138|192271838|VERB|05540000|23404|414|server01|OK
20180704150931|api|465456786|30154|443|BAD|BAD004416550.ts|172.30.16.50|280212202|VERB|04740000|44301|18|server01|OK
20180704150931|api|5437863735|30157|383|VSS|VSS0011062009.ts|172.30.16.66|312727922|VERB|05700000|38303|381|server01|OK
20180704150931|api|3453432|30215|223|VAE|VAE00TF548197.ts|172.30.16.74|114127126|VERB|05060000|22305|35|server01|OK
20180704150931|api|312121|30044|487|BOV|BOVVAE00549424.ts|172.30.16.58|69139448|VERB|05300000|48708|131|server01|OK
20180704150931|rtsp|453432123|30127|203|GZD|GZD0900032066.ts|172.30.16.58|83164150|VERB|05460000|20303|793|server01|OK
20180704150932|api|12345348|30154|465|TYH|TYH0011224259.ts|172.30.16.50|279556843|VERB|04900000|46503|241|server01|OK
20180704150932|api|4343212312|30154|326|VAE|VAE00TF548637.ts|172.30.16.3|28966797|VERB|04740000|32601|969|server01|OK
20180704150932|api|312175665|64530|305|TTT|TTT000000011852.ts|172.30.16.98|47868183|VERB|04740000|30501|275|server01|OK
'''

selector = [0, 3, 5, 7]

__DATA__ = StringIO(__DATA__)
with    open(path + ".py.txt", "w") as output:
        for line in __DATA__:
                line = line.rstrip( "\r\t\n" )
                if not line.strip(): continue  # skip the empty line
                columns = line.split("|")
                newColumns = [ columns[i] for i in selector ]
                output.write("|".join(newColumns) + "\n")
                print ("|".join(newColumns))

Question

So, with the Perl script I have an array like Python On my script I'm doing that without using the array in a loop I do directly $tab[0] etc... I don't know how do the same with an array list I would like to use the @list instead

Here the same code in Perl

#!/usr/bin/perl
use strict;
use warnings;
use autodie;

my $path = "/tmp/log.csv";
my @list = (0, 3, 5, 7);

open(OUT, ">", $path . ".pl.txt");
while (<DATA>) {
        chomp;
        my @tab = split(/\|/, $_);
        my $record = join('|', $tab[0], $tab[3], $tab[5], $tab[7]);
        print $record, "\n";
        print OUT $record, "\n";
}

close(OUT);


__DATA__
20180704150930|rtsp|645645643|30193|211|KLM|KLM00SD624817.ts|172.30.16.34|127299264|VERB|01780000|21103|277|server01|OK
20180704150931|api|456456546|30130|234|VC3|VC300179201139.ts|172.30.16.138|192271838|VERB|05540000|23404|414|server01|OK
20180704150931|api|465456786|30154|443|BAD|BAD004416550.ts|172.30.16.50|280212202|VERB|04740000|44301|18|server01|OK
20180704150931|api|5437863735|30157|383|VSS|VSS0011062009.ts|172.30.16.66|312727922|VERB|05700000|38303|381|server01|OK
20180704150931|api|3453432|30215|223|VAE|VAE00TF548197.ts|172.30.16.74|114127126|VERB|05060000|22305|35|server01|OK
20180704150931|api|312121|30044|487|BOV|BOVVAE00549424.ts|172.30.16.58|69139448|VERB|05300000|48708|131|server01|OK
20180704150931|rtsp|453432123|30127|203|GZD|GZD0900032066.ts|172.30.16.58|83164150|VERB|05460000|20303|793|server01|OK
20180704150932|api|12345348|30154|465|TYH|TYH0011224259.ts|172.30.16.50|279556843|VERB|04900000|46503|241|server01|OK
20180704150932|api|4343212312|30154|326|VAE|VAE00TF548637.ts|172.30.16.3|28966797|VERB|04740000|32601|969|server01|OK
20180704150932|api|312175665|64530|305|TTT|TTT000000011852.ts|172.30.16.98|47868183|VERB|04740000|30501|275|server01|OK
like image 202
Seth Avatar asked Jan 20 '26 14:01

Seth


1 Answers

You can't put a loop construct into a data structure in Perl. That would be a syntax error.

But you can use an array slice.

my $record = join('|', @tab[0, 3, 5, 7]);

Notice how the sigil changed from $ to @, because we now return a list. Of course you can put those four numbers into another array defined outside of the loop and use that.

my @indexes = ( 0, 3, 5 );
my @slice =  @array[ @indexes ];
like image 146
simbabque Avatar answered Jan 23 '26 05:01

simbabque



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!