LinuxQuestions.org
Visit Jeremy's Blog.
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-28-2004, 01:26 AM   #1
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Spaces reading a file


Hello there,

Once again I'm at it with some perl stuff . I have a simple question I think (which I can't figure...argh) is easy. I have a file called crap.txt that consists of:

lisa
bart
homer

My script is:
----------------------------------------------
#!/usr/bin/perl

$file="/home/crap.txt";
open (FILE, $file) || die "Cannot open file $file: $!\n";

@array=<FILE>;
print "Contents of $file is: \n@array\n";
----------------------------------------------

However, it prints like so:
lisa
bart
homer

As you can see, there is an extra space for bart and homer but not for lisa. How do I eliminate this space and why does this occur? Thanks!

-twantrd
 
Old 09-28-2004, 05:36 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443

Rep: Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791
Looks ok there. Where are the extra spaces? Try using code tags around the code+output to make it clearer?
 
Old 09-28-2004, 11:31 AM   #3
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Original Poster
Rep: Reputation: 52
Oh crap, it formatted the text differently. Sorry, the output looks like this:

lisa
<1 spacebar>bart
<1 spacebar>homer

Hope that makes it understandable for you guys....let me know if I need to be more clearer. Thanks!

-twantrd
 
Old 09-28-2004, 12:22 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443

Rep: Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791
Ok, got it. The default in Perl if you print an entire array in one step, is to put spaces between each element, otherwise (for example) if you print the array (1,2,3,4,5,6), you'd get
123456 , prob not what you'd want...
If you loop through the array instead, you'll be fine.
 
Old 09-28-2004, 09:00 PM   #5
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Original Poster
Rep: Reputation: 52
That did it. Thanks

-twantrd
 
  


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
how to handle file names with spaces in them bahadur Programming 3 03-23-2005 05:15 AM
file name spaces problem hesher Linux - Software 2 03-06-2005 11:12 AM
du or wc and file names with spaces bramadams Slackware 2 01-27-2005 11:43 AM
Spaces in file names JohnKFT Slackware 3 11-09-2004 03:44 PM
File/Direcory Names with Spaces ar1 Linux - General 3 01-15-2004 10:41 AM

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

All times are GMT -5. The time now is 07:22 PM.

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