UPDATE I'll state the problem I'm trying to solve, which I didn't do originally. I thought the added detail would just clutter the post, but since I'm still stuck here goes.

I have a large array of arrays, the original data. When going through that data, serially, at each row I need to check back over the previous 15 rows to see if a value exists in a specified field of those previous rows. Call it a look-back. The value I'm looking for can change based on the values in the current row, so this has to be evaulated with every original data row.

I decided to populate a look-back array with rows from the original data. This would be the 15 previous rows. The flow concept goes like this:

run through the original data via foreach. For every original data row :
while there are less than 15 rows in the look-back array, push each original data row onto the look-back array
after there are 15 rows in the look-back array, scan the look-back array for the specified value,
then shift the first row off of the look-back array and push the current original data row onto the look-back array
repeat until the end of the original data

The look-back array would thus always have the previous 15 rows, based on the current original data row. Is this flow concept so far off that it can't be done ?

I had considered setting up a while loop with a counter to go through the original data, and using that counter to look back over the last 15 rows. That seems awful clumsy to me, but maybe in this case ! TMTOWTDI...

Original Post

I've got an array of arrays from which I can't access single elements. Seems to me I've used this syntax before, but apparently my neurons are misfiring because it's not working the way I think it should.

When trying to retrieve one element of the array of arrays using the form

$OneElement = $Records[ 1 ][ 2 ];

I get an error that I can't use a string as an Array ref with strict, and it lists the entire row from the array as the string. It appears that Perl is only using the first index, and returning the entire row. I'm sure this is what I'm telling Perl to do, I just can't see how I'm telling it to do that.

Here's a demo code which reproduces the error message. I've commented out the $OneElement assignment so the rest runs. With the last two statements, commenting out the first one results in the same error from the second print statement - only the line number is different.

#!C:\Perl\bin use diagnostics; use strict; splice( my @Records ); splice( my @RecordRowFields ); splice( my @RecCheck ); my $RecordRow = ""; my $OneElement = ""; push( @Records, join ',', ( "AAA", 20130610, 730, 1015, "\n" ) ); push( @Records, join ',', ( "BBB", 20130610, 1015, 1200, "\n" ) ); push( @Records, join ',', ( "CCC", 20130610, 1230, 1400, "\n" ) ); push( @Records, join ',', ( "DDD", 20130610, 1415, 1530, "\n" ) ); #$OneElement = $Records[ 1 ][ 2 ]; foreach $RecordRow( @Records ) { @RecordRowFields = split /,/, $RecordRow; push( @RecCheck, join ',', @RecordRowFields ); } print "Array Records\n"; print @Records; print "\n\n"; print "Array RecCheck\n"; print @RecCheck; print "\n\n"; print "$Records[ 1 ][ 3 ]\n"; print "$RecCheck[ 1 ][ 3 ]\n";
Anyone seen this (newbie mistake) before ?

Dyslexics Untie !!!

In reply to element of an array of arrays by JockoHelios

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.