For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2007 > print 2 valuable









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author print 2 valuable
cold1man@gmail.com

2007-08-04, 7:01 pm

I am new in perl. i want print 2 valuable in same line but my script
it print split into 2 line. if i hard code will work. i dont
understand why is happen. can some please explain to me thanks,

#!/usr/bin/perl
print "which client\n";
$a = <>;
$db = <>;
$dir="/u1/data/$a";
$avar="$dir/$db;
print "$avar";

Chas Owens

2007-08-04, 7:01 pm

On 8/4/07, cold1man@gmail.com <cold1man@gmail.com> wrote:
> I am new in perl. i want print 2 valuable in same line but my script
> it print split into 2 line. if i hard code will work. i dont
> understand why is happen. can some please explain to me thanks,
>
> #!/usr/bin/perl
> print "which client\n";
> $a = <>;
> $db = <>;
> $dir="/u1/data/$a";
> $avar="$dir/$db;
> print "$avar";


$a and $db both end with a "\n" (LF on unix, CRLF on windows). This
means that $avar has two "\n"s. This is why you are getting two
lines. If you want to get only one line you need to use the chomp
function to remove the "\n"s from the ends of your variables. Also,
do not use the variable $a. It and $b are special variables. They
are used by the sort function.

#!/usr/bin/perl

use strict;
use warnings;

my $adir = <>;
chomp($adir);

my $db = <>;
chomp($db);

my $dir = "/u1/data/$adir";
my $avar = "$dir/$db;

print "$avar\n";
John W. Krahn

2007-08-04, 7:01 pm

cold1man@gmail.com wrote:
> I am new in perl. i want print 2 valuable in same line but my script
> it print split into 2 line. if i hard code will work. i dont
> understand why is happen. can some please explain to me thanks,
>
> #!/usr/bin/perl
> print "which client\n";
> $a = <>;
> $db = <>;
> $dir="/u1/data/$a";
> $avar="$dir/$db;
> print "$avar";


When you use something like:

$a = <>;

you are getting the contents of $a from the command line and it will include a
newline at the end so when you see:

which client

and you type:

cold1man

your perl program assigns the string "cold1man\n" to the variable $a. You
need to chomp the variables first before you can use them:

#!/usr/bin/perl
use warnings;
use strict;
print "which client\n";
my $a = <>;
chomp $a;
my $db = <>;
chomp $db;
my $dir="/u1/data/$a";
my $avar="$dir/$db;
print "$avar";




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Sydney

2007-08-05, 4:01 am

On Aug 4, 2:45 pm, kra...@telus.net (John W. Krahn) wrote:
> cold1...@gmail.com wrote:
>
>
> When you use something like:
>
> $a = <>;
>
> you are getting the contents of $a from the command line and it will include a
> newline at the end so when you see:
>
> which client
>
> and you type:
>
> cold1man
>
> your perl program assigns the string "cold1man\n" to the variable $a. You
> need to chomp the variables first before you can use them:
>
> #!/usr/bin/perl
> use warnings;
> use strict;
> print "which client\n";
> my $a = <>;
> chomp $a;
> my $db = <>;
> chomp $db;
> my $dir="/u1/data/$a";
> my $avar="$dir/$db;
> print "$avar";
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you
> can special-order certain sorts of tools at low cost and
> in short order. -- Larry Wall


Thank it works :)

-LC

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com