Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

load file in a single line (hack) 1

Status
Not open for further replies.

133tcamel

Programmer
Sep 22, 2002
137
IN
Hi,
Is there a way to load a file into an array using a single line of pure perl code? I need something like this, but platform independent:
Code:
@data = `cat filename.ext` 
#the problem is cat doesn't run on windows, "type" doesn't run on unix
i can use open, but that is not i'm looking for because its at least 2 lines. I am asking this because i recently came across the "glob" where you can get entire directory listings using <.*>, etc, so mind started wondering if there is something similar for reading files. It'd be so cool to do something like this in perl.

if anybody knows this, please do reply. It doesn't need to be useful, i just want to learn more of these cool hacks in perl!

thanks,
san

---
cheers!
san
smoking3vc.gif


print length "The answer to life, universe & everything!
 
Code:
$/=undef;open FH,"<$inputfile";$data=<FH>;close (FH);@data=split /\n/,$data;

because it's terse, it doesn't make it right IMO, and I should have stored the value of $/ to restore it after the operation ;-)


Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
If you're running a script that does not require/use command line arguments, there are two ways to do this (well, they're different ways of doing the same thing.)

1. Enter the file name(s) as an arguemnt(s): when you run a script, all the arguments initially are in the special @ARGV array. So, if you were to run your script like: readfile.pl somefile.txt you would end up with $ARGV[0] = somefile.txt. Which leads to:

2. Assign the file name(s) to @ARGV: alternately, you can assign the file names within your script, and accomplish the same thing. For example, the code:
Code:
@ARGV = <*.txt>;
would assign all the file names that the glob returns to @ARGV.

After you get done populating the @ARGV array with your file names, you can use the the null filehandle (take a look at perldoc perlop for more info.) It will take care of the file opening and closing for you.

So, to read a file into an array with a single line, you could use:
Code:
push(@ARGV, 'somefile.txt') && (my @data = <>);
 
Hello Folks,
Thanks for all the replies.

@Paul, that definitely looks like the righteous code but not the cool one liner I am looking for ;)

@rharsh, i like this @ARGV idea and I did learn something new from it! :) I think this is simiar to perl -p -i -e 's[][]' filename.ext for the quick search/replace on terminal? Nice hack!

thanks again for sharing all your ideas!


---
cheers!
san
smoking3vc.gif


print length "The answer to life, universe & everything!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top