Last number in the array --- access problem --Newbie question

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

  1. #1

    Default 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
    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
    5 0.840596405164578 0.838883453436171 0.836919185707587
    6 0.839512137681004 0.838072896555497 0.836534486876684

    I have to get the last number of the 4th column which is
    0.836534486876684 and so i use the following code.
    #------
    open (MAER_FILE, "MAER_output.txt");
    while (<MAER_FILE>) {
    chomp;
    my @maer_array = split(/\t/);
    print "$maer_array[-1]\n";
    }
    #------

    But this gives me the output as the entire 4th column instead of just
    the last number.
    once i get the last number which is 0.836534486876684 i have to search
    for this number in EVAL_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

    The last number in the previous line must match with one of the number
    in the 4 th column of the above file and i should be able to extract
    the entire line.
    I am not able to store these columns into arrays..either i don't know
    perl or arrays. Please help me solve this problem efficiently.
    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. how to determine number of files in directory? array question
      Hello, I have an asp script that lists the files in a directory: CurrentPATH = "c:\temp\" Set oFSO =...
    4. 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...
    5. array access problem - newbie question
      #-------------code----------# open (MAER_FILE, "MAER_output.txt"); while (<MAER_FILE>) { chomp; my @maer_array = split(/\t/); print...
  3. #2

    Default Re: Last number in the array --- access problem --Newbie question

    Go Perl wrote:
    > 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
    > 5 0.840596405164578 0.838883453436171 0.836919185707587
    > 6 0.839512137681004 0.838072896555497 0.836534486876684
    >
    > I have to get the last number of the 4th column which is
    > 0.836534486876684 and so i use the following code.
    > #------
    > open (MAER_FILE, "MAER_output.txt");
    > while (<MAER_FILE>) {
    > chomp;
    > my @maer_array = split(/\t/);
    > print "$maer_array[-1]\n";
    > }
    > #------
    As one should expect, this prints out the last columns for every row.
    From what I understand, you just want the last column for the last row?
    In that case, you should first get the last row, then the last column
    for that row;

    my @rows = <MAER_FILE>;
    my $last = $rows[-1];
    my @cols = split( /\s+/, $last );

    The code above is just an example of how you should think. The actual
    solution might differ a little; you should seriously think about how big
    the file you're reading is, as 'my @rows = <MAER_FILE>;' throws the
    whole file into memory.
    > once i get the last number which is 0.836534486876684 i have to search
    > for this number in EVAL_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
    >
    > The last number in the previous line must match with one of the number
    > in the 4 th column of the above file and i should be able to extract
    > the entire line.
    Hmm. I'm not quite sure if I understood that one. Do you mean that
    your 'Min_Error' number from the MAER_FILE should match with one of the
    'Error' numbers in the EVAL_FILE, and that the whole line with the match
    should be printed out?

    If that's the case:

    my $wanted = 0.0;
    open( MAER_FILE, 'maer.log' ) || die $! . "\n";
    while ( <MAER_FILE> ) {
    chomp;
    $wanted = ( split(/\s+/) )[-1];
    }
    close( MAER_FILE );

    open( EVAL_FILE, 'eval.log' ) || die $! . "\n";
    while ( <EVAL_FILE> ) {
    chomp;
    if ( m,$wanted, ) {
    print , "\n";
    }
    }
    close( EVAL_FILE );

    This is _totally_ untested, but you should get the idea. Feel free to
    optmize the code when you figure out that the above works for you. :-)


    --
    Tore Aursand <tore@extend.no>

    "I'm so old they've cancelled my blood type." -- Bob Hope

    Tore Aursand Guest

  4. #3

    Default Re: Last number in the array --- access problem --Newbie question

    Go Perl <puissant00@yahoo.com> wrote:
    > I have two files MAER_FILE and EVAL_FILE
    > MAER_FILE contains
    >
    > ITR Max_error Avg_error Min_error
    > 1 0.849197022974602 0.845806355744732 0.843444019245762
    > my @maer_array = split(/\t/);
    > But this gives me the output as the entire 4th column instead of just
    > the last number.

    I showed you how to solve that problem 3 days ago, so why are
    you asking it yet again?

    > which looks like
    > ITR CHR SUM Error Error(%)
    > 0 1 46.7058362636031 0.849197022974602 84.9197022974602
    > 0 2 47.0231420611022 0.854966219292767 85.4966219292767
    > I am not able to store these columns into arrays..

    Do it using split() like you did for the other file.


    --
    Tad McClellan SGML consulting
    [email]tadmc@augustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  5. #4

    Default Re: Last number in the array --- access problem --Newbie question

    [email]puissant00@yahoo.com[/email] (Go Perl) wrote:

    : I have two files MAER_FILE and EVAL_FILE
    : MAER_FILE contains
    :
    : ITR Max_error Avg_error Min_error
    : 1 0.849197022974602 0.845806355744732 0.843444019245762
    : 2 0.844939386865682 0.841991065818245 0.837942476416068
    [truncated]
    :
    : I have to get the last number of the 4th column which is
    : 0.836534486876684

    $want = (split /\t/, (grep 1+chomp, <MAER_FILE>)[-1])[-1];

    : once i get the last number which is 0.836534486876684 i have to search
    : for this number in EVAL_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
    [truncated]
    :
    : The last number in the previous line must match with one of the number
    : in the 4 th column of the above file and i should be able to extract
    : the entire line.

    ($line) = grep {chomp; $want == (split /\t/)[3]} <EVAL_FILE>;

    : I am not able to store these columns into arrays

    Is creating such a data structure a necessary part of the solution?

    : Please help me solve this problem efficiently.

    What is your gauge of efficiency?

    Jay Tilton Guest

  6. #5

    Default Re: Last number in the array --- access problem --Newbie question

    Tore Aursand <tore@aursand.no> wrote in comp.lang.perl.misc:

    [...]
    > From what I understand, you just want the last column for the last row?
    > In that case, you should first get the last row, then the last column
    > for that row;
    >
    > my @rows = <MAER_FILE>;
    > my $last = $rows[-1];
    > my @cols = split( /\s+/, $last );
    >
    > The code above is just an example of how you should think. The actual
    > solution might differ a little; you should seriously think about how big
    > the file you're reading is, as 'my @rows = <MAER_FILE>;' throws the
    > whole file into memory.
    The slurping can easily be avoided:

    my $last;
    $last = <MAER_FILE> until eof( MAER_FILE);

    Or use File::ReadBackwards from CPAN.

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