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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:23:43+00:00 2026-05-30T23:23:43+00:00

Is it possible to load records from a file straight into a hash please?

  • 0

Is it possible to load records from a file straight into a hash please? The records are delimited with /begin and /end, and have a fixed order of content.

What I want is a hash populated like this:

hash_city{London}{slurped_record}='/begin CITY London\n   big\n   England\n   Sterling\n/end CITY'
hash_city{Paris}{slurped_record}='/begin CITY\n   Paris\n   big\n   France\n   Euro\n/end CITY'
hash_city{Melbourne}{slurped_record}='/begin CITY\n\n   Melbourne\n   big\n   Australia\n   Dollar\n   hot\n/end CITY'

I can then go off and process the records in the hash etc.. (reason for the ‘slurped_record’ entry is later I want to add new keys to say London like, ‘country=England’ etc

hash_city{London}{Country}='England'

I’ve managed to achieve something that works by slurping instead of reading the file line-by-line. Matching on a /begin, building up a record ($rec.=$_), then matching on a /end and processing. It’s a bit messy and wondered if there was a more elegant Perl approach..

My code attempt so far is as follows:

use strict;
use warnings;
use Data::Dumper;

my $string = do {local $/; <DATA>};
my %hash_city = map{$2=>$1} $string =~ /(\/begin\s+CITY\s+(\w+).+\/end\s+CITY)/smg;
print Dumper(%hash_city);

__DATA__
stuff
stuff
/begin CITY London
   big
   England
   Sterling
/end CITY

stuff
stuff

/begin CITY
   Paris
   big
   France
   Euro
/end CITY
stuff

/begin CITY

   Melbourne
   big
   Australia
   Dollar
   hot
/end CITY

stuff
  • 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-30T23:23:44+00:00Added an answer on May 30, 2026 at 11:23 pm

    Made a little program to show the other way around, advancing your process as well. ) Don’t know whether is’t elegant or not, but I suppose it gets the job done. )

    my %city_record;
    
    ## we're going to process the input file in chunks.
    ## here we define the chunk start marker, and make Perl to separate file input with it
    local $/ = "/begin CITY";
    
    # ignoring anything before the first section starts
    scalar <DATA>;
    
    while (<DATA>) {
      # throwing out anything after the section end marker
      # (might be done with substr-index combo as well, 
      # but regex way was shorter and, for me, more readable as well )
      my ($section_body) = m{^(.+)/end CITY}ms;
    
      # now we're free to parse the section_body as we want. 
      # showing here pulling city name - and the remaining data, by using the split special case
      my ($city, @city_data) = split ' ', $section_body;
    
      # filling out all the fields at once
      # (may seem a bit unusual, but it's a simple hash slice actually, great Perl idiom)
      @{ $city_record{$city} }{qw/ size country currency misc /} = @city_data;
    }
    
    # just to test, use something of yours instead. )
    print Dumper \%city_record;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to load data from a single file with a 100,000+ records into multiple
Need to load data from a single file with a 100,000+ records into multiple
Is it possible to load new lines from a text file to variables in
I know. It is possible to dynamically load JavaScript and style sheet file into
I am trying to load all records from the database that have been created
Is it possible to load an html document into a DOM javascript object so
i have a problem with getting content from a XML into a mysql database.
I want to save some specific records from a database to a file, thus:
I have an array of records from a database (although the database is irrelevant
Is it possible to load child entities in a single query without using DataLoadOptions?

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.