Reading a certain spot from a file

I know how to read a file, and BTW, I'm a n00b to perl.

But how would I read a certain spot in a file?

for instance if the file looked like:

Mike|Oyerth|banana
Dan|g0|cheese

How would I program it just to read "Mike" and "Dan"?

Thank you!

Comments

  • Hi,

    Loads of ways of doing this. Here's how I might go about it depending on the problem I wanted to solve.

    [code]

    #Open file.
    open FILE, ") {
    #Split up line into an array.
    chop;
    my @line = split(/|/);

    #Suppose we want to print Mike and Dan...
    print "$line[0]
    ";
    }

    #Close file.
    close FILE;
    [/code]

    What you're looking for? I think you could also get it using a reg exp in the loop, maybe like this:-

    [code]#Go through each line in the file.
    while () {
    #Extract bits we want.
    m/(.*)|.*/is;

    #Suppose we want to print Mike and Dan...
    print "$1
    ";
    }
    [/code]

    I'm pretty sure first will work, ain't tested the second or done it that way, but occurs to be it could be done like that.

    TMTOWTDI... ;-)

    Jonathan

    -------------------------------------------
    Count your downloads:
    http://www.downloadcounter.com/
    And host your site:
    http://www.incrahost.com/
    Don't say I never give you anything... ;-)

  • Is there anyway I can get a vertical list?

    The way that you showd, gave a messy horizontal one...

    Thanx ^_^
  • Hi,

    I'm guessing you want to output this to a web page rather than the console then? If so, you'll want to put a
    tag before or after the
    in the print statement. Then there will be one item per line (e.g a vertical list). Newline characters don't count for much in HTML, you need to specify if you want to start a new line.

    Of course, if this was outputting to the console, having
    floating around all over would be messy too... So I figured I could leave it to you to sort out the easy bit of how you wanted to lay it out! :-)

    Hope this is what you meant anyway,

    Jonathan


    -------------------------------------------
    Count your downloads:
    http://www.downloadcounter.com/
    And host your site:
    http://www.incrahost.com/
    Don't say I never give you anything... ;-)

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories