473,804 Members | 3,031 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to read file into keyed array?

I have a text file ("eighty.txt ") that looks like this:

83|84|85|86

I can read the file into an array like this:

$numbers= file("eighty.tx t");

But how do I key the array? I'd like to use strings as keys: three, four, five,
six - do I add the key names in the file?

The reason I need keys is because I will need to open the file and change one or
more of the key elements every so often. So, I assume I would read the file
into an array, reference the key, and change the value for that key?

suggestions/examples welcome...

Thanks!

Jul 17 '05 #1
5 3511
"deko" <dj****@hotmail .com> schrieb im Newsbeitrag news:ru******** ***********@new ssvr25.news.pro digy.com... I have a text file ("eighty.txt ") that looks like this:

83|84|85|86

I can read the file into an array like this:

$numbers= file("eighty.tx t");

But how do I key the array? I'd like to use strings as keys: three, four, five, six - do I add the key names in the file?

The reason I need keys is because I will need to open the file and change one or more of the key elements every so often. So, I assume I would read the file into an array, reference the key, and change the value for that key?

suggestions/examples welcome...

Thanks!


Hi!

Look at the following code:

$lines=file('ei ghty.txt');
$numbers=explod e('|',$lines[0]);
$numbers[0]=11; //change any number
$output=implode ('|',$numbers); //create a string (numbers seperated by | )
$fhandler=fopen ('eighty.txt',' w');
fwrite($fhandle r,$output);
fclose($fhandle r);

1. First, the file is read into the array $lines.
2. Your numbers are in line no 1, seperated by '|'. -> use explode to read
the numbers into an array
3. now you can change the numbers, e.g. $numbers[0] (the first number).
4. finally, you create a string containing all numbers seperated by '|' and
write them into your file.
Erik
Jul 17 '05 #2
Uzytkownik "deko" <dj****@hotmail .com> napisal w wiadomosci
news:ru******** ***********@new ssvr25.news.pro digy.com...
The reason I need keys is because I will need to open the file and change one or more of the key elements every so often. So, I assume I would read the file into an array, reference the key, and change the value for that key?


If the actual format of the file doesn't matter, the easiest thing to do is
to serialize the array and save it to file.

$data = file_get_conten ts("array.dat") ;
$a = unserialize($da ta);

..... modify aray ...

$data = serialize($a);
file_put_conten ts("array.dat" , $data);
Jul 17 '05 #3
> contents of eighty.txt:

83|84|85|86

code:

1 $lines=file('ei ghty.txt');
2 $numbers=explod e('|',$lines[0]);
3 $numbers[0]=11; //change any number
4 $output=implode ('|',$numbers); //create a string (numbers seperated by | )
5 $fhandler=fopen ('eighty.txt',' w');
6 fwrite($fhandle r,$output);
7 fclose($fhandle r);


Fantastic! This is exactly what I was looking for - thanks.

A couple of questions:

Let's say I just want to get the value of the second element in the array (84).
Do I still need the ",$lines[0]" bit at the end of line 2? Is this optional, or
necessary, to begin the key index at 0? If it's optional, then my code might
look like this:

$lines=file('ei ghty.txt');
$numbers=explod e('|');
$my_second_numb er = $numbers[1];
echo "my second number = ".$my_second_nu mber[value]

This assumes the array will be automatically indexed starting at 0. Is this
correct? Also, I'm not opening the file with fopen - I don't need to, if all I
want is the value of $numbers[1], correct? I will not be adding or removing
anything from the file, so I will not have to worry about the the index getting
out of order - is this correct? Again, if ",$lines[0]" is optional in the above
example, then do I really need it when writing to the file?

Jul 17 '05 #4

"deko" <dj****@hotmail .com> schrieb im Newsbeitrag
news:xc******** *********@newss vr27.news.prodi gy.com...
contents of eighty.txt:

83|84|85|86

code:

1 $lines=file('ei ghty.txt');
2 $numbers=explod e('|',$lines[0]);
3 $numbers[0]=11; //change any number
4 $output=implode ('|',$numbers); //create a string (numbers seperated by | ) 5 $fhandler=fopen ('eighty.txt',' w');
6 fwrite($fhandle r,$output);
7 fclose($fhandle r);
Fantastic! This is exactly what I was looking for - thanks.

A couple of questions:

Let's say I just want to get the value of the second element in the array

(84). Do I still need the ",$lines[0]" bit at the end of line 2? Is this optional, or necessary, to begin the key index at 0? If it's optional, then my code might look like this:

$lines=file('ei ghty.txt');
$numbers=explod e('|');
$my_second_numb er = $numbers[1];
echo "my second number = ".$my_second_nu mber[value] explode needs two arguments: the seperator ( in your case '|', and the
string that should be read into an array; in your case, this is the first
row of the textfile ($lines[0] ), so you shouldn't change the first two
rows.

if you want to get the value of the second element, you simply use
$numbers[1], the third $numbers[2] and so on.
your example should look like this:

$lines=file('ei ghty.txt');
$numbers=explod e('|',$lines[0]);
$my_second_numb er = $numbers[1];
echo "my second number = ".$my_second_nu mber;

This assumes the array will be automatically indexed starting at 0. Is this correct? yes, the index of arrays usually starts at 0. Also, I'm not opening the file with fopen - I don't need to, if all I
want is the value of $numbers[1], correct? you could also use fopen(), fread(), ... to get the content of the text
file, but in your case as the file has only one line its the easiest way to
use
$lines=file('ei ghty.txt');
to read the content.
I will not be adding or removing
anything from the file, so I will not have to worry about the the index getting out of order - is this correct? Again, if ",$lines[0]" is optional in the above example, then do I really need it when writing to the file?


$lines[0] is not optional. it is required to read your data.



Jul 17 '05 #5
Okay - it seems to be working as you've described - thanks.

I have another issue with this script I'm wondering if you can help me with. I
want to update certain values in the file based on a time interval. For
example:

$lines=file('ei ghty.txt');
$numbers=explod e('|',$lines[0]);

//pseudo code
$startdate = "03/19/2004"
if today's date is more than one day ahead of $startdate, then:

$numbers[0]=11; //change any number
$output=implode ('|',$numbers); //create a string (numbers seperated by | )
$fhandler=fopen ('eighty.txt',' w');
fwrite($fhandle r,$output);
fclose($fhandle r);

Basically, I want a conditional clause that prevents $numbers[0] from being
updated until 24 hours after the $startdate. How would I code this? Is
"03/19/2004" proper format for specifying a date?
Jul 17 '05 #6

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

Similar topics

8
17092
by: Chris | last post by:
Can anybody help. I need to read a txt file backwords line by line. Can anybody help me do this. Thanks Chris
8
3727
by: Gary Quiring | last post by:
I am trying to test a PHP script that is going to parse an XML string to access an Informix database. I have successfully recompiled PHP for Informix. I am running Fedora Core 3 with PHP 5. How can for testing purposes send an XML file to my PHP program running on Apache? Thanks Gary
6
1855
by: Xah Lee | last post by:
© # -*- coding: utf-8 -*- © © # in Python, there's a special type of © # data structure called keyed list. it © # is a unordered list of pairs, each © # consists of a key and a value. It is © # also known as dictionary. © © # define a keyed list © aa = {'john':3, 'mary':4, 'jane':5, 'vicky':7}
33
2374
by: jacob navia | last post by:
Recently there was a discussion in this group about how to retrieve the file name given a FILE *. The question raised my curiosity, and after some research I have come up with a good implementation. The solution is in the tutorial for lcc-win32 (http://www.cs.virginia.edu/~lcc-win32) page 331.
9
5214
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still facing problems: Assume that FILE* filePointer; unsigned char lineBuffer;
9
2025
by: srikanth | last post by:
i have a text file like below, test.txt file (actually my test file file is with 10000 lines but here i tested with 3 lines) 3 06.09.2006 16:37:25 3 06.09.2006 16:40:02 3 06.09.2006 16:42:31 i want to read this and output as it looks but iam getting abnormal
14
11646
by: chance | last post by:
Hello, I have a file on disk called TEMP.ZIP and I would like to somehow get this into a memory stream so I can eventually do this: row = dataStream.ToArray() However, I am not sure of the correct way to get it into a memory stream. Any help appreciated.
22
4734
by: Steve Richter | last post by:
Does the .NET framework provide a class which will find the item in the collection with a key which is closest ( greater than or equal, less than or equal ) to the keys of the collection? ex: collection keys are 20, 30, 40, 50, 60, 70, 80 find key which is >= 35. would return the 30 key.
5
11281
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a couple areas that have ASCII text that I need to extract. At the end of the 2580 bytes, I can read the report like a standard text file. It should have CR/LF at the end of each line. What is the best way for me to read this report using C#. It is...
0
9705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9575
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10564
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10320
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10308
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6846
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2981
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.