the following script (test.pl) append $insert[1] text between $first_line[1] and $second_line[1] on myfile.txt file and send output to output.txt
but if I declare the array as
my $first_line[1]=")";
my $second_line[1]="NIC Hr_Nic (";
my $insert[1]="hello world
line 2
line3 "
I get
syntax error at ./test.pl line 10, near "$first_line["
syntax error at ./test.pl line 11, near "$second_line["
syntax error at ./test.pl line 12, near "$insert["
Execution of ./test.pl aborted due to compilation errors.
how to declare the follwoing arrays?
remark: (without the my on the array the script work fine)
lidia
#!/usr/bin/perl
# Slurp file myfile.txt into a single string
open(FILE,"myfile.txt") || die "Can't open file: $!";
undef $/;
my $file = <FILE>;
# Set strings to find and insert
my $count=1;
my $first_line[1]=")";
my $second_line[1]="NIC Hr_Nic (";
my $insert[1]="hello world
line 2
line 3 " ;
You should use
my @first_line = ();to declare a new empty array. You don’t have to give a size.But there are many, many things wrong with the code you posted. For instance, if you only ever have one element, why use an array at all?