Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

script not working

by jeffle (Initiate)
on Dec 27, 2012 at 11:51 UTC ( #1010502=perlquestion: print w/ replies, xml ) Need Help??
jeffle has asked for the wisdom of the Perl Monks concerning the following question:

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 please help me ?

Comment on script not working
Download Code
Re: script not working
by Anonymous Monk on Dec 27, 2012 at 11:59 UTC
      Or even better, use strict; as shown here:
      #! perl -w use strict; my @lines1= <DATA>; print "number of lines = $#lines1\n"; my $fivefirstfile1= substr($lines1[2], 0, 5); print "Five first characters are: $fivefprot1\n"; __DATA__ First line of the file. Second line of the file. Third line of the file. Fourth line of the file. Fifth line of the file. Sixth line of the file.

        That will still not work, except this line print "Five first characters are: $fivefprot1\n";is changed to print "Five first characters are: $fivefirstfile1\n";
        variable $fivefprot1 requires explicit package name.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
Re: script not working
by Anonymous Monk on Dec 27, 2012 at 12:08 UTC
    You need to print the variable $fivefirstfile1, but you're printing the non-defined variable $fivefprot1.
    If you're using string and warnings, you can catch it.

    You might want to consider using something like this:
    use strict; use warnings; my $file = $0; open(my $fh, '<', $0) or die "$file: $!"; my @lines = <$fh>; my $chars = substr($lines[7], 0, 5); print "Five first characters are: <$chars>\n";
Re: script not working
by ww (Bishop) on Dec 27, 2012 at 13:06 UTC
    Aside from issues already noted, you're processing the wrong element of the array, @lines1.

    Array elements start at ZERO, not one.

    Update: For clarity (Apologies for the late update and despite upvotes already cast), the "wrong element" problem is in Ln 3 of the OP, at $lines1[2] and is unrelated to the name of the array, which was provided for (unnecessary?) identification.

Re: script not working
by RFMonk (Acolyte) on Dec 27, 2012 at 16:29 UTC
    Hi mate please check the following, just change the path since I work on Windows.
    #!/usr/bin/perl use strict; use warnings; open (file1, "file1.txt"); my @lines1= <file1>; my $fivefirstfile1= substr($lines1[2], 0, 5); print "Five first characters are:", $fivefirstfile1, "\n"; # print "Five first characters are: $fivefprot1\n";

      just change the path since I work on Windows.

      forward slashes work even on windows

        thanks. Didn´t know.

      Wrong, wrong, wrong! You still have the original error in the array_element's index... and your reply duplicates what's offered (one hopes, "with good intent," but with the same mistake) above.

      Please, test your answers. Any other approach runs a serious chance that you'll be providing bad guidance to future readers.

      <rant> ... especially when XP-whoring leads the careless to upvote your node.
      aaaAAARGH!</rant> &cv():#r$!!

        the code was tested, and the index should be 1 instead of 2, since start with 0 and the outcome is Line#2. In that you are right. But, the code works!!!

        Happy new year.

        Take lots of deep breaths, not everyone plays XP :)
Re: script not working
by CountZero (Bishop) on Dec 27, 2012 at 19:03 UTC
    Putting the whole file into an array is wasteful of time and memory. What will happen if it is a 10 GB file?

    You can do just as easy the following:

    use Modern::Perl; use autodie; open my $file1, '<', './file1.txt'; <$file1>; my $secondline = <$file1>; my $fivefirstfile1= substr($secondline, 0, 5);
    It is faster and uses less memory.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: script not working
by karlgoethebier (Curate) on Dec 28, 2012 at 09:11 UTC

    Why not doing it like this:

    #!/usr/bin/perl use strict; use warnings; use IO::All; use autodie qw(:all); my $file = io shift; print $file->[1] =~ m/(.{5})/; __END__

    Short, nice, doesn't matter how big the file is...

    See IO::All and Tie::File

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1010502]
Approved by McDarren
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-11-22 17:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (123 votes), past polls