Loading ...
Sorry, an error occurred while loading the content.
 

Include file within script

Expand Messages
  • malaikat@inbox.as
    Hi, How to include a file within script? Say I have a file called top.txt in the root dir, and I want the perl script to display the pure texts contents of
    Message 1 of 2 , Jan 2, 2002
      Hi,

      How to include a file within script?

      Say I have a file called "top.txt" in the root dir, and I want the perl
      script to display the pure texts contents of the file using print at the
      display when the script executed.


      Thanks,
      --Malai
    • Purr
      ... I m assuming this is what you want to do. 1. open text file 2. display text file 3. close text file. to do this you do #!perl open(FILE,
      Message 2 of 2 , Jan 2, 2002
        At 02:59 AM 1/2/02 -0800, you wrote:
        >Hi,
        >
        >How to include a file within script?
        >
        >Say I have a file called "top.txt" in the root dir, and I want the perl
        >script to display the pure texts contents of the file using print at the
        >display when the script executed.
        >
        >
        >Thanks,
        >--Malai

        I'm assuming this is what you want to do.
        1. open text file
        2. display text file
        3. close text file.

        to do this you do

        #!perl
        open(FILE, "<top.txt");

        while $i (<FILE>) {
        chomp($i);
        print "$i\n";
        }

        close(FILE);

        Lets explain a little.

        open(); opens a file

        You have to name the varivable that will contain it... <FILE> then put in
        a comma (DON'T FORGET THE COMMA) Big mistake made by everyone.

        Then put in the path for the file. I just put the file name for current
        directory. if you on win32 like I am you may want to put the path in but
        be sure to use / and not \ because it will pass through and you won't know
        why it doesn't work. I spent hours today working on a script and finally
        noticed this mistake. example( c:/dir/top.txt = good c:\dir\top.txt = bad)

        the other thing is the < the > and the others I wont go into.
        The open(FILE,"<file.txt"); the < mean read from file and the > means
        write to file. There are others you can use but you should look them up at
        another time.

        dont' forget the ; it also kills programs.

        I use the while because it will go through line be line without having to
        load the whole document into memory before running. That causes problems
        when working with 200meg log files.

        so while $i (<FILE>) {}

        I use $i out of habbit because I get lost easily. You CAN go while $_
        (<FILE>) {} and it's the same thing. . and since this is perl you can just
        drop it completely. while(<FILE>) {}

        But as I said I get lost easy. ... Are you lost yet?

        chomp($i); will take any \n's from the lines. I like to do this cause I
        don't like seeing text show up with spaces between the lines unless it
        really needs to be there.

        NOW we get to print it. So we do a print "$i\n"; (Hey we just took the \n
        off. why did we put it back on.) Because you don't want the whole
        document to print on one line. AND I've run into cases with the spaces
        between. so I chomp first then manually put it on to be sure it's there.

        (NOTE: I've been awake for 3 days now so I may not make much sense)

        Then we need to close the file.

        close(FILE);

        this will print the text file no matter how long the file is.


        here is the other way. OH NO NOT ANOTHER WAY

        #!perl

        open(FILE, "<c:/dir/top.txt");
        @FILE = <FILE>;
        close(FILE);

        foreach $i (@FILE) {
        chomp($i);
        print "$i\n";
        }


        looks similar

        On a server that changes the file on the fly it's a good idea to open the
        file and close it again quick as for some things it may take a while for it
        to be closed and things tend to get confused when 3 people are updating the
        file and someone else is currently reading it.

        SO we open the file
        Copy the whole file into an array
        Close the file.

        Each line of the file will go into each part of the array. This makes for
        easy line counts and other things.

        I use foreach cause I have a macro set up for it. BUT I think you can use
        while the same way just substitue <FILE> with @FILE

        oh oh oh Yeah variables names.. Perl looks at all variable types as
        different. $FILE @FILE <FILE %FILE &FILE are all different items and perl
        reads them as different. If you don't know what these are then I suggest
        going to www.cgi101.com and read the first few free chapters. In the time
        it takes to read about 20 pages you will be up to speed with the rest of
        us... kinda

        anyway I like to keep the variable name the same as I said before I get
        lost. and as long as I know WHAT I want be it an array, or a file I can
        just use the symbols.

        I think that's it... Oh and sorry for all the text. Little much for 5-7
        lines of code eh?

        again sorry.
        --
        PurrNaK








        ----------

        ______________________________________________________________________
        This messsage was sent using the trial version of the
        1st Class Mail Server software. You can try it for free
        at http://www.1cis.com/download/1cismail.asp

        Is this unsolicited email? Instructions for reporting unsolicited
        email can be found at at http://www.1cis.com/articles/spam.asp


        [Non-text portions of this message have been removed]
      Your message has been successfully submitted and would be delivered to recipients shortly.