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

open(FILE, "<", "file");

while(<FILE>) {
  chomp;
  push(@arr, $_);
}

close(FILE);

thanks for the reply.. but it dint work... do u have any further suggestions???

Why it didn't work ?

What is the expected output ?

Do you want to print the array elements ? If so, add the following snippet at the end

foreach(@arr) {
  print "array element: $_\n";
}

i dont know but.. this one worked for me...

open(FILE, "<", "tmpfile");
my @values = <FILE>;
close(FILE);

i just wanted to write the values of a file into @array... but still.. ur example helped me as a start point... appreciate it.. thanks again...

can you help me on this... i try to print them($count, $last_rec) in two different lines but they get printed in one line if i use this script.....

open (fh,">>","file");
print fh "$count";
print fh "$last_rec";
close(fh);

can you suggest any solution for this...thanks

Well, this is also one of the way to do that.

The other one I had posted should also work :slight_smile:

newline

print fh "$count" . "\n";
print fh "$last_rec" . "\n";

It worked!!... thanks a lot..

This is a bit of a nit-pick....

Try and use concatenation only when necessary, it is a slower process than just constructing an annonymous string:

print fh "$count\n";
print fh "$last_rec\n";

or print expects a list so using the comma operator is also a better option

print fh "$count", "\n";
print fh "$last_rec", "\n";

open(FH,"<file");
while(<FH>){
$_=~ tr/\n//d;
push(@arr, $_); 
}
close(FH);
print join("|",@arr);

That is a bad solution for such a simple problem. Use chomp instead of tr to remove end of line record seperators, it is way more efficient for that purpose. And using push() just slows down reading the file into an array, which perl does automatically when the filehandle is output in list context:

@array = <FH>;

summer_cherry,

ahhh...why are you digging up old threads and posting ancient perl code anyway? You dug up a thread from 2001 (not this one) and posted a reply, not even the correct one!

$\="\n";
open ( FH,">jas");
print FH '$count';
print FH '$last';
close(FH);

I guess if someone literally wants to print $count and $last to a file your code will work. :wink:

If u want to get the literally value of $count and $last, then use double codes (" ") instead of single codes (' ') in code.

Hi Meghana,

use a line-break "\n" in your print statements :wink:

~sudhir

Someone should lock this year old thread, for some reason it attracts attention.

Locking this thread would be a travesty.. Newbies like myself need these strings of information to help elevate us to a higher level of understanding of a lang seasoned programmers take for granted.

This is my first post to unix.com.. a true newbie.

++++++

From what I have read through this string I have been able to read in a file to an array and print it back to console.

open(FILE, "<", "dataFile");
my @dataValues=<FILE>;
close(FILE);
print("\n@dataValues");

What I would like to do now is take a data file

AAA
BBB
CCC
1
2
3

and read strings into @stringArray and read the int into @intArray. Should be simple.. just have not been able to figure it out..

/r
Rick

Here's one way to do it:

$
$ cat -n datafile
     1  AAA
     2  BBB
     3  CCC
     4  1
     5  2
     6  3
     7  xYz
     8
     9  456
    10  abc12de
    11  xyz~!lm.&
$
$
$ cat testarray.pl
#!perl -w
open(FILE, "<datafile");
while (<FILE>) {
  chomp;
  if ($_ =~ /^[[:alpha:]]+$/) {
    push @stringArray, $_;
  } elsif ($_ =~ /^\d+$/) {
    push @intArray, $_;
  }
}
close(FILE);
print("\nString Array ===> @stringArray");
print("\nInt Array    ===> @intArray");
print("\n");
$
$
$ perl testarray.pl
String Array ===> AAA BBB CCC xYz
Int Array    ===> 1 2 3 456
$
$

Note:

  • Due to the "+" quantifier, the regexes look for at least one character/digit. Hence line no. 8 was left out.
  • The match is case-insensitive e.g. line no. 7
  • The [:alpha:] POSIX character class does not match alphanumeric and special characters due to which lines 10 and 11 were left out.

Hope that helps,
tyler_durden