For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > December 2007 > how to increment to records using SEEK









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to increment to records using SEEK
Minky Arora

2007-12-18, 7:00 pm

Hi Gurus,

I am parsing through a file and need to print the records in the
following order:

Minky Arora
235 River Drive,
Newton,PA 19073

Here is my code:
!/usr/bin/perl

use strict;
open FILE, "/users/meenaksharora/db.txt" or die"cnt open $!";
my($fname,$lname,$address,$lline,$line,@
db);
foreach my $line(<FILE> ){
$fname=substr($line,0,10);
$lname=substr($line,10,15);
$address=substr($line,16,25);

$lline=substr($line,59,13);
$fname=~s/\s+/ /;
$lname=~s/\s+/ /;
$address=~s/\s+/ /;
$lline=~s/\s+/ /;
print "$fname $lname\n";


print "$address\n";
print"$lline\n";
}

Now each record as I calculated is of a fixed lenght of 75 chars.I
need some idea as to how to run the Loop to print all such
records.Right now I am only able to print the first record,

Thanks in advance,
Min
Tom Phoenix

2007-12-18, 7:00 pm

On Dec 18, 2007 1:48 PM, minky arora <minky.arora@gmail.com> wrote:

> !/usr/bin/perl


I think you meant for that to begin with #!, not to nitpick....

> foreach my $line(<FILE> ){


That should run your loop once for each line in the file. But I prefer
this line:

while (my $line = <FILE> ) {

That should also run the loop once for each line in the file. But
because it uses <FILE> in scalar context, it will read one line, run
the loop, then go back to read another line. Using <FILE> in list
context (as foreach does) means to read the entire file at once, but
there's no need to bring what may be a large file into memory if
you'll be processing it a line at a time. (To be sure, perl may
optimize this code; but it's safer in general to use while.)

> $fname=substr($line,0,10);
> $lname=substr($line,10,15);
> $address=substr($line,16,25);
>
> $lline=substr($line,59,13);


That doesn't add up right, unless you're not using substr() correctly.

http://perldoc.perl.org/functions/substr.html

For the kind of thing you're looking to do, unless you really do want
overlapping fields, I think you want unpack().

http://perldoc.perl.org/functions/unpack.html
http://perldoc.perl.org/functions/pack.html

> Now each record as I calculated is of a fixed lenght of 75 chars.I
> need some idea as to how to run the Loop to print all such
> records.Right now I am only able to print the first record,


Perhaps your data file isn't organized in lines? The readline operator
(angle brackets around a filename, like <FILE> ) reads a text file a
line at a time, but it's not usually appropriate for non-text files.
You probably want the read() operator.

http://perldoc.perl.org/functions/read.html

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training
John W . Krahn

2007-12-18, 7:00 pm

On Tuesday 18 December 2007 13:48, minky arora wrote:
>
> Hi Gurus,


Hello,

> I am parsing through a file and need to print the records in the
> following order:
>
> Minky Arora
> 235 River Drive,
> Newton,PA 19073
>
> Here is my code:
> !/usr/bin/perl
>
> use strict;
> open FILE, "/users/meenaksharora/db.txt" or die"cnt open $!";
> my($fname,$lname,$address,$lline,$line,@
db);
> foreach my $line(<FILE> ){
> $fname=substr($line,0,10);
> $lname=substr($line,10,15);
> $address=substr($line,16,25);
>
> $lline=substr($line,59,13);
> $fname=~s/\s+/ /;
> $lname=~s/\s+/ /;
> $address=~s/\s+/ /;
> $lline=~s/\s+/ /;
> print "$fname $lname\n";
>
>
> print "$address\n";
> print"$lline\n";
> }
>
> Now each record as I calculated is of a fixed lenght of 75 chars.I
> need some idea as to how to run the Loop to print all such
> records.Right now I am only able to print the first record,


You could try something like this:

#!/usr/bin/perl
use warnings;
use strict;

my $file = '/users/meenaksharora/db.txt';

open my $fh, '<', $file or die "Cannot open '$file' $!";

while ( my $line = <$fh> ) {
my ( $fname, $lname, $address, $lline ) = unpack 'A10 A15 A25 A13',
$line;

print "$fname $lname\n$address\n$lline\n";
}

__END__



John
--
use Perl;
program
fulfillment
Jenda Krynicky

2007-12-18, 7:00 pm

From: "minky arora" <minky.arora@gmail.com>
> Here is my code:
> !/usr/bin/perl
>
> use strict;
> open FILE, "/users/meenaksharora/db.txt" or die"cnt open $!";
> my($fname,$lname,$address,$lline,$line,@
db);
> foreach my $line(<FILE> ){
> $fname=substr($line,0,10);
> $lname=substr($line,10,15);
> $address=substr($line,16,25);
>
> $lline=substr($line,59,13);


my ($fname,$lname,$address,$lline) = ($line =~
/^(.{10})(.{15})(.{25})(.{13})/);


> $fname=~s/\s+/ /;
> $lname=~s/\s+/ /;
> $address=~s/\s+/ /;
> $lline=~s/\s+/ /;


for ($fname,$lname,$address,$lline) {
s/\s+/ /;
# you sure? Replace the first group of spaces by a single one?
}

> print "$fname $lname\n";
> print "$address\n";
> print"$lline\n";
> }
>
> Now each record as I calculated is of a fixed lenght of 75 chars.I
> need some idea as to how to run the Loop to print all such
> records.Right now I am only able to print the first record,


If the file doesn't contain lines, but fixed length records you
should either use read() or set

$/ = \75;

see
perldoc -f read
and
perldoc perlvar

If you modify $/ you should do so for the smallest part of the code
possible ... I would rather use read() myself.

Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com