array access problem - newbie question

G

Go Perl

#-------------code----------#
open (MAER_FILE, "MAER_output.txt");
while (<MAER_FILE>) {
chomp;
my @maer_array = split(/\t/);
print "$maer_array[-1]\n";
}
#----------------------------#

#-----Output----#

0.843444019245762
0.837942476416068
0.836919185707587
0.836919185707587
0.836919185707587
0.836534486876684

#------------------#
Where as i just need the output as 0.836534486876684 which is the last
number of the third column. Can anyone please guide me here.
Thanks in advance,
GP
 
S

Steven Kuo

Go Perl wrote:

(snipped)


You previously displayed space delimited entries which
is confirmed by your split match. No need for an array.

Purl Gurl
--

#!perl

while (<DATA>)
{
<DATA>;
if (EOF)
{
$last_line = <DATA>;
print substr ($last_line, rindex ($last_line, " ") + 1);
}
}

__DATA__
one two three
four five six
seven eight nine


PRINTED RESULTS:
________________

nine




You're throwing away to many lines. Check the results when __DATA__
contains four lines:

__DATA__
one two three
four five six
foo bar baz
seven eight nine

PRINTED RESULTS:
_______________
baz


To the OP,

You can either

(1) Loop through the file and save the last line:

my $last_line;
$last_line = $_ while (<DATA>);
print "Parsing last line : $last_line\n";
# do stuff with $last_line

Or (2) loop through the file and check for eof:

while (<DATA>) {
if (eof DATA) {
print "Parsing last line : $_\n";
# do stuff with $_
}
}

Or (3) read the file backwards and process the first line.

use File::ReadBackwards

See http://search.cpan.org/author/URI/File-ReadBackwards-1.00/ReadBackwards.pm
 
J

JS Bangs

Go Perl sikyal:
#-------------code----------#
open (MAER_FILE, "MAER_output.txt");
while (<MAER_FILE>) {
chomp;
my @maer_array = split(/\t/);
print "$maer_array[-1]\n";
}
#----------------------------#

#-----Output----#

0.843444019245762
0.837942476416068
0.836919185707587
0.836919185707587
0.836919185707587
0.836534486876684

#------------------#
Where as i just need the output as 0.836534486876684 which is the last
number of the third column. Can anyone please guide me here.
Thanks in advance,
GP

No one can tell you anything until you show us what the data your working
from is. Your code doesn't have any compilation problems, so there must be
a logic problem, which we can't work out unless we get the data in
MAER_FILE.


--
Jesse S. Bangs (e-mail address removed)
http://students.washington.edu/jaspax/
http://students.washington.edu/jaspax/blog

Jesus asked them, "Who do you say that I am?"

And they answered, "You are the eschatological manifestation of the ground
of our being, the kerygma in which we find the ultimate meaning of our
interpersonal relationship."

And Jesus said, "What?"
 
B

Benjamin Goldberg

Purl Gurl wrote:
[snip]
while (<DATA>)
{

While we can read a line and stuff it into $_, and it's not undef...

Read another line and throw it away.

Check whether the constant string "EOF" is true. (Which it always
is.)
{
$last_line = <DATA>;

Now read another line, and store it into $last_line.
print substr ($last_line, rindex ($last_line, " ") + 1);

This, at least, does the right thing (if $last_line happens to be the
correct line, of course).
}
}

__DATA__
one two three
four five six
seven eight nine

PRINTED RESULTS:
________________

nine

What happens if your data contains more than three lines?
 
B

Benjamin Goldberg

Go Perl wrote:
[snip]
Where as i just need the output as 0.836534486876684 which is the last
number of the third column. Can anyone please guide me here.
Thanks in advance,
GP

To get the last number of the third column, try:

open (MAER_FILE, 'MAER_output.txt')
or die "could not open 'MAER_output.txt' $!";
my $lastline;
$lastline = $_ while <MAER_FILE>;
print ((split ' ', $lastline)[2]), "\n";
__END__
 

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
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top