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

New User

Dec 27, 2012, 3:34 AM

Views: 2800
little script that doesn't work

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

Views: 2780
Re: [jeffle] little script that doesn't work

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

Views: 2780
Re: [jeffle] little script that doesn't work

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.