By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,643 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

Read from file into array

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.