Write an array into a file in perl


















Your printf has problems. For a number it means to print it to three decimal places. I don't know what you're going for. With GPA I'm going to assume your intent was to print like 2. Yes, d for If you want to read this data back in again and separate the fields, a usual way of doing this is to separate the fields with tabs. Then you can split up each line on tabs when reading it back in. Putting that all together The next step to explore would be to restructure your data. Having data about each student split between multiple arrays makes it awkward to coordinate and pass around into subroutines.

Instead you'd use a hash. This is getting a little advanced, and I'm skimming a bit, but now you can get the name of the first student like so. This is not what you want to do. It will basically concatenate all arrays and print the first 3 elements. If all arrays have two elements you will get name1 name2 age1. If all arrays have 3 elemens name1 name2 name3.

There are several ways to write this code in perl. This is the c-like version. According to the original post: i want to write all the elements of an array to a file to generate a report. My impression could be wrong, in which case you are right trudge. Hopefully the OP will clarify. Facebook Like. Twitter Tweet. Be a part of the DaniWeb community. Sign Up — It's Free! Reply to this Topic.

This topic is old! By the way, you should technically always check print statements. Many people don't, but perl the Perl interpreter doesn't do this automatically and neither does autodie opens new window. To write a gzipped file, use the module IO::Compress::Gzip and create a filehandle by creating a new instance of IO::Compress::Gzip for the desired output file:.

To read from a gzipped file, use the module IO::Uncompress::Gunzip and then create a filehandle by creating a new instance of IO::Uncompress::Gunzip for the input file:.

This pragma changes the default mode of reading and writing text files, standard input, standard output, and standard error to UTF-8, which is typically what you want when writing new applications. However, it is important that you know what the encoding of your files is that you are dealing with and handle them accordingly.

Reasons that we should not ignore Unicode. Before reading and writing text files you should know what encoding to use. See the Perl Unicode Documentation for more details on encoding opens new window. Here we show the setting of UTF-8 as the default encoding and decoding for the function open. Note that the Perl print statements shown here are very similar to normal Perl print statements, except I precede the text that is printed with the Perl filehandle name named "FILE" in this example.

I also print two arrays to this output file in this example. FWIW, I create those arrays by reading from two input files, like this:. While this Perl write to file example may not seem like much, I use code like this display random ads in the footer section of "Java Warehouse" pages like this one.



0コメント

  • 1000 / 1000