Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I iterate over an array with foreach without changing the address of the loop variable?
    primarykey
    data
    text
    <p>When I run a simple bit of code like this:</p> <pre><code>my @arr=(1..5); my $x; foreach $x (@arr) { $x+=10; } print "@arr"; </code></pre> <p>The result is "11 12 13 14 15" because $x "becomes" each element in the @arr array in the foreach. Well enough.</p> <p>But here's my thing... not so much a problem (the solution is easy, but inelegant, and I want my perl to be as elegant as possible).</p> <p>I wrote a tie module for dealing with COBOL data. It takes a copybook, parses the fields, and then attaches that to a scalar/string so that access to/from the tied hash will return/set values in the string. It works wonderfully.</p> <pre><code>my %h,$rec; my $cb=&lt;&lt;END; 01 CH-RECORD. 05 JOB-NUM PIC X. 05 FILLER PIC X(76). 05 REC-TYPE PIC X(2). END tie %h, 'COBOLDataTie',$cb,\$rec; #tie the hash to the record via the copybook </code></pre> <p>From there, I can move a COBOL record to $rec and access the COBOL fields with the %h hash.</p> <p>Again, this works perfectly. But the problem comes when I want to iterate over, say, an array of COBOL records. So if after the above code I had something akin to:</p> <pre><code>foreach $rec (@arr) { print "Job is ",$h{'JOB-NUM'},"\n"; } </code></pre> <p>it won't work because the foreach actually changes the location of $rec, which breaks the tie on it. I end up having to do something like this:</p> <pre><code>foreach (@arr) { $rec=$_; print "Job is ",$h{'JOB-NUM'},"\n"; } </code></pre> <p>Is there any way I can do the "foreach $rec (@arr)" and not break my tied hash?</p> <p>(And before anyone says, yes I know this begs for a nice object-oriented solution... some day I'll get to that; I just have to find some time first)</p> <p>EPILOGUE: I revised the TieHash code to, instead of pointing to an external record, it intercepts "special" keys for the hash, among which is 'record'. So when I assign a record string to $h{'record'} it's the same as loading $rec in the example above. This is a far better solution, more self-contained. It also exposes a more OOP-like interface.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload