473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How to read associative array from file?

If I had a text file with the following
two columns:

key1 value1
key2 value2
key3 value3

How would I read in this file and create an
associative array?
-Thanks

Jul 19 '05 #1
2 11113
nospam wrote:
If I had a text file with the following two columns:

key1 value1
key2 value2
key3 value3

How would I read in this file and create an associative array?


What have you tried? Give it a serious try by help of the Perl docs,
and somebody may be willing to help you correct it if needed.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #2
nospam <no@spam.com> wrote in message news:<pa****************************@spam.com>...
If I had a text file with the following
two columns:

key1 value1
key2 value2
key3 value3

How would I read in this file and create an
associative array?
-Thanks


Hi,

There are two solutions for this problem, and each one depends on the
kind of the file you are trying to read.

1. If your columns are not fixed-width, but are separated by some sort
of a delimiter ( A double space in your example ). Then you can use
this:

my @cols = split($delimiter, $line);

Where $delimiter is the column separator and $line is the line you
want to extract information from. This will give you the key in
$cols[0] and the value in $cols[1] where you can easily put them in a
hash. Note that you can use a regular expression in place of
$delimiter.

2. If your columns are fixed-width, then there is a faster and more
efficient way to extract information from them ( according to the
"Effective Perl Programming" book ):

my @cols = unpack 'A32 A250', $line;

The above example assumes that the key column is always 32 characers
wide and the value column is 250 characters wide. If the key is less
than 32 characters, than you have to add spaces to it's right until
you get a count of 32 characters.

A Complete Example:

use Fcntl ':flock';

my %file = ();

open(FILE, 'foo.txt') or die($!);
flock(FILE, LOCK_SH) or die($!);
while(<FILE>) {
chomp($_);

# The delimiter here is a comma
my @line = split(',', $_);

# Put the key => value pair in the hash
$file{$line[0]} = $line[1];
}
close(FILE);
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stefan Richter | last post by:
Hi, I want to create an associative Array with a PHP variable (article ID) as Key and another associative array as it's value. How do I instanciate it, how can I fill it? I want something...
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
5
by: soup_or_power | last post by:
Hi I have an associative array like this: arr=30; arr=20;arr=40;arr=10; I want the sort function to sort keys in ascending order of the values on the right hand side with the following result:...
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
1
by: swayze | last post by:
Hi there, We're using php 4.1 and it doesn't seem to have built in support for this. Coming from a dotnet background this surprised me...Anyways, thats a different topic altogether... I'm...
3
by: pbali | last post by:
Hi, I am using PHP 5.1 and MySQL. I have a result set obtained by executing PDO:: query. I want to create an XML file by using this result set. The XML file will contain column names as XML node...
8
code green
by: code green | last post by:
I am transfering data from a CSV file to a MySql database. Unfortunately the dates are in 2008.27.08 US string format instead instead of 2008-08-27 database format. The CSV data is first read into...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.