Possible Duplicate:
Easiest way to open a text file and read it into an array with Perl
I’m new to Perl and want for each file push the contents of that file in one separate array, I managed to do so by the following, which uses if statements. But, I want something like $1 for my arrays. Is that possible?
#!/usr/bin/perl
use strict;
my @karray;
my @sarray;
my @testarr = (@sarray,@karray);
my $stemplate = "foo.txt";
my $ktemplate = "bar.txt";
sub pushf2a {
open(IN, "<$_[0]") || die;
while (<IN>) {
if ($_[0] eq $stemplate) {
push (@sarray,$_);
} else {
push (@karray,$_);
}
}
close(IN) || die $!;
}
&pushf2a($stemplate,@sarray);
&pushf2a($ktemplate,@karray);
print sort @sarray;
print sort @karray;
I want something like this:
#!/bin/sh
myfoo=(@s,@k)
barf() {
pushtoarray $1
}
barf @s
barf @k
If you are going to slurp a file, use File::Slurp: