Perl

Moderators: Jonathan
Number of threads: 1259
Number of posts: 3644

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Stepping through a file Posted by saridski on 18 Dec 2008 at 2:36 PM
Hi there,

I have a file containg this:

aabbccddeeffgg

And i want the following output:

aa
ab
bb
bc
cc
cd
dd
de
..
..

I have tried with the following:

..
while (($n = read(F_IN, $data, 2, $offset)) != 0) {
print "$data\n";
$offset += 1;
}
..

But the output looks all wrong:

aa
abb
abcc
abcdd
abcdee
abcdeff
abcdefgh
abcdefghi
abcdefghij
abcdefghijk
abcdefghijk

Can anyone come with some insight? I'd really appreciate it.

Thanks in advance!

saridski.
Report
Re: Stepping through a file Posted by leeb003 on 10 Jan 2009 at 4:50 PM
If your file was called data.txt...here's one way to do it.

my $file = "data.txt";
my $str;

open FILE, "$file";

while (<FILE>) {
$str = $_;
$str =~ s/(..)/$1\n/g;
print $str;
}

I have some more script examples at www.scripthat.com.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.