Question about Perl Array

I never did a lot of programming - I was put to work with manipulating data from comment cards. Using perl so far, I got a database to correctly put my daily comments into an array. Comments represent each LINE of text in the database, so I just split the array into line breaks.

my @comments = split("\n", $c_data);

And yes, this was my first programming that helped me figure out for too long.

At this point, I now need to organize these array elements (is that what should I call them?) Into my own heading-based scalars (this is the behavior of a database that was corrupted at some point).

An example of how two elements of an array look:

print "$comments[0]\n";
This dining experience was GOOD blah blah blah.

or

print "$comments[1]\n";
Overall this was a BAD time and me and my blah blah.

"" "" "" , .

Perl ?

+3
6

, , . :

my @bad_comments = grep { /\bBAD\b/ } @comments;
my @good_comments = grep { /\bGOOD\b/ } @comments;

, "" "" .

, , join ( split):

my $bad_comments  = join "\n", grep { /\bBAD\b/ } @comments;
my $good_comments = join "\n", grep { /\bGOOD\b/ } @comments;
+3

-, . BAD-. , SO-SO? , @good, @bad, @soso, .

, :

#!/usr/bin/perl

use strict; use warnings;

use Regex::PreSuf;

my %comments;

my @types = qw( GOOD BAD ); # DRY
my $types_re = presuf @types;

while ( my $comment = <DATA> ) {
    chomp $comment;
    last unless $comment =~ /\S/;

    # capturing match in list context returns captured strings
    my ($type) = ( $comment =~ /($types_re)/ );
    push @{ $comments{$type} }, $comment;
}

for my $type ( @types ) {
    print "$type comments:\n";

    for my $comment ( @{ $comments{$type} } ) {
        print $comment, "\n";
    }
}

__DATA__
This dining experience was GOOD blah blah blah.
Overall this was a BAD time and me and my blah blah.
+3

, :

if ($comments[$i] =~ /GOOD/) {
    # good comment
}

if ($comments[$i] =~ /\b([A-Z]{2,})\b/) {
    print "Comment: $1\n";
}

\b ,() , [AZ] - , {2} , 2 , .

+1

-, . ( ), :

use strict;
use warnings;

my @comments = <DATA>;
chomp @comments;

my %data;
for (@comments) {
    my $cap;    
    for (split) {
        $cap = $_ if /^[A-Z]+$/;
    }
    if ($cap) { push @{ $data{$cap} }, $_ }
}
use Data::Dumper; print Dumper(\%data);

__DATA__
This is GOOD stuff
Here some BAD stuff.
More of the GOOD junk.
Nothing here.

:

$VAR1 = {
          'BAD' => [
                     'Here\ some BAD stuff.'
                   ],
          'GOOD' => [
                      'This is GOOD stuff',
                      'More of the GOOD junk.'
                    ]
        };
+1

, (SQLite?), .

.

, Perl DBI SQL SQLite Perl.

0

, "" " ".

, , ( toolic), :

my %CAPS = ();

map {
    my ($word) = /(\b[A-Z]+\b)/;
    push( @{ $CAPS{$word} }, $_)
} @comments;

, .

And you can refer to these lists as $ CAPS {'GOOD'} or $ CAPS {'BAD'}, or $ CAPS {whatever}.

0
source

Source: https://habr.com/ru/post/1726251/


All Articles