Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Why is 'while loop' not executing

Posted on 2011-02-17
4
Medium Priority
?
328 Views
Last Modified: 2012-06-27
Hey,

I'm writing a perl script where I read a conf file. The conf file is a csv file that has two columns of data. I want to read the first column and match to the host the script is running on and iterate through it's matches. It's clearer when you see the format of the csv file that is like this:

"hostA","host1"
"hostA","host2"
"hostA","host3"
...
"hostB","host1"
"hostB","host2"
"hostB","host3"
...
"hostC","host1"
"hostC","host2"
"hostC","host3"
...

Basically it's a simplified routing table. The first column contains a source host and the second column is the destination it's connecting to.

If the script happens to be on hostA, the loop will find the "to hosts" and print them. Well, really I dont want it to print, I will do another task with the output but for testing purposes, so I know it's working I want to print it. The correct output would be:

The 'to-hosts' for this machine are "host1"
The 'to-hosts' for this machine are "host2"
The 'to-hosts' for this machine are "host3"

But it's not executing the loop. I'm a super newbie to perl, in fact I've never a perl script before so it's learn as I go so I'm not sure why the loop is not executing. Can someone take a look at the code and see what is wrong with it?

Thanks,
Jose

open (F, $file) || die ("Could not open $file");

while ($line = <F>)
{
        ($field1,$field2) = split ',', $line;
        if ($field1 eq $host){
                print "The 'to-hosts' for this machine are $field2\n";
                }
}
close (F);

Open in new window

0
Comment
Question by:akatsuki27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
Fairlight2cx earned 2000 total points
ID: 34921721
I strongly suspect your loop is running.  I think you're getting no output because of the values of $field1 and $host.  Nowhere in the code snippet you included is $host defined.

My first step towards debugging would be to just insert a print("Inside while().\n"); statement first thing inside the while() loop's block.  I suspect you'll see one per line read.  If that's true, then the conditional is your issue.

Assuming that, I'd then swap out the "Inside while()." with "field1: $field1\nhost: $host\n" in that extra print statement I had you add, and move that line down to right under the split statement, but before the conditional.
0
 

Author Comment

by:akatsuki27
ID: 34921936
I figured it out. My conditional was failing because the two sides weren't equal in any of the tests. There were some quotes in one and not the other.

I added the line: $field1 =~ s/\"//g;

That solved it.

Btw Fairlight, I defined $host in the top of the script. But now that I have your attention, could I put that output into an array? Most likely yes. But do I need to do that outside of the while loop?
0
 
LVL 7

Assisted Solution

by:Fairlight2cx
Fairlight2cx earned 2000 total points
ID: 34923702
You can put it into an array inside the loop.  Just push() the values onto an array:

push(@arrayname,$field1);
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 34923994
Check what is the host name you are getting by printing it:
#!/usr/bin/perl
use Sys::Hostname;
$file = $ARGV[0];
$host = hostname;

open (F, $file) || die ("Could not open $file");

while ($line = <F>)
{
        ($field1,$field2) = split ',', $line;
        print "$field1, $field2, $host";
        if ($field1 eq $host)
        {
                print "The 'to-hosts' for this machine are $field2\n";
        }
}
close (F);

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question