Matarela
Matarela

Reputation: 37

Create and increment an array in Perl

I have a problem with my array and I need your help.

I want to create an array with the result given by my if, for comparison with an another array.

if (grep {$community eq $_ } @communities) {
   my $community_matchs = "";
   print "$community;$login;$last_at;;$size;1\n";
   push (@matchs, $community_matchs); 
   #my array
}
else{
   print "$community;$login;$last_at;;$size;0\n";
}

Then, later on

if (grep {$c ne $_} @matchs) {
   print "$community;3\n";

I am a beginner and French, so be understanding with me.

Upvotes: 2

Views: 484

Answers (2)

user1126070
user1126070

Reputation: 5069

You could use Data::Dumper for debugging.

use Data::Dumper;
print 'matchs:'.Dumper(\@matchs);

You are not adding anything to @matchs so it is going to be empty.

Maybe this what are you looking for:

if (my @community_matchs = grep {$community eq $_ } @communities) {
   print "$community;$login;$last_at;;$size;1\n";
   push (@matchs, @community_matchs); 
   #my array
}

Upvotes: 1

fugu
fugu

Reputation: 6578

Just an illustration of what I think you're trying to do, but I would consider re-writing as such:

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

my @communities = qw(community1 community2 community3 community4 community5 community6 community7 community8 community9);

my $community = 'community6';

my @matches;

foreach (@communities){
    if ($_ eq $community) {
        print "Match: $_\n";
        push (@matches, $_);
        # Do something else...
    }
    else {
        print "No match: $_\n";
        # Do something else...
    }
}

Outputs:

No match: community1
No match: community2
No match: community3
No match: community4
No match: community5
Match: community6
No match: community7
No match: community8
No match: community9

Upvotes: 0

Related Questions