Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8806871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:14:04+00:00 2026-06-14T02:14:04+00:00

When I run a simple bit of code like this: my @arr=(1..5); my $x;

  • 0

When I run a simple bit of code like this:

my @arr=(1..5);
my $x;
foreach $x (@arr) {
    $x+=10;
}
print "@arr";

The result is “11 12 13 14 15” because $x “becomes” each element in the @arr array in the foreach. Well enough.

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).

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.

my %h,$rec;
my $cb=<<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

From there, I can move a COBOL record to $rec and access the COBOL fields with the %h hash.

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:

foreach $rec (@arr) {
    print "Job is ",$h{'JOB-NUM'},"\n";
}

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:

foreach (@arr) {
    $rec=$_;
    print "Job is ",$h{'JOB-NUM'},"\n";
}

Is there any way I can do the “foreach $rec (@arr)” and not break my tied hash?

(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)

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:14:05+00:00Added an answer on June 14, 2026 at 2:14 am

    It seems like the best way is to do something like:

    for (my $i=0;($rec=$arr[$i], $i<@arr);$i++) {
    

    Not exactly the elegance I was hoping for, but it seems to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run this simple code: from pylab import * import numpy as np
I have a simple bit of code as follows: foreach ($someFile in Get-ChildItem) {
I'm trying to run a simple query with PDO in PHP. The code is
I'm trying to run a simple query with $this->db in Kohana, but am running
This is a bit complex. I've created an array (NSArray) called pointsOfInterest. The elements
Trying to run a very simple flot.js example, I have the following code to
I have a project which looks like this: my_project/ __init__.py -- empty run.py datacheck/
I've run into a bit of a problem, which I simply cannot find a
I want to run a simple test project in a subdirectory alias on our
I want to run a simple JavaScript function on a click without any redirection.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.