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 6776801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:04:20+00:00 2026-05-26T16:04:20+00:00

In perl DBI , you can bind a hash element value to a specific

  • 0

In perl DBI, you can bind a hash element value to a specific column of your query results.
Such that,
as you fetch each row, the hash element value is updated to the current row.

I’m trying to figure out if there’s a way to accomplish that in core perl,
using an hash of (array of arrays).

The end goal is to be able to run something like this:

my $i;
my @a = (
  [1,2,3],
  [4,5,6],
  [7,8,9]
);
my %superhash{'first', 'second', 'third'} = (\$i[0], \$i[1], \$i[2]);
for $i (@a) {
  print ${$hash{'first'}} . "\n";
}

I’d love to be able to point $hash{'first'} to the first element in an arrayref, without having to have an array to point to beforehand.

Expected output:

1
4
7

Of course, that code doesn’t work, because there are no elements that $i references to, thus they are undefined.

  • 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-05-26T16:04:20+00:00Added an answer on May 26, 2026 at 4:04 pm

    The tie interface is Perl’s way of making magic variables. In this case, a bit of glob magic needs to be thrown in due to the way the for loop assigns to its loop variable.

    my @a = (
        [1,2,3],
        [4,5,6],
        [7,8,9]
    );
    
    {package Tie::Rows;
        my %keys = qw(first 0 second 1 third 2);
        sub TIEHASH {bless [$_[1]]}               # store glob reference
        sub FETCH {$${$_[0][0]}[$keys{$_[1]}]}    # deref glob as array, lookup key
    }
    
    tie my %hash, 'Tie::Rows', *i;  # passing the glob here
    
    for our $i (@a) {  # since the for loop aliases at that level
        print $hash{first} . "\n";
    }
    

    which prints

    1
    4
    7
    

    You can also exploit dynamic scope to solve this problem:

    sub first  () {$$_[0]}  # each of these uses the array in $_
    sub second () {$$_[1]}
    sub third  () {$$_[2]}
    
    for (@a) {              # for loop puts each array into $_
        print second, $/;
    }
    

    which prints

    2
    5
    8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running DBI in Perl and can't figure out how, when I run a
I like to create a generic perl script that will input sql query from
Some Perl modules, such as DBI , need to be downloaded, compiled and installed
I've connected to a MySQL database using Perl DBI. I would like to find
I'm using Perl's DBI module. I prepare a statement using placeholders, then execute the
How do I connect to a MSSQL database using Perl's DBI module in Windows?
I'm trying to read data from SQL Server database using Perl and the DBI
I'm having a problem with executing a stored procedure from Perl (using the DBI
Is CPAN DBI the best database interface to use in Perl for general database
i have something like: #!/usr/bin/perl use strict; use warnings; use CGI::Simple; use DBI; my

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.