steba
steba

Reputation: 29

perl: refer to groups in foreach loop

Can I refer to groups in a foreach loop? my code below works:

@SEP_line=grep(/,\s(SEP[A-F0-9]*),\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}),\s(\+\d+),\s.*,\s(\w+)\n$/, @lines);

foreach (@SEP_line)
{
    /,\s(SEP[A-F0-9]*),\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}),\s(\+\d+),\s.*,\s(\w+)\n$/;

    print $1.",".$2.",".$3.",".$4."\n";
}

Since I already specified the match regex in @SEP_line definition, I'd do something like this:

@SEP_line=grep(/,\s(SEP[A-F0-9]*),\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}),\s(\+\d+),\s.*,\s(\w+)\n$/, @lines);

foreach (@SEP_line)
{
    print @SEP_line[$1].",".@SEP_line[$2].",".@SEP_line[$3]."\n";
}

This doesnt work. Thanks in advance

Upvotes: 1

Views: 131

Answers (2)

ikegami
ikegami

Reputation: 386541

Don't hand-parse and hand-generate CSV! It's simpler and cleaner to use a CSV parser.

use Text::CSV_XS qw( );

my $fh_in  = ...;
my $fh_out = \*STDOUT;

my $csv_in  = Text::CSV_XS->new({ binary => 1, auto_diag => 2, sep => ', ' });
my $csv_out = Text::CSV_XS->new({ binary => 1, auto_diag => 2 });

while ( my $row = $csv_in->getline($fh_in) ) {
    $csv_out->say($fh_out, [ @$row[-4,-3,-2,-1] ]
       if $row->[-4] =~ /^SEP[A-F0-9]*\z/
       && $row->[-3] =~ /^\d{1,3}(?:\.\d{1,3}){3}\z/
       && $row->[-2] =~ /^\+\d+\z/
       && $row->[-1] =~ /^\w+\z/;
}

Upvotes: 1

melpomene
melpomene

Reputation: 85867

Why loop twice (grep, foreach)? You can do it all in one loop:

foreach my $line (@lines)
{
    if ($line =~ /,\s(SEP[A-F0-9]*),\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}),\s(\+\d+),\s.*,\s(\w+)\n$/)
    {
        print "$1,$2,$3,$4\n";
    }
}

Upvotes: 4

Related Questions