LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 06-15-2007, 06:18 PM   #1
Elukyz
LQ Newbie
 
Registered: Jun 2006
Distribution: Slackware 11.0
Posts: 11

Rep: Reputation: 0
using variables as strings in perl


Hello, i've recently begun learning perl and have came to an issue is where when I use a variable a new line is printed right after it...Example...

Code:

Quote:
#!/usr/bin/perl -w

#==========================================================
# Last updated: June 11, 2007
# NOTE TO SELF: ALL code blocks finish with ;
#==========================================================

######## Variables #######
my $myname = `whoami`;

####### Start of main script #######
print "Hello $myname, how are you doing today?\n";
Output from that code is as follows...
Hello Ike
, how are you doing today?

However, if I add an escape character before variable the string prints out as expected....

Quote:
#!/usr/bin/perl -w

#==========================================================
# Last updated: June 11, 2007
# NOTE TO SELF: ALL code blocks finish with ;
#==========================================================

#>>>>>>> Variables <<<<<<<
my $myname = `whoami`;

#>>>>>>> Start of main script <<<<<<<
print "Hello \$myname, how are you doing today?\n";
output:
Name "main::myname" used only once: possible typo at ./myperl.pl line 9.
Hello $myname, how are you doing today?
-end of output-

Can you help me find what it is that I am missing?

EDIT:
Left out the resource I am using for learning perl ...
site name: http://perldoc.perl.org/

Last edited by Elukyz; 06-15-2007 at 06:27 PM.
 
Old 06-15-2007, 06:48 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
First, I don't think the string prints out as expected when you escape the dollar sign, does it?

I mean, you want the result of whoami to be printed, but what you're getting is the string:

Quote:
$myname
itself.

So get rid of that escape character before the dollar sign, because you want the content of variable $myname, not the variable name itself.

Well, you want most of the content, anyway.

The result of the backtick quotes is to execute the command and place the output in variable $myname. But the output includes a line feed at the end. So you want to get rid of that.

Try this. I just tried it myself and it works just fine:

Code:
#!/usr/bin/perl

$myname=`whoami`;
chomp($myname);
print "Hello, $myname, how are you doing today?\n"
(I put the comma after Hello because my mother was an English teacher, and she brought me up right.)

Hope this helps.
 
Old 06-15-2007, 06:49 PM   #3
rtspitz
Member
 
Registered: Jan 2005
Location: germany
Distribution: suse, opensuse, debian, others for testing
Posts: 307

Rep: Reputation: 33
the output of $myname = `whoami`; contains the username + \n at the end !
that's why a new line is started.

you can get rid of it with a substitition based on "regular expressions" like this:

Code:
#!/usr/bin/perl -w

#==========================================================
# Last updated: June 11, 2007
# NOTE TO SELF: ALL code blocks finish with ;
#==========================================================

######## Variables #######
my $myname = `whoami`;
$myname =~ s/(\n)$//;

####### Start of main script #######
print "Hello $myname, how are you doing today?\n";
Just google for perl and regex to find more on this or take a look at "learning pearl" published by O'Reilly.

what the "$myname =~...." does is this:

substitute the (\n) controll sequence at the very end (indicatd by $) of the initial string with "nothing".
 
Old 06-15-2007, 06:51 PM   #4
Elukyz
LQ Newbie
 
Registered: Jun 2006
Distribution: Slackware 11.0
Posts: 11

Original Poster
Rep: Reputation: 0
Isn't it funny how you stumble upon answers right after you ask a question....

Ok, so I found a function built into perl, chmop() that can do it for me. (URL: http://perldoc.perl.org/functions/chomp.html) However, is it possible for me to use it for all variables once defined?
 
Old 06-15-2007, 06:54 PM   #5
Elukyz
LQ Newbie
 
Registered: Jun 2006
Distribution: Slackware 11.0
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by rtspitz
the output of $myname = `whoami`; contains the username + \n at the end !
that's why a new line is started.

you can get rid of it with a substitition based on "regular expressions" like this:

Code:
#!/usr/bin/perl -w

#==========================================================
# Last updated: June 11, 2007
# NOTE TO SELF: ALL code blocks finish with ;
#==========================================================

######## Variables #######
my $myname = `whoami`;
$myname =~ s/(\n)$//;

####### Start of main script #######
print "Hello $myname, how are you doing today?\n";
Just google for perl and regex to find more on this or take a look at "learning pearl" published by O'Reilly.

what the "$myname =~...." does is this:

substitute the (\n) controll sequence at the very end (indicatd by $) of the initial string with "nothing".


Thanks rtspitz, I was typing my post when you must've posted yours. I will look into that feature as well.

Sorry, wjevans_7d1@yahoo.co , I was probably looking and typing when you were posting your post as well. Thank you for your input all.

Last edited by Elukyz; 06-15-2007 at 07:02 PM.
 
Old 06-15-2007, 07:22 PM   #6
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Quote:
Originally Posted by Elukyz
However, is it possible for me to use it for all variables once defined?
As long as said variable is a scalar string, yes. Otherwise, you can call chomp() on it, but there won't be any effect. (In other words, chomping an array or hash does not change the object, chomping undef does nothing; the same goes for chomping a scalar number.)
 
Old 06-16-2007, 11:31 AM   #7
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Quote:
chomping an array or hash does not change the object
I beg to differ. I ran this Perl script:

Code:
#!/usr/bin/perl

@the_array=("line_one\n","line_two\n","line_three\n");

$the_hash{"aaa"}="xxx\n";
$the_hash{"bbb"}="yyy\n";
$the_hash{"ccc"}="zzz\n";

print("'@the_array'\n");

print("'",%the_hash,"'\n");

print("We are now going to chomp() both.\n");

chomp(@the_array);

chomp(%the_hash);

print("'@the_array'\n");

print("'",%the_hash,"'\n");
... and got this output:

Code:
'line_one
 line_two
 line_three
'
'bbbyyy
aaaxxx
ccczzz
'
We are now going to chomp() both.
'line_one line_two line_three'
'bbbyyyaaaxxxccczzz'
 
Old 06-16-2007, 04:16 PM   #8
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
You're absolutely right. I'm sorry, I don't know what I was thinking.
 
Old 06-16-2007, 05:59 PM   #9
cramer
Member
 
Registered: Feb 2006
Distribution: Red Hat 9
Posts: 112

Rep: Reputation: 15
I would stick with the chomp() function instead of using regular expressions if all you want to remove is the new line character (/n).
 
Old 06-17-2007, 12:22 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Yeah, chomp() is safe and has Perl's DWIM (Do What I Mean) functionality that you'll find in a lot of Perl fns/operators.
Incidentally, chomping an array is handy if you want to process small files in mem, instead of reading from the disk & processing rec-by-rec. eg

open(FILE"<yourfile") or die "open failed: $!\n";
@file_recs = <FILE>;
close(FILE) or die "close failed: $!\n";
chomp(@file_recs);
 
  


Reply



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
Get strings from list using variables mwade Programming 5 10-28-2006 08:01 PM
Newbie: Ruby and Writing Variables In Strings lmcilwain Programming 4 10-18-2006 01:19 PM
C++ converting strings to variables dhbiker Programming 9 05-31-2004 04:06 AM
Perl>>working with strings format_c Programming 2 11-15-2001 04:21 PM
Using letters/strings to assign variables in python Colonel Panic Programming 0 09-14-2001 10:13 PM

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

All times are GMT -5. The time now is 09:21 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