Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read lines from a textfile 2

Status
Not open for further replies.

Virrum

Technical User
Sep 19, 2007
2
NO
Hi,
I've never looked at a perl script before, but I'd like to modify a fragment of a bigger program. So I'd like to ask you this:

sub RewriteFile {
my ($file, $tmpfile) = @_;
my $i;
my $temporary;
if (!open (IN, "$file")) {
print "Couldn't open input file $file\n";
return 0;
}
if (!open (TEMP, ">$tmpfile")) {
print("Couldn't open temporary file $tmpfile\n");
return 0;
}
binmode TEMP;
binmode IN;
while (<IN>) {
s/\r?\n$/\n/;
s/\r/\n/g;
$_ = &TabConversion($_);
# Walk through the line and see if it longer than MaxChars.
if (length($_) >= $MaxChars) {
print TEMP substr($_, 0, $MaxChars);
print TEMP "\n";
print TEMP substr($_, $MaxChars);
}
else {
print TEMP "$_";
}
}
print TEMP "\n";
close(IN);
close(TEMP);
return 1;
}

sub TabConversion {
my $line=shift(@_);
while($line=~/([^\t]*)\t/) {
my $spaces="";
my $spaceNO=$SpacesPerTab-(length $1)%$SpacesPerTab;
for (1..$spaceNO) {$spaces.=" ";}
$line =~ s/([^\t]*)\t/$1$spaces/;
}
return $line;
}


This script reads a text file, and writes a tmp file.

I'd like to just write line number 10 of that file. Can anybody help me with that?

thanks a lot in advance.
 
put a counter in and when = 10 print line!

Code:
 binmode TEMP;
    binmode IN;
   [b] my $counter=0;[/b]
    while (<IN>) {
        [b]$counter++;[/b]
        s/\r?\n$/\n/;
        s/\r/\n/g;
        $_ = &TabConversion($_);
        # Walk through the line and see if it longer than MaxChars.
      [b]if($counter == 10){[/b]
        if (length($_) >= $MaxChars ) {
            print TEMP substr($_, 0, $MaxChars);
            print TEMP "\n";
            print TEMP substr($_, $MaxChars);
        }
        else {
            print TEMP "$_";
        }
      [b]}[/b]
    }
    print TEMP "\n";
    close(IN);
    close(TEMP);
    return 1;
}

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Inside your while() loop, just tell it to only process the 10th line.
while(<IN>) {
if($. == 10) {
...
...
...
}
}
 
whats $. as a var?

I know @_ , $_ but not $.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Hi,
Thanks a lot! While you're in that theme, what is $_ and @_?
 
$. is the record counter of the last file you just read from.

Code:
print "$. = $_" while (<DATA>);

__DATA__
First Line
Second Line
Third Line
Fourth Line
 
cool as nice one Brigmar,

if you are doing a loop for example
Code:
for(@myarray){

print $_;

}

would $. equal anything in that senario?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
IC Brigmar, I was just applying the logic of the $ var to the loop, but alas, it doesn't work that way, would be cool if it did :)

thanks Kevin appreciated.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top