Out of Memory - ActivePerl 5.6/Win98 SE 192MB RAM 
Author Message
 Out of Memory - ActivePerl 5.6/Win98 SE 192MB RAM

Getting out of memory error. When I comment out the OUTFILE print loop, no
out of memory, but of course, I have to print the results! Here's the code.

#!c:/perl/bin/perl -w

use strict;

my $infile = "c:/prog/acct001.txt";
my $outfile = "c:/prog/acct002.txt";
open(INFILE, "$infile");

my $flag = 0;
my ($name, $addr1, $city, $state, $zip, $phone, $fax, $email, $spec, $desc)
= "";
while (<INFILE>) {
 my $line = $_;
 if (($line !~ /[a-z]/) && ($line =~ /[A-Z]/) && ($line !~ /\d/)) {
  if ($flag == 6) {

$state, zip => $zip, phone => $phone, fax => $fax, email => $email, spec =>
$spec, desc => $desc });
   ($name, $addr1, $city, $state, $zip, $phone, $fax, $email, $spec, $desc)
= "";
  }
  $name .= $line;
  $flag = 1;
  next;
 }
 elsif ($flag == 1) {
  unless (($line !~ /[a-z]/) && ($line =~ /[A-Z]/) && ($line =~ /\d/)) {
   $addr1 .= $line;
   next;
  }
  else {
   $line =~ /(.*)\, (.*) (\d*)/;
   $city = $1; $state = $2; $zip = $3;
   $flag = 2;
   next;
  }
 }
 elsif ($flag == 2) {
  if ($line =~ /Telephone: \((...)\) (...)\-(....)/) {
   $phone = $1.$2.$3;
  }
  $flag = 3;
 }
 elsif ($flag == 3) {
  if ($line =~ /FAX: \((...)\) (...)\-(....)/) {
   $fax = $1.$2.$3;
  }
  $flag = 4;
 }
 elsif ($flag == 4) {
  $email = $1 if ($line =~ /E-mail\: (.*)/);
  $flag = 5;
 }
 elsif ($flag == 5) {
  if ($line ne "\n") {
   $spec .= $line;
  }
  elsif ($line eq "\n") {
   $flag = 6;
   next;
  }
 }
 elsif ($flag == 6) {
  unless ($line eq "\n") {
   $desc .= $line;
   next;
  }
 }

Quote:
}

close INFILE;
open(OUTFILE, ">$outfile");

 chomp ($dir[$i]{name}, $dir[$i]{addr1}, $dir[$i]{city}, $dir[$i]{state},
$dir[$i]{zip}, $dir[$i]{phone}, $dir[$i]{fax}, $dir[$i]{email},
$dir[$i]{spec}, $dir[$i]{desc});
 print
"$dir[$i]{name}\t$dir[$i]{addr1}\t$dir[$i]{city}\t$dir[$i]{state}\t$dir[$i]{
zip}\t$dir[$i]{phone}\t$dir[$i]{fax}\t$dir[$i]{email}\t$dir[$i]{spec}\t$dir[
$i]{desc}\n";
Quote:
}

close OUTFILE;

Thanks for any assistance!

Gabe



Wed, 18 Jun 1902 08:00:00 GMT  
 Out of Memory - ActivePerl 5.6/Win98 SE 192MB RAM

Gabe


Quote:
> Getting out of memory error. When I comment out the OUTFILE print loop, no
> out of memory, but of course, I have to print the results! Here's the
code.

> #!c:/perl/bin/perl -w

> use strict;

> my $infile = "c:/prog/acct001.txt";
> my $outfile = "c:/prog/acct002.txt";
> open(INFILE, "$infile");

> my $flag = 0;
> my ($name, $addr1, $city, $state, $zip, $phone, $fax, $email, $spec,
$desc)
> = "";
> while (<INFILE>) {
>  my $line = $_;
>  if (($line !~ /[a-z]/) && ($line =~ /[A-Z]/) && ($line !~ /\d/)) {
>   if ($flag == 6) {

> $state, zip => $zip, phone => $phone, fax => $fax, email => $email, spec
=>
> $spec, desc => $desc });
>    ($name, $addr1, $city, $state, $zip, $phone, $fax, $email, $spec,
$desc)
> = "";
>   }
>   $name .= $line;
>   $flag = 1;
>   next;
>  }
>  elsif ($flag == 1) {
>   unless (($line !~ /[a-z]/) && ($line =~ /[A-Z]/) && ($line =~ /\d/)) {
>    $addr1 .= $line;
>    next;
>   }
>   else {
>    $line =~ /(.*)\, (.*) (\d*)/;
>    $city = $1; $state = $2; $zip = $3;
>    $flag = 2;
>    next;
>   }
>  }
>  elsif ($flag == 2) {
>   if ($line =~ /Telephone: \((...)\) (...)\-(....)/) {
>    $phone = $1.$2.$3;
>   }
>   $flag = 3;
>  }
>  elsif ($flag == 3) {
>   if ($line =~ /FAX: \((...)\) (...)\-(....)/) {
>    $fax = $1.$2.$3;
>   }
>   $flag = 4;
>  }
>  elsif ($flag == 4) {
>   $email = $1 if ($line =~ /E-mail\: (.*)/);
>   $flag = 5;
>  }
>  elsif ($flag == 5) {
>   if ($line ne "\n") {
>    $spec .= $line;
>   }
>   elsif ($line eq "\n") {
>    $flag = 6;
>    next;
>   }
>  }
>  elsif ($flag == 6) {
>   unless ($line eq "\n") {
>    $desc .= $line;
>    next;
>   }
>  }
> }
> close INFILE;
> open(OUTFILE, ">$outfile");

>  chomp ($dir[$i]{name}, $dir[$i]{addr1}, $dir[$i]{city}, $dir[$i]{state},
> $dir[$i]{zip}, $dir[$i]{phone}, $dir[$i]{fax}, $dir[$i]{email},
> $dir[$i]{spec}, $dir[$i]{desc});
>  print

"$dir[$i]{name}\t$dir[$i]{addr1}\t$dir[$i]{city}\t$dir[$i]{state}\t$dir[$i]{
zip}\t$dir[$i]{phone}\t$dir[$i]{fax}\t$dir[$i]{email}\t$dir[$i]{spec}\t$dir[

- Show quoted text -

Quote:
> $i]{desc}\n";
> }
> close OUTFILE;

> Thanks for any assistance!

> Gabe



Wed, 18 Jun 1902 08:00:00 GMT  
 Out of Memory - ActivePerl 5.6/Win98 SE 192MB RAM

say such a terrible thing:

Quote:
>Getting out of memory error. When I comment out the OUTFILE print loop, no
>out of memory, but of course, I have to print the results! Here's the code.

Well your main problem is that you write Perl like a poor C programmer.
I really don't know what your data file format looks like although I'm
guessing it's something like this:

name
possibly address
city, state zip
telephone
fax
email
optional 'spec' lines
spec
spec
spec
...

optional 'desc' lines
desc
desc
desc
...

followed by the start of the next field.

You don't describe it that well so I'm guessing a bit here. I recommend
making your code look like your data, rather than having this kind of
warped around thinking with the flag variable and stuff. This way we can
get one field at a time without having to consider what state out code
is in at any time. This cuts down on the number of paths we have to
consider. You notice how my version goes through the while() loop once
only for each field, which makes it much simpler to understand what it
is trying to do.

Anyway, if you are running out of memory, that is because you are trying
to hold too much data in memory at one time. By reading all the fields

running out of memory. So what I suggest doing is reading in the data
one field at a time and then dealing with it. It's good that you 'use
strict' and enable warnings, however you also need to check for erros on
system calls (such as open()).

#!/usr/bin/perl -w
use strict;

my $infile = 'acct001.txt';
my $outfile = 'acct002.txt';

# Note checking for an error when opening the file
open INFILE, $infile or die "Cannot open $infile: $!";

open OUTFILE, ">$outfile" or die "Cannot open $outfile for writing: $!";

# input field seperator.
$/ = "\n\n";


while (<INFILE>) {

    chomp;


    # remove newlines

    my %input;



    unless (!m/[a-z]/ && m/[A-Z]/ && m/\d/)
    {
        $input{addr1} = $_;

    }



    $input{phone} = join '', m/Telephone: \((...)\) (...)-(....)/;


    $input{fax} = join '', m/FAX: \((...)\) (...)-(....)/;




    # read in the 'desc' field
    my $desc = <INFILE>;

    $input{desc} = join '', split /\n/, $desc;

    # print the data out to the file
    {
        no warnings 'uninitialized';

    }

Quote:
}

--

In the spider-web of facts, many a truth is {*filter*}d.
-Paul Eldridge


Fri, 11 Apr 2003 11:49:29 GMT  
 Out of Memory - ActivePerl 5.6/Win98 SE 192MB RAM

Quote:

> however you also need to check for erros on
> system calls (such as open()).

What are those system calls doing under the covers? ;-)

John



Wed, 18 Jun 1902 08:00:00 GMT  
 Out of Memory - ActivePerl 5.6/Win98 SE 192MB RAM

say such a terrible thing:

Quote:

>> however you also need to check for erros on
>> system calls (such as open()).

>What are those system calls doing under the covers? ;-)

Well I hope I don't have to explain what bad things can happen when you
mix touch, open, join, pipe, split, fork, creat(e), and sleep in the one
program.

--
Gwyn "not to mention hash" Judd

"Just kidding about that God part. No offense."
        -- Ivanova (after looking up), "A Voice in the Wilderness I"



Wed, 18 Jun 1902 08:00:00 GMT  
 Out of Memory - ActivePerl 5.6/Win98 SE 192MB RAM


...

Quote:
> while (<INFILE>) {

>     chomp;



That 'm' is just noise.

Quote:
>     # remove newlines


What newlines did you have in mind?  The split() removed every one
already.

...

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Wed, 18 Jun 1902 08:00:00 GMT  
 Out of Memory - ActivePerl 5.6/Win98 SE 192MB RAM

say such a terrible thing:

Quote:


>...

>> while (<INFILE>) {

>>     chomp;


>That 'm' is just noise.

>>     # remove newlines

>What newlines did you have in mind?  The split() removed every one
>already.

Yeah yeah I know I know. The program tested okay so I didn't find the


less typing and less chance of mistakes :)

--

The rule on staying alive as a forecaster is to give 'em a number or
give 'em a date, but never give 'em both at once.
                -- Jane Bryant Quinn



Sat, 12 Apr 2003 09:59:30 GMT  
 Out of Memory - ActivePerl 5.6/Win98 SE 192MB RAM

say such a terrible thing:

Quote:
>Yeah yeah I know I know. The program tested okay so I didn't find the


>less typing and less chance of mistakes :)

Interestingly, when I typed that, I thought I didn't have a reason for
it, but then when I tried it I found out I did, only when I typed it
that way, I didn't know I did. If you know what I mean :) Oh well two
unnecessary lines and one necessary line that I thought wasn't isn't too
bad considering everything.

--

The perfect lover is one who turns into a pizza at 4:00 A.M.
                -- Charles Pierce



Sat, 12 Apr 2003 10:16:59 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. ActivePerl 5.6 and ImageMagick

2. gd module for ActivePerl 5.6

3. XML::Parser under NT4.0 & ActivePerl 5.6

4. ActivePerl 5.6 GPF when use RE /\X/

5. ActivePerl 5.6 on NT 4.0/IIS4 not working

6. Installing ActivePerl 5.6 on Linux/Apache shared website

7. PPM not work on ActivePerl 5.6 for NT ?

8. embedded ActivePerl 5.6 GUI code demo?

9. Activeperl 5.6 wrong ascii in Windows98

10. installing ActivePerl 5.6

11. gd module for ActivePerl 5.6

12. ActivePerl 5.6 and mod_perl

 

 
Powered by phpBB® Forum Software