473,385 Members | 1,570 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,385 software developers and data experts.

Read from file into array

I see what my problem is but not sure what I need to change. I'm
reading a single number from a file that's fed into an array but the
array doesn't want to print out its output.

$oncall=array();

$oncall[1]="msims";
$oncall[2]="dtilbury";
$oncall[3]="jma";
$oncall[4]="jweaver";
$oncall[5]="cirwin";
$filename="oncall/.oncall";

$n=file_get_contents($filename);

echo "$oncall[$n]";

This prints nothing.
But when I comment out the file_get_contents and simply do $n="2" the
array prints out dtilbury like it should. My guess is the error lies
in how $n is being assigned from the file_get_contents. Any thoughts?

Much appreciation!

--Matt
Jul 16 '05 #1
5 9190
On 6 Aug 2003 15:33:17 -0700, ma**@killermookie.org (Matthew Sims) wrote:
I see what my problem is but not sure what I need to change. I'm
reading a single number from a file that's fed into an array but the
array doesn't want to print out its output.

$oncall=array();

$oncall[1]="msims";
$oncall[2]="dtilbury";
$oncall[3]="jma";
$oncall[4]="jweaver";
$oncall[5]="cirwin";
$filename="oncall/.oncall";

$n=file_get_contents($filename);

echo "$oncall[$n]";

This prints nothing.
But when I comment out the file_get_contents and simply do $n="2" the
array prints out dtilbury like it should. My guess is the error lies
in how $n is being assigned from the file_get_contents. Any thoughts?


Print out $n using var_dump or print_r. Does it contain a newline at the end?

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #2
Hi, Matt

An easier way to check whether the returned value contains a carriage return
is to run this after $n=file_get_contents($filename):

echo "$n is this on the same line?";

If the string after $n wraps to a new line, then there is something wrong
with the return type. If $n does not print out anything at all, then either
the file get function is at fault or the $filename variable.

Hope this helps

==James==

"Matthew Sims" <ma**@killermookie.org> wrote in message
news:1e**************************@posting.google.c om...
I see what my problem is but not sure what I need to change. I'm
reading a single number from a file that's fed into an array but the
array doesn't want to print out its output.

$oncall=array();

$oncall[1]="msims";
$oncall[2]="dtilbury";
$oncall[3]="jma";
$oncall[4]="jweaver";
$oncall[5]="cirwin";
$filename="oncall/.oncall";

$n=file_get_contents($filename);

echo "$oncall[$n]";

This prints nothing.
But when I comment out the file_get_contents and simply do $n="2" the
array prints out dtilbury like it should. My guess is the error lies
in how $n is being assigned from the file_get_contents. Any thoughts?

Much appreciation!

--Matt

Jul 16 '05 #3
"James Jiao" <ja*******@paradise.net.nz> wrote in message news:<ri********************@news02.tsnz.net>...
Hi, Matt

An easier way to check whether the returned value contains a carriage return
is to run this after $n=file_get_contents($filename):

echo "$n is this on the same line?";

If the string after $n wraps to a new line, then there is something wrong
with the return type. If $n does not print out anything at all, then either
the file get function is at fault or the $filename variable.

Hope this helps

==James==

"Matthew Sims" <ma**@killermookie.org> wrote in message
news:1e**************************@posting.google.c om...
I see what my problem is but not sure what I need to change. I'm
reading a single number from a file that's fed into an array but the
array doesn't want to print out its output.

$oncall=array();

$oncall[1]="msims";
$oncall[2]="dtilbury";
$oncall[3]="jma";
$oncall[4]="jweaver";
$oncall[5]="cirwin";
$filename="oncall/.oncall";

$n=file_get_contents($filename);

echo "$oncall[$n]";

This prints nothing.
But when I comment out the file_get_contents and simply do $n="2" the
array prints out dtilbury like it should. My guess is the error lies
in how $n is being assigned from the file_get_contents. Any thoughts?

Much appreciation!

--Matt

Thanks for the response. I did both tests to see what $n looks like.

I did var_dump ($n) with this output:
string(2) "2 "
Is that really a space after the 2? There's no space inside the file
being read.

I did print_r ($n) with this output:
2
Can't tell if that space is there

And I also did echo "$n is this on the same line"; with this output:
2 is this on the same line
Doesn't look like the space is there

It seems $n is being assigned properly. But when I stick $n into the
$oncall array it still prints out nothing.

echo "$oncall[$n]";
Prints nothing.
Jul 16 '05 #4
"James Jiao" <ja*******@paradise.net.nz> wrote in message news:<ri********************@news02.tsnz.net>...
Hi, Matt

An easier way to check whether the returned value contains a carriage return
is to run this after $n=file_get_contents($filename):

echo "$n is this on the same line?";

If the string after $n wraps to a new line, then there is something wrong
with the return type. If $n does not print out anything at all, then either
the file get function is at fault or the $filename variable.

Hope this helps

==James==

"Matthew Sims" <ma**@killermookie.org> wrote in message
news:1e**************************@posting.google.c om...
I see what my problem is but not sure what I need to change. I'm
reading a single number from a file that's fed into an array but the
array doesn't want to print out its output.

$oncall=array();

$oncall[1]="msims";
$oncall[2]="dtilbury";
$oncall[3]="jma";
$oncall[4]="jweaver";
$oncall[5]="cirwin";
$filename="oncall/.oncall";

$n=file_get_contents($filename);

echo "$oncall[$n]";

This prints nothing.
But when I comment out the file_get_contents and simply do $n="2" the
array prints out dtilbury like it should. My guess is the error lies
in how $n is being assigned from the file_get_contents. Any thoughts?

Much appreciation!

--Matt

In addition to performing the tests stated above, I also tried the following:

$fp=fopen($filename,"r");
$n=fread($fp, 2);
fclose($fp);

Same results, no change.
Jul 16 '05 #5
In article <1e**************************@posting.google.com >,
ma**@killermookie.org says...
I got it to work!!

After posting my last reply here I tried one more adjustment:

$fp=fopen($filename,"r");
$n=fread($fp, 1);
fclose($fp);

And now the whole process works.

For some reason, when reading the number from the file it always
placed a space at the end of the number despite the fact that there is
no space after the number in the file.

Thanks to those that opened up my troubleshooting more.


As has previously been suggested, the extra character is a newline, not a
space. It will appear as a space if you view it in output in a browser,
but if you do a view source on a document containing it you'll see it as a
newline.

Cheers
--
Quod subigo farinam

$email =~ s/oz$/au/o;
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet?
Jul 16 '05 #6

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

Similar topics

8
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
5
by: deko | last post by:
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.txt"); But how do I key the array? I'd like to use...
3
by: deko | last post by:
It's nice to be able to generate an html table from a PHP array. I know how to do this, but the array in question is built from a file. The file in question can be very long, and I only want the...
3
by: Wei-Chao Hsu | last post by:
There are some data files look like 1.) data1.txt ---------------- 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 2.) data2.txt
9
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...
9
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...
14
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...
5
by: runsun | last post by:
Thanks in advance. This program is written in C. It needs to read all characters from a file; then write them into a 3D array (yes, 3D!). The file is a .prn file (one of the Excel types), which...
13
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
5
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.