array access problem - newbie question

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default array access problem - newbie question

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

  2. Similar Questions and Discussions

    1. Byte array to string and back - newbie question
      Hi, I am trying to implement DES algorithm as described in the Microsoft article at...
    2. newbie array question
      Hi All Im new to php and are getting a bit confused about the sybase_fetch_array function (i think that this is the same as mysql_fetch_array?). ...
    3. newbie question. how to assign an array to a hash table?
      Hi, I have something like: $value = 'A' 'C' 'G'; and I would like store this value on a hash table. my @value = split(' ',$value); print...
    4. Last number in the array --- access problem --Newbie question
      Here is the complete problem: I have two files MAER_FILE and EVAL_FILE MAER_FILE contains ITR Max_error Avg_error Min_error...
    5. Access/ASP newbie problem!
      I've set up an Access db with the following =tables & fields; Categories; catID CatName CatPic SUbCategory subCatID catID subCatName
  3. #2

    Default Re: array access problem - newbie question

    Go Perl wrote:

    (snipped)
    > while (<MAER_FILE>)
    > chomp;
    No need for chomp.

    > my @maer_array = split(/\t/);
    > print "$maer_array[-1]\n";
    > 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.
    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
    Purl Gurl Guest

  4. #3

    Default Re: array access problem - newbie question

    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 [url]http://search.cpan.org/author/URI/File-ReadBackwards-1.00/ReadBackwards.pm[/url]

    --
    Hope this helps,
    Steven

    Steven Kuo Guest

  5. #4

    Default Re: array access problem - newbie question

    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 [email]jaspax@u.washington.edu[/email]
    [url]http://students.washington.edu/jaspax/[/url]
    [url]http://students.washington.edu/jaspax/blog[/url]

    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 Guest

  6. #5

    Default Re: array access problem - newbie question

    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 Guest

  7. #6

    Default Re: array access problem - newbie question

    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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139