Re: Writing to file

From: Jeff 'Japhy' Pinyan (japhy_at_perlmonk.org)
Date: 02/01/04


Date: Sun, 1 Feb 2004 17:40:53 -0500 (EST)
To: Support <support@emlgroup.co.nz>

On Feb 2, Support said:

>foreach $lines(@database_array) {
> @edit_array = split(/\:/,$lines);
> push(@member_array,[@edit_array]);
> $x++;
>} #loop

First, it doesn't look like you're using 'strict', which means you're not
declaring your variables. This is probably going to lead to trouble down
the line.

Second, colons don't need backslashing in regexes.

Third, why are you using a counter variable, $x? I don't think you're
going to need it (even though you use $count and $x later).

Fourth, WHY do your array names end in "_array"? I think that's a waste
of space and typing.

I'd write your code as:

  for my $line (@database) {
    my @edit = split /:/, $line;
    push @members, [@edit];
  }

But I'd probably use \@edit instead of [@edit]. This won't work in YOUR
code, because in your code, @edit_array isn't lexically scoped, so every
entry in @member_array would be the same array reference.

  for my $line (@database) {
    my @edit = split /:/, $line;
    push @members, \@edit;
  }

But I'd go even further, by using the default variable for the loop and
for split():

  for (@database) {
    my @edit = split /:/;
    push @members, \@edit;
  }

But I don't even NEED @edit:

  for (@database) {
    push @members, [ split /:/ ];
  }

And by this point, I'd probably just use map() which is like a for loop
with a return value:

  my @members = map [ split /:/ ], @database;

>while($count<$x){
> $newline = join("\:",$member_array[$count],[@edit_array]);

I don't know WHAT'S going on here.

> chomp($newline);
> print DAT "$newline\n";
> $count++;
>}

I think your join() line should be

  $line = join ":", @{ $members[$count] };

Since $members[$count] is an array reference, we need to DE-reference it
(with the @{ ... } syntax) to get the array out of it. Also, colons don't
need to be backslashed in strings.

But again, I'd do things differently. First, instead of a while loop with
a counter, I'd use another for loop.

  for (@members) {
    my $line = join ":", @$_;
    chomp $line;
    print DAT "$line\n";
  }

Notice that instead of @{ $members[$x] }, I'm doing @$_. This is because
$_ is each element of the array in turn (first it's $members[0], then it's
$members[1], etc.).

And I'm curious why you're chomp()ing the line... just to add a newline at
the end. I have a feeling you're creating @database like this:

  open FILE, ...;
  @database = <FILE>;
  close FILE;

If so, I'll ignore the whole "why are you slurping a file into an array?"
talk and just tell you that you can chomp the lines there:

  chomp(@database = <FILE>);

And then you don't need to chomp() them later:

  for (@members) {
    my $line = join ":", @$_;
    print DAT "$line\n";
  }

I might even do without $line:

  for (@members) {
    print DAT join(":", @$_), "\n";
  }

Then, since it looks like that, I might use an inverted for loop:

  print DAT join(":", @$_), "\n" for @members;

I request that, when you get your program working, you submit it to this
list for a critique.

-- 
Jeff "japhy" Pinyan      japhy@pobox.com      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


Relevant Pages

  • RE: Error 3021
    ... Kevin Backmann ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)
  • RE: Error 3021
    ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)
  • RE: Error 3021
    ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)
  • Re: Displaying a large amount of data quickly (VB6)
    ... >>> involving a loop of VB code would be too slow. ... but I'd sure be interested to know if that StringBuilder ... Array elements: 25000 ... Array construction: 17 ...
    (microsoft.public.vb.controls)
  • Re: No destructive modification of array in while loop [wasRe: Trim whitespace with cookbook...]
    ... You have not made any attempt at modifying an array. ... # chomp $file; # don't need the chomp because if the second regex ... Your for loop is inside your while loop. ...
    (comp.lang.perl.misc)