Sponsored Content
Top Forums Shell Programming and Scripting file content in an array PERL Post 302290073 by KevinADC on Saturday 21st of February 2009 03:22:42 PM
Old 02-21-2009
You should really do your own school work, but since you seem to have tried something I'll help you.

Code:
open(FILE,'path/to/your/file') or die "$!";
my $line = <FILE>;#because it is one line you read it into a scalar
close FILE;
my @array = split(//, $line);
foreach my $i (0..$#array) {
   print "$array[$i]\n";
}


Last edited by KevinADC; 02-21-2009 at 04:35 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

formating array file output using perl

Hello, I am trying to output the values in an array to a file. The output needs to be formated such that each array value is left jusified in a field 8 character spaces long. Also, no more than 6 fields on a line. For example: @array= 1..14; Needs to be output to the file like so: 1 ... (4 Replies)
Discussion started by: seismic_willy
4 Replies

2. Shell Programming and Scripting

Perl search and replace file content.

I am not sure if this is doable. I am trying to open and print the content of the file by replacing all instances fo perl to PERL . This is my code but it is giving me the number count instead of the actual lines with changes. open (PERLHISTORY, 'sample.txt') or die "The file sample.txt could... (3 Replies)
Discussion started by: jxh461
3 Replies

3. Shell Programming and Scripting

perl -write values in a file to @array in perl

Hi can anyone suggest me how to write a file containing values,... say 19 20 21 22 .. 40 to an array @array = (19, 20, ... 40) -- Thanks (27 Replies)
Discussion started by: meghana
27 Replies

4. Shell Programming and Scripting

Array in Perl - Detect several file to be in one array

Hi everyone I have one question about using array in perl. let say I have several log file in one folder.. example test1.log test2.log test3.log and the list goes on.. how to make an array for this file? It suppose to detect log file in the current directory and all the log file will... (3 Replies)
Discussion started by: sayachop
3 Replies

5. Shell Programming and Scripting

Store content from array to Spread_sheet using perl

How to store the content from array to either "row-column" or "column-row" order? (0 Replies)
Discussion started by: kavi.mogu
0 Replies

6. Shell Programming and Scripting

perl extract content of file

I'm using Mail::Internet module, which will basically filter through email content and extract the body of the message my perl script to extract the body of the email #!/usr/bin/perl -w use Mail::Internet; @lines = <STDIN>; $mi_obj = new Mail::Internet(); ... (2 Replies)
Discussion started by: amlife
2 Replies

7. Shell Programming and Scripting

Print @array content to a file

Hi, as the title, I have an array @f_lines with gene information in it. How can I put the content of @f_lines into a file so that I can read it? I tried this: open(OUTPUT, "file"); # put gene information in this file; @f_lines = ("gene1", "gene2", "gene3"...); # gene information; print... (3 Replies)
Discussion started by: lyni2ULF
3 Replies

8. Shell Programming and Scripting

Changing file content in perl

Hi All, I have a file content like this. #<clear_category_list> #<include file="SUITE:Provision-FLEXPONDER_FAC.inc"> #<include file="CAT:SYSTEM:SAS_U23.inc"> #<include file="CAT:PRIORITY:10.inc"> #<include file="CAT:FAC_TYPE:OC48.inc"> #<include file="CAT:FAC_TYPE:OC192.inc">... (2 Replies)
Discussion started by: Syed Imran
2 Replies

9. Shell Programming and Scripting

Perl SCript to read file content (if else statemenet)

Hi All, I wanted to write a perl script to read the content in a file,the file content is either 0 (zero) OR 1. The idea is like this. If (content =1), then it will proceed to perform some step. and then update the file content to 0(zero) else if (content =0), it will update the content to... (11 Replies)
Discussion started by: hploh
11 Replies

10. Shell Programming and Scripting

Ksh: how compare content of a file with an other array

Hi, I created a skript in ksh which generate a file with semicolon as separator, this is an example of the file a created: example content file: hello;AAAA;2014-08-17 hello;BBBB;2014-08-17 hello;CCCC;2014-08-17 I would need to compare the content in of the second column of this file... (3 Replies)
Discussion started by: jmartin
3 Replies
FILE(3) 								 1								   FILE(3)

file - Reads entire file into an array

SYNOPSIS
array file (string $filename, [int $flags], [resource $context]) DESCRIPTION
Reads an entire file into an array. Note You can use file_get_contents(3) to return the contents of a file as a string. PARAMETERS
o $filename - Path to the file. Tip A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen(3) for more details on how to specify the filename. See the "Supported Protocols and Wrappers" for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide. o $flags - The optional parameter $flags can be one, or more, of the following constants: o FILE_USE_INCLUDE_PATH - Search for the file in the include_path. o FILE_IGNORE_NEW_LINES - Do not add newline at the end of each array element o FILE_SKIP_EMPTY_LINES - Skip empty lines o $context - A context resource created with the stream_context_create(3) function. Note Context support was added with PHP 5.0.0. For a description of contexts, refer to "Streams". RETURN VALUES
Returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached. Upon failure, file(3) returns FALSE. Note Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used, so you still need to use rtrim(3) if you do not want the line ending present. Note If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem. CHANGELOG
+--------+----------------------------+ |Version | | | | | | | Description | | | | +--------+----------------------------+ | 4.3.0 | | | | | | | file(3) became binary safe | | | | +--------+----------------------------+ EXAMPLES
Example #1 file(3) example <?php // Get a file into an array. In this example we'll go through HTTP to get // the HTML source of a URL. $lines = file('http://www.example.com/'); // Loop through our array, show HTML source as HTML source; and line numbers too. foreach ($lines as $line_num => $line) { echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br /> "; } // Another example, let's get a web page into a string. See also file_get_contents(). $html = implode('', file('http://www.example.com/')); // Using the optional flags parameter since PHP 5 $trimmed = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); ?> NOTES
Warning When using SSL, Microsoft IIS will violate the protocol by closing the connection without sending a close_notify indicator. PHP will report this as "SSL: Fatal Protocol Error" when you reach the end of the data. To work around this, the value of error_reporting should be lowered to a level that does not include warnings. PHP can detect buggy IIS server software when you open the stream using the https:// wrapper and will suppress the warning. When using fsockopen(3) to create an ssl:// socket, the developer is responsible for detecting and suppressing this warning. SEE ALSO
readfile(3), fopen(3), fsockopen(3), popen(3), file_get_contents(3), include(3), stream_context_create(3). PHP Documentation Group FILE(3)
All times are GMT -4. The time now is 05:00 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy