CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
little script that doesn't work


New User

Dec 27, 2012, 3:34 AM

Post #1 of 3 (1852 views)
little script that doesn't work Can't Post

Dear perl masters

My problem is the following:
I'm trying to write a script in order to extract the five first characters from the second line of a ".txt" file.
I wrote this:

open (file1, "./file1.txt");

@lines1= <file1>;

$fivefirstfile1= substr($lines1[2], 0, 5);

print "Five first characters are: $fivefprot1\n";

But it doesn't work... Could you help me please ?


Dec 27, 2012, 6:08 AM

Post #2 of 3 (1832 views)
Re: [jeffle] little script that doesn't work [In reply to] Can't Post

You did not say what did not work. One mistake I do see is that you are working on the third line, not the second (array indices start at zero).

Always use strict and warnings. If your file only has two lines this would have found the error. (I suspect that this is true because you should have recognized the first five characters of the third line)

Always check the status of open. If your script, as is, is unable to open the text file, you will not get any output.

Edit: Please reference your post on perlmonks.
Good Luck,

(This post was edited by BillKSmith on Dec 27, 2012, 6:43 AM)

Veteran / Moderator

Dec 27, 2012, 6:11 AM

Post #3 of 3 (1832 views)
Re: [jeffle] little script that doesn't work [In reply to] Can't Post

You stored the value in $fivefirstfile1 but then used $fivefprot1 in your print statement. Those are different vars.

Also, $lines1[2] is the 3rd line, not the second.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives