Re: script not working
by ww (Archbishop) 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. | [reply] [d/l] [select] |
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";
| [reply] [d/l] [select] |
Re: script not working
by CountZero (Bishop) on Dec 27, 2012 at 19:03 UTC
|
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
| [reply] [d/l] |
Re: script not working
by Anonymous Monk on Dec 27, 2012 at 11:59 UTC
|
| [reply] |
|
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.
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
Re: script not working
by karlgoethebier (Abbot) on Dec 28, 2012 at 09:11 UTC
|
#!/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»
| [reply] [d/l] |
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";
| [reply] [d/l] |
|
| [reply] |
|
| [reply] |
|
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$!!
| [reply] |
|
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.
| [reply] |
|
Take lots of deep breaths, not everyone plays XP :)
| [reply] |