LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-17-2009, 06:35 PM   #1
OldGaf
Member
 
Registered: Feb 2008
Posts: 47

Rep: Reputation: 15
Unhappy Perl wonky IF outcome inside a @list foreach loop


Thanks for reading another post from a frustrated Perl noob

Sorry for the long post but I want to be as clear as possible.

setup to reproduce:

3 test files containing names:

File name --- File Content
---------- ------------------------------------
mailgroup1 - MARY BOB FRANK MYNAME JANE

mailgroup2 - SUSAN CHRIS WANDA

mailgroup3 - ANNE MYNAME DEBBIE JOHN MIKE FRANK


Code:
Quote:
1 #!/bin/perl
2
3 use strict;
4
5 system(clear);
6
7
8 my $user = "MYNAME";
9 my @groups = ('mailgroup1', 'mailgroup2','mailgroup3',);
10
11 foreach my $group (@groups) {
12 chomp $group;
13 my @group_list = `cat $group`;
14 print "$group\n";
15 foreach my $match (@group_list) {
16 chomp $match;
17 if ($match eq $user) {
18 print "*** $match ***\n\n";
19 } else {
20 print "$match\n\n";
21 }
22
23 }
24 }
25
26 print "$user <--- is what \$user returns.\n\n";
27

I want to print to the screen all members from the mailgroups with *'s surrounding MYNAME i.e. *** MYNAME ***

This does not happen

With the above I get this:

Quote:
mailgroup1
MARY BOB FRANK MYNAME JANE

mailgroup2
SUSAN CHRIS WANDA

mailgroup3
ANNE MYNAME DEBBIE JOHN MIKE FRANK

MYNAME <--- is what $user returns.
If I change line 17 to this:
17 if ($match = $user) {

I get this:
Quote:
mailgroup1
*** MYNAME ***

mailgroup2
*** MYNAME ***

mailgroup3
*** MYNAME ***

MYNAME <--- is what $user returns.
If I change line 17 to this:
17 if ($match =~ $user) {

I get this:
Quote:
mailgroup1
*** MARY BOB FRANK MYNAME JANE ***

mailgroup2
SUSAN CHRIS WANDA

mailgroup3
*** ANNE MYNAME DEBBIE JOHN MIKE FRANK ***

MYNAME <--- is what $user returns.
The output I am looking for is this:
Quote:
mailgroup1
MARY BOB FRANK *** MYNAME *** JANE

mailgroup2
SUSAN CHRIS WANDA

mailgroup3
ANNE *** MYNAME *** DEBBIE JOHN MIKE FRANK
What am I doing wrong? Why does eq not work?

Thanks,
-OG-
 
Old 09-17-2009, 08:16 PM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by OldGaf View Post
Thanks for reading another post from a frustrated Perl noob

Sorry for the long post but I want to be as clear as possible.

[ snip ]
Quote:
If I change line 17 to this:
17 if ($match = $user) {
But that is an assignment, not a comparison. You want:

Code:
if ($match == $user) {
a = b # assignment! b is copied to a
a == b # comparison! b is compared with a
 
Old 09-17-2009, 11:33 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,873

Rep: Reputation: 2600Reputation: 2600Reputation: 2600Reputation: 2600Reputation: 2600Reputation: 2600Reputation: 2600Reputation: 2600Reputation: 2600Reputation: 2600Reputation: 2600
Actually, he does want a string comparison.
Taking the data files as they appear (1 line each field space separated) I indented(!) the code and amended it. I get the required output
Code:
#!/usr/bin/perl -w
use strict;
my $user = "MYNAME";
my @groups = ('mailgroup1', 'mailgroup2','mailgroup3');


foreach my $group (@groups)
{
    my @group_list = split(/ /, `cat $group`);
    chomp(@group_list);

    print "$group\n";

    foreach my $match (@group_list)
    {
        if ($match eq $user)
        {
            print "*** $match *** ";
        }
        else
        {
            print "$match ";
        }
    }
    print "\n\n";
}

print "\n\n$user <--- is what \$user returns.\n\n";
note also the -w switch; highly recommended
 
Old 09-18-2009, 10:21 AM   #4
OldGaf
Member
 
Registered: Feb 2008
Posts: 47

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by chrism01 View Post
Actually, he does want a string comparison.
Taking the data files as they appear (1 line each field space separated) I indented(!) the code and amended it. I get the required output
Code:
#!/usr/bin/perl -w
use strict;
my $user = "MYNAME";
my @groups = ('mailgroup1', 'mailgroup2','mailgroup3');


foreach my $group (@groups)
{
    my @group_list = split(/ /, `cat $group`);
    chomp(@group_list);

    print "$group\n";

    foreach my $match (@group_list)
    {
        if ($match eq $user)
        {
            print "*** $match *** ";
        }
        else
        {
            print "$match ";
        }
    }
    print "\n\n";
}

print "\n\n$user <--- is what \$user returns.\n\n";
note also the -w switch; highly recommended
Most excellent thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl - foreach loop reading only the last $scalar in a @list OldGaf Programming 3 08-08-2009 09:57 AM
Perl: How to do a foreach for elements in a database? donnied Programming 5 07-17-2009 02:07 PM
PERL - foreach array not clearing michael.barnes Programming 8 08-21-2007 09:37 PM
Perl: Where am I in a foreach loop? jrtayloriv Programming 3 01-30-2005 10:43 PM
perl:foreach katana Programming 3 07-24-2001 01:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration