CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Basic comparison problem

 



aixrus
New User

May 21, 2010, 10:30 AM

Post #1 of 3 (685 views)
Basic comparison problem Can't Post

Goal: read all lines of a file, colon delimited, searching for a specific server name (always located in the first field). If the current server equals the servername in that 1st field, then populate 2 other variables based on 2nd and 3rd fields.

On an aix system.

When I try to compare strings using eq, it always evaluates true.

When I try with == it always evaluates false.

Where system hostname = x100

/inputfile reads:

x100:KRB Master:NIM
x101:Some other stuff:Cognos, WAS




$host=`hostname`;
# Read the Description File
$desc_file="/inputfile";
open(INP, $desc_file) || die("Could not find $desc_file ! Aborting!");

$text = <INP>;
while ($text){

($ahost)=split(/\:/,$text);

if ( $host eq $ahost ){
($ahost,$desc,$apps)=split(/\:/,$text);
}


$text = <INP>;
}


Thanks ahead for help!


shawnhcorey
Enthusiast


May 21, 2010, 11:46 AM

Post #2 of 3 (681 views)
Re: [aixrus] Basic comparison problem [In reply to] Can't Post

Try:

Code
#!/usr/bin/perl 

use strict;
use warnings;

chomp( my $host = `hostname` );

# Read the Description File
my $desc_file = "/inputfile";
open my $input_fh, '<', $desc_file or die "Could not open $desc_file: $!\n";

while( my $text = <$input_fh> ){
my ( $ahost, $desc, $apps ) = split /\:/, $text;
if( $host eq $ahost ){
# Do something with $ahost, $desc and $apps
}
}

close $input_fh or die "Could not close $desc_file: $!\n";


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


aixrus
New User

May 24, 2010, 11:58 AM

Post #3 of 3 (658 views)
Re: [shawnhcorey] Basic comparison problem [In reply to] Can't Post

Thanks!

I tried the first change you made, which was the chomp... and it worked!

All I needed to do was chomp $host, even though the var looked ok, it had the CR in it, thus causing the comparison to be strange.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives