file content in an array PERL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting file content in an array PERL
# 1  
Old 02-21-2009
file content in an array PERL

Hello everybody, I'm new in this forum.

I searched a long time for a solution for my problem but I didn't find the right thing.

I have to read from a file (content is "abngjm" without any other signs) and have to write this content in an array. But every sign has to be called by its own index. If I use "open file" and "print @array = FILE" I can write the signs only in correlation to one index ("$array[0]").

Has anyone an idea?

greetings
# 2  
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..
# 3  
Old 02-21-2009
using input record separator

Hi,

One more way to do this is to set the input record separator $/ to "";

$/ = "";
@array = <FILE>;

The variable "array" would contain your file characters.

~thanks
Sudhir
# 4  
Old 02-22-2009
Quote:
Originally Posted by sudhir_onweb
Hi,

One more way to do this is to set the input record separator $/ to "";

$/ = "";
@array = <FILE>;

The variable "array" would contain your file characters.

~thanks
Sudhir
No it won't. It will do the same thing that his script already does, read in the one line of data into the array and store it in $array[0].
# 5  
Old 02-22-2009
I have to say thank you for the answers!

KevinADC I think it works. I'll try it later because there is not enough time at the moment.

The solution mentioned by sudhir_onweb I tried for myself and the effect was the same I said before.

thanks
# 6  
Old 02-22-2009
I know my solution works. I read your question, I understood your question, I tested my code, then I posted a reply. The other guy simply did not understand the question, we are all guilty of that at times.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question