Solved

Multiple line input into an array variable

Posted on 1998-12-03
4
162 Views
Last Modified: 2010-03-04
I'm trying to read in, from a file, multiple lines into an array variable... Lets call it @temp.  If I do @temp = <FILE>;  Than it seperates the array by newlines.  Is there a way to prevent this?  Or another way to read in the data that prevents this?  

Thanks...
0
Comment
Question by:willpd
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
b2pi earned 100 total points
ID: 1206715
The basic answer is to:

undef $/;

Then @temp = <FILE> will put all of <FILE> into $temp[0].  However, I wonder if you might want each the first line into $temp[0], the next into $temp[1], and so on.  If so, you could do:

undef $/;
open(FILE, "<$filename") || die "Unable to open $filename: $!";
@temp = split("\n", <FILE>);
close(FILE);

On the other hand, you could also do:
open(FILE, "<$filename") || die "Unable to open $filename: $!";
push(@temp, <FILE>);
close(FILE);

And the most basic of all is
open(FILE, "<$filename") || die "Unable to open $filename: $!";
@temp= <FILE>;
close(FILE);

On the other hand, maybe you really do want the entire file into zero'th element of the array....  (But, really, for maintainability's sake, you really ought say
$/=0;
open(FILE, "<$filename") || die "Unable to open $filename: $!";
$temp[0] = <FILE>;
close(FILE);



0
 
LVL 84

Expert Comment

by:ozo
ID: 1206716
The simplest way to prevent it would be not to do it.
But that begs the question of what you would prefer to be doing instead
( $/=0; seems rather less likely to be what was wanted than undef $/;)
0
 

Author Comment

by:willpd
ID: 1206717
Thanks, I think the undef $/; and then splitting will work for me!

Thanks again!
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1206718
No, if you're going to use that approach, my point was that leaving $/ alone, and using

open(FILE, "<$filename") || die "Unable to open $filename: $!";
@temp= <FILE>;
close(FILE);


is more efficient (as well as clearest).

And yes, it was a brain cramp that made me type $/=0 (Anyone got an ace bandage?)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl strange behaviour 5 67
Add additional column to .csv using Perl. 8 128
iSeries PERL Scripts 7 146
perl: Cleaning meta tags using RegEX 12 60
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now