Array access problems

G

Go Perl

The below is my code and i would like to access the last element of
the array
maer_array.
The MAER_FILE looks like the following
ITR Max_error Avg_error Min_error
1 0.849197022974602 0.845806355744732 0.843444019245762
2 0.844939386865682 0.841991065818245 0.837942476416068
3 0.844144961481595 0.841099932356234 0.836919185707587
4 0.844144961481595 0.841099932356234 0.836919185707587
while($maer_line=<MAER_FILE>) {
chop($maer_line);
@maer_array=split(/\t/, $maer_line);
if($maer_array[0] != "ITR") {
#print "$maer_array[3]\n";
}
}

I am having problems accessing the last element and how can i compare
the last element of the 4th column with every element of 4th column
of this file which looks like
ITR CHR SUM Error Error(%)
0 1 46.7058362636031 0.849197022974602 84.9197022974602
0 2 47.0231420611022 0.854966219292767 85.4966219292767
0 3 46.4794258296669 0.845080469630308 84.5080469630308
0 4 46.6549049902152 0.848270999822095 84.8270999822095
0 5 46.8269040494763 0.851398255445024 85.1398255445024
0 6 46.9823066656241 0.854223757556801 85.4223757556801
0 7 46.3894210585169 0.843444019245762 84.3444019245762
0 8 46.4279728814877 0.844144961481595 84.4144961481595
0 9 46.4266047910362 0.844120087109749 84.4120087109749

I am making a mistake somewhere in the storing of arrays and i am not
able to acces the elements of arrays in the manner i want. If anyone
can shed some light on this that would be very helpful.

Regards,
GP
 
J

James E Keenan

Go Perl said:
The below is my code and i would like to access the last element of
the array
maer_array.
The MAER_FILE looks like the following
ITR Max_error Avg_error Min_error
1 0.849197022974602 0.845806355744732 0.843444019245762
2 0.844939386865682 0.841991065818245 0.837942476416068
3 0.844144961481595 0.841099932356234 0.836919185707587
4 0.844144961481595 0.841099932356234 0.836919185707587
while($maer_line=<MAER_FILE>) {
chop($maer_line);
@maer_array=split(/\t/, $maer_line);
if($maer_array[0] != "ITR") {
#print "$maer_array[3]\n";
}
}
1. Consult documentation (perldoc -f chomp perldoc -f chop) on why you
should use 'chomp' rather than 'chop'.
2. 'ne' is string non-equality operator; '!=' is numeric non-equality
operator.
3. Last element of an array carries index -1.

while (<DATA>) {
chomp;
my @maer_array = split(/\t/);
print "$maer_array[-1]\n" unless ($maer_array[0] eq "ITR");
}

__DATA__
ITR Max_error Avg_error Min_error
1 0.849197022974602 0.845806355744732 0.843444019245762
2 0.844939386865682 0.841991065818245 0.837942476416068
3 0.844144961481595 0.841099932356234 0.836919185707587
4 0.844144961481595 0.841099932356234 0.836919185707587


I am having problems accessing the last element and how can i compare
the last element of the 4th column with every element of 4th column
of this file which looks like


Not quite sure what you mean by "last element of 4th" versus "every element
of 4th". But in any case you can use the -1 index to access the last
element.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,129
Messages
2,570,770
Members
47,329
Latest member
FidelRauch

Latest Threads

Top