array access problem - newbie question

Discussion in 'Perl Misc' started by Go Perl, Aug 26, 2003.

  1. Go Perl

    Go Perl Guest

    #-------------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
    Go Perl, Aug 26, 2003
    #1
    1. Advertising

  2. Go Perl

    Steven Kuo Guest

    On Tue, 26 Aug 2003, Purl Gurl wrote:

    > Go Perl wrote:
    >
    > (snipped)
    >
    > > Where as i just need the output as 0.836534486876684 which is the last
    > > number of the third column.

    >
    > 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

    --
    Hope this helps,
    Steven
    Steven Kuo, Aug 26, 2003
    #2
    1. Advertising

  3. Go Perl

    JS Bangs Guest

    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
    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?"
    JS Bangs, Aug 26, 2003
    #3
  4. Purl Gurl wrote:
    [snip]
    > while (<DATA>)
    > {


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

    > <DATA>;


    Read another line and throw it away.

    > if (EOF)


    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?

    --
    $a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
    );{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
    ]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
    Benjamin Goldberg, Aug 27, 2003
    #4
  5. 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__

    --
    $a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
    );{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
    ]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
    Benjamin Goldberg, Aug 27, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    2
    Views:
    534
    Guest
    Feb 2, 2004
  2. Guest
    Replies:
    0
    Views:
    495
    Guest
    Feb 9, 2004
  3. Guest
    Replies:
    2
    Views:
    184
    Guest
    Feb 2, 2004
  4. Go Perl
    Replies:
    4
    Views:
    81
    Anno Siegel
    Sep 2, 2003
  5. Jerry C.
    Replies:
    8
    Views:
    208
    Uri Guttman
    Nov 23, 2003
Loading...

Share This Page