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

  • SEARCH
  • Home
  • 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 703871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:52:43+00:00 2026-05-14T03:52:43+00:00

I have a hash in which I store the products a customer buys (

  • 0

I have a hash in which I store the products a customer buys (%orders). It uses the product code as key and has a reference to an array with the other info as value.

At the end of the program, I have to rewrite the inventory to the updated version (i.e. subtract the quantity of the bought items)

This is how I do rewrite the inventory:

sub rewriteInventory{
    open(FILE,'>inv.txt');
    foreach $key(%inventory){
        print FILE "$key\|$inventory{$key}[0]\|$inventory{$key}[1]\|$inventory{$key}[2]\n"
    }
    close(FILE);
}

where $inventory{$key}[x] is 0 → Title, 1 → price, 2 → quantity.

The problem here is that when I look at inv.txt afterwards, I see things like this:

CD-911|Lady Gaga - The Fame|15.99|21
ARRAY(0x145030c)|||
BOOK-1453|The Da Vinci Code - Dan Brown|14.75|12
ARRAY(0x145bee4)|||

Where do these ARRAY(0x145030c)||| entries come from? Or more important, how do I get rid of them?

  • 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-14T03:52:44+00:00Added an answer on May 14, 2026 at 3:52 am

    EDIT: I was wrong about having to use an explicit dereferencing arrow; this is inferred between brackets when necessary, even if the first brackets do NOT require a dereference. That said, I will leave the remainder of the answer as posted since it was accepted, but merely note that if you choose not to use join, you needn’t actually use $inventory{$key}->[0] but can in fact use $inventory{$key}[0] as originally posted.

    Just be aware that the first (hash) brackets do not imply a dereference but the second (array) brackets do. Your errant array refs in the output were coming from looping over not only keys but also values of the hash.

    ORIGINAL ANSWER:

    In addition to using keys, you also need to dereference the references-to-array (this is why you’re seeing each value output as ARRAY with an address—you’re printing the references, not the values of the dereferenced array) when you print, so your loop becomes something like:

    foreach my $key (sort keys %inventory) {
        print FILE "$key\|$inventory{$key}->[0]\|$inventory{$key}->[1]\|$inventory{$key}->[2]\n";
    }
    

    I’d probably rewrite it a little more idiomatically as:

    foreach my $key (sort keys %inventory) {
        print FILE (join '|', $key, @{$inventory{$key}}) . "\n";
    }
    

    Hope that helps!

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

Sidebar

Related Questions

I have a page which uses a the hash # to store some extra
What I can think of is: Algo: Have a hash table which will store
I have a table which should store an id, a name and a hash.
Say I have a population of key-value pairs which I plan to store in
For example i have algorithmic function, which calculates specific hash-code. Function itself is 300+
I have a ruby hash which looks like {10.1.1.6=>nick, 127.0.0.1=>nick1} But I can't manage
I have a string which looks like a hash: { :key_a => { :key_1a
I have an object for which I want to generate a unique hash (override
I have a db column which is a serialized Hash : class Foo <
I'm using leveldb to store records (key-value), where the key is a 64-bit hash

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.