Read from file into array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthew Sims

    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="onca ll/.oncall";

    $n=file_get_con tents($filename );

    echo "$oncall[$n]";

    This prints nothing.
    But when I comment out the file_get_conten ts 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_conten ts. Any thoughts?

    Much appreciation!

    --Matt
  • Andy Hassall

    #2
    Re: Read from file into array

    On 6 Aug 2003 15:33:17 -0700, matt@killermook ie.org (Matthew Sims) wrote:
    [color=blue]
    >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="onc all/.oncall";
    >
    >$n=file_get_co ntents($filenam e);
    >
    >echo "$oncall[$n]";
    >
    >This prints nothing.
    >But when I comment out the file_get_conten ts 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_conten ts. Any thoughts?[/color]

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

    --
    Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
    Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

    Comment

    • James Jiao

      #3
      Re: Read from file into array

      Hi, Matt

      An easier way to check whether the returned value contains a carriage return
      is to run this after $n=file_get_con tents($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" <matt@killermoo kie.org> wrote in message
      news:1e963607.0 308061433.766dc 97f@posting.goo gle.com...[color=blue]
      > 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="onca ll/.oncall";
      >
      > $n=file_get_con tents($filename );
      >
      > echo "$oncall[$n]";
      >
      > This prints nothing.
      > But when I comment out the file_get_conten ts 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_conten ts. Any thoughts?
      >
      > Much appreciation!
      >
      > --Matt[/color]


      Comment

      • Matthew Sims

        #4
        Re: Read from file into array

        "James Jiao" <jamesjiao@para dise.net.nz> wrote in message news:<rigYa.965 8$9f7.1148199@n ews02.tsnz.net> ...[color=blue]
        > Hi, Matt
        >
        > An easier way to check whether the returned value contains a carriage return
        > is to run this after $n=file_get_con tents($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" <matt@killermoo kie.org> wrote in message
        > news:1e963607.0 308061433.766dc 97f@posting.goo gle.com...[color=green]
        > > 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="onca ll/.oncall";
        > >
        > > $n=file_get_con tents($filename );
        > >
        > > echo "$oncall[$n]";
        > >
        > > This prints nothing.
        > > But when I comment out the file_get_conten ts 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_conten ts. Any thoughts?
        > >
        > > Much appreciation!
        > >
        > > --Matt[/color][/color]


        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.

        Comment

        • Matthew Sims

          #5
          Re: Read from file into array

          "James Jiao" <jamesjiao@para dise.net.nz> wrote in message news:<rigYa.965 8$9f7.1148199@n ews02.tsnz.net> ...[color=blue]
          > Hi, Matt
          >
          > An easier way to check whether the returned value contains a carriage return
          > is to run this after $n=file_get_con tents($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" <matt@killermoo kie.org> wrote in message
          > news:1e963607.0 308061433.766dc 97f@posting.goo gle.com...[color=green]
          > > 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="onca ll/.oncall";
          > >
          > > $n=file_get_con tents($filename );
          > >
          > > echo "$oncall[$n]";
          > >
          > > This prints nothing.
          > > But when I comment out the file_get_conten ts 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_conten ts. Any thoughts?
          > >
          > > Much appreciation!
          > >
          > > --Matt[/color][/color]


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

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

          Same results, no change.

          Comment

          • David Robley

            #6
            Re: Read from file into array

            In article <1e963607.03080 70855.668f0d60@ posting.google. com>,
            matt@killermook ie.org says...[color=blue]
            > I got it to work!!
            >
            > After posting my last reply here I tried one more adjustment:
            >
            > $fp=fopen($file name,"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.[/color]

            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?

            Comment

            Working...