2

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.

7
  • 2
    You only ever push "" to @matchs Commented Sep 30, 2013 at 9:20
  • 3
    You are setting $community_matchs ="" and then pushing $community_matchs. This means your are pushing "". Maybe you need to do push (@matchs, $community) instead?? Commented Sep 30, 2013 at 9:21
  • It's not clear what effect you want. Please would you explain what you want in @matchs, and the purpose of $community_matchs? Commented Sep 30, 2013 at 10:57
  • Show us the content of @communities, $community and $c. Commented Sep 30, 2013 at 11:08
  • Vorsprung - When I test push (@matchs, $community) I have the same result that $community_matchs ="" Commented Sep 30, 2013 at 11:12

2 Answers 2

1

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
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.