EBK has asked for the wisdom of the Perl Monks concerning the following question:

Guys, I have an array declared this way.
while ( <FB> ){ chomp; my($col0, $col1, $col2, $col3) = split ","; push @B, [$col0, $col1, $col2, $col3]; }
I want the last value from $col0. I have tried all these examples but I could not get. https://www.perl.com/article/6/2013/3/28/Find-the-index-of-the-last-element-in-an-array/. File
file706,010390010010,doc_01,paragraph,11 file707,010390010010,doc_01,paragraph,12 file708,010390010010,doc_01,paragraph,09 file710,010390010010,doc_01,paragraph,10 file711,010390010010,doc_01,paragraph,13 file712,010390010010,doc_01,paragraph,11 file713,010390010010,doc_01,paragraph,12 file714,010390010010,doc_01,paragraph,09 file715,010390010010,doc_01,paragraph,10 file716,010390010010,doc_01,paragraph,13 file717,010390010010,doc_01,paragraph,11 file718,010390010010,doc_01,paragraph,12 file719,010390010010,doc_01,paragraph,09 file720,010390010010,doc_01,paragraph,10 file721,010390010010,doc_01,paragraph,11 file722,010390010010,doc_01,paragraph,12 file723,010390010010,doc_01,paragraph,11 file725,010390010010,doc_01,paragraph,12 file726,010390010010,doc_01,paragraph,11 file727,010390010010,doc_01,paragraph,12 file728,010390010010,doc_01,paragraph,11 file729,010390010010,doc_01,paragraph,12 file730,010390010010,doc_01,paragraph,12
I want this value: file730

Replies are listed 'Best First'.
Re: Last position of array
by stevieb (Canon) on May 30, 2018 at 13:25 UTC

    I did not follow the link, so I don't know what you've tried. What you are creating there is an array of array references (AoA). To access the "fileNNN" part, you have to first select the element you want of the top level array (in this case the last element, aka -1), then you select the element of the second level array (the first element, ie. 0):

    use warnings; use strict; my @stash; while (<DATA>){ chomp; my($col0, $col1, $col2, $col3) = split ","; push @stash, [$col0, $col1, $col2, $col3]; } print "$stash[-1]->[0]\n"; __DATA__ file706,010390010010,doc_01,paragraph,11 file707,010390010010,doc_01,paragraph,12 file708,010390010010,doc_01,paragraph,09 file710,010390010010,doc_01,paragraph,10 file711,010390010010,doc_01,paragraph,13 file712,010390010010,doc_01,paragraph,11 file713,010390010010,doc_01,paragraph,12 file714,010390010010,doc_01,paragraph,09 file715,010390010010,doc_01,paragraph,10 file716,010390010010,doc_01,paragraph,13 file717,010390010010,doc_01,paragraph,11 file718,010390010010,doc_01,paragraph,12 file719,010390010010,doc_01,paragraph,09 file720,010390010010,doc_01,paragraph,10 file721,010390010010,doc_01,paragraph,11 file722,010390010010,doc_01,paragraph,12 file723,010390010010,doc_01,paragraph,11 file725,010390010010,doc_01,paragraph,12 file726,010390010010,doc_01,paragraph,11 file727,010390010010,doc_01,paragraph,12 file728,010390010010,doc_01,paragraph,11 file729,010390010010,doc_01,paragraph,12 file730,010390010010,doc_01,paragraph,12

    Output:

    file730

    Update: Note that you can shorten your code quite a bit while at the same time eliminating unnecessary variable assignments by expanding split inside of the aref you're pushing. This is of course if you aren't using those individual variables elsewhere in the code:

    while (<DATA>){ chomp; push @stash, [split /,/]; }
      Yes, it's worked. I understand your point but the way I have declared is useful for me because I know which information there is in each field. It is more for identification.
Re: Last position of array
by hippo (Archbishop) on May 30, 2018 at 13:25 UTC

    SSCCE:

    use strict; use warnings; use Test::More tests => 1; my @B = (); while (<DATA>) { chomp; my($col0, $col1, $col2, $col3) = split ","; push @B, [$col0, $col1, $col2, $col3]; } is ($B[-1][0], 'file730'); __DATA__ file706,010390010010,doc_01,paragraph,11 file707,010390010010,doc_01,paragraph,12 file708,010390010010,doc_01,paragraph,09 file710,010390010010,doc_01,paragraph,10 file711,010390010010,doc_01,paragraph,13 file712,010390010010,doc_01,paragraph,11 file713,010390010010,doc_01,paragraph,12 file714,010390010010,doc_01,paragraph,09 file715,010390010010,doc_01,paragraph,10 file716,010390010010,doc_01,paragraph,13 file717,010390010010,doc_01,paragraph,11 file718,010390010010,doc_01,paragraph,12 file719,010390010010,doc_01,paragraph,09 file720,010390010010,doc_01,paragraph,10 file721,010390010010,doc_01,paragraph,11 file722,010390010010,doc_01,paragraph,12 file723,010390010010,doc_01,paragraph,11 file725,010390010010,doc_01,paragraph,12 file726,010390010010,doc_01,paragraph,11 file727,010390010010,doc_01,paragraph,12 file728,010390010010,doc_01,paragraph,11 file729,010390010010,doc_01,paragraph,12 file730,010390010010,doc_01,paragraph,12
Re: Last position of array
by anonymized user 468275 (Curate) on May 30, 2018 at 14:13 UTC
    The most normal way to get the position of the last entry in an array is via $#arrayname, so:
    print $B[$#B][0] . "\n";

    One world, one people

Re: Last position of array
by karthiknix (Sexton) on May 31, 2018 at 08:36 UTC

    Below is a code which could print the last word or the word you wanted to be printed.

    open(FH, "input.txt"); while(<FH>){ push (@after_split, split(/\n/)); } push (@final_output, split(/,/,$after_split[scalar @after_split - 1])) +; print "Words you wanted\t".$final_output[0]."\n";
Re: Last position of array
by Anonymous Monk on May 31, 2018 at 13:07 UTC
    You have four primitives that treat an array like a push-down stack or a queue: push/pop, shift/unshift.