Ask a Question related to PERL Beginners, Design and Development.
-
Support #1
Writing to file
Hi all
I have a little bit of code you may be able to help with. I load a file
into an array with
foreach $lines(@database_array) {
@edit_array = split(/\:/,$lines);
push(@member_array,[@edit_array]);
$x++;
} #loop
and then play around with the array a bit, then I would like to load it
back into the file with
while($count<$x){
$newline = join("\:",$member_array[$count],[@edit_array]);
chomp($newline);
print DAT "$newline\n";
$count++;
}
but I can not retrieve the data into $newline with join. Is this bit wrong
$member_array[$count],[@edit_array])
or am I missing the point completely or is there another way of doing it.
I am currently loading direct from the file working on that line and them
loading back to file, but I can only change one line at a time. Doing it
with as above I can alter the entire array and then load back to file.
Any help is appreciated and please excuse if I'm in the wrong forum.
Cheers
Colin
-----------------------------------------------------------------------------------------------
[url]www.rentmyplace.co.nz[/url]
The ultimate in that holiday spot away from the maddening crowd
Join as a member today its FREE
List your holiday accommodation for FREE
------------------------------------------------------------------------------------------------
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus ([url]http://www.grisoft.com[/url]).
Version: 7.0.211 / Virus Database: 261.8.1 - Release Date: 30/01/2004
Support Guest
-
Writing a text file to the file system
Using Visual Studio C# When I ran the following code: System.IO; private void Button1_Click(object sender, System.EventArgs e) {... -
Writing in to a xml file
Can i write a text content in the swf to a xml file. Can i use the same xml file for authentifications -
Setting the file permissions of a file I'm writing to
Is it possible to specify the permissions of a file I create when I: open ("FOO", "> ./bar") or die ("Could not create file"); Thanks in... -
File Access error - writing to .txt file
Normally web sites run under the ASPNET user account. It appears that this account does not have write privileges to the file path you've... -
A failure occurred writing to the resources file. Access is denied. -- RESX file is locked? -- WHY?
Hi. This is an error that comes up fairly regularly when trying to run the "Rebuild All" command in a Solution that contains more than one... -
Rob Hanson #2
RE: Writing to file
> but I can not retrieve the data into $newline with join. Is this bit wrong
Yes, it is a little off.
This *isn't* an array, it is an *array reference*> $member_array[$count]
Same with this.> [@edit_array]
Join takes a list of scalars, not a list of references. So you need to
dereference them. I think this is what you are after.
join("\:",@{$member_array[$count]});
Rob
-----Original Message-----
From: Support [mailto:support@emlgroup.co.nz]
Sent: Sunday, February 01, 2004 2:31 PM
To: [email]beginners@perl.org[/email]
Subject: Writing to file
Hi all
I have a little bit of code you may be able to help with. I load a file into
an array with
foreach $lines(@database_array) {
****************@edit_array = split(/\:/,$lines);
****************push(@member_array,[@edit_array]);
****************$x++;
****************}** #loop
and then play around with the array a bit, then I would like to load it back
into the file with
while($count<$x){*******
$newline = join("\:",$member_array[$count],[@edit_array]);
chomp($newline);
print DAT "$newline\n";
$count++;
}
but I can not retrieve the data into $newline with join. Is this bit wrong
$member_array[$count],[@edit_array])
or am I missing the point completely or is there another way of doing it.
I am currently loading direct from the file working on that line and them
loading back to file, but I can only change one line at a time. Doing it
with as above I can alter the entire array and then load back to file.
Any help is appreciated and please excuse if I'm in the wrong forum.
Cheers
Colin
----------------------------------------------------------------------------
-------------------
[url]www.rentmyplace.co.nz[/url]
The ultimate in that holiday spot away from the maddening crowd
Join as a member today its FREE
List your holiday accommodation for FREE
----------------------------------------------------------------------------
--------------------
Rob Hanson Guest
-
Jeff 'Japhy' Pinyan #3
Re: Writing to file
On Feb 2, Support said:
First, it doesn't look like you're using 'strict', which means you're not>foreach $lines(@database_array) {
> @edit_array = split(/\:/,$lines);
> push(@member_array,[@edit_array]);
> $x++;
>} #loop
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;
I don't know WHAT'S going on here.>while($count<$x){
> $newline = join("\:",$member_array[$count],[@edit_array]);
I think your join() line should be> chomp($newline);
> print DAT "$newline\n";
> $count++;
>}
$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 [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
<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. ]
Jeff 'Japhy' Pinyan Guest
-
John W. Krahn #4
Re: Writing to file
Support wrote:
Hello,>
> Hi all
You should limit the scope of $lines to the foreach loop. Does $lines> I have a little bit of code you may be able to help with. I load a
> file into an array with
>
> foreach $lines(@database_array) {
contain multiple lines or just one line? Perhaps you should name it
$line instead.
foreach my $line ( @database_array ) {
You are assigning the list returned from split() to the array> @edit_array = split(/\:/,$lines);
> push(@member_array,[@edit_array]);
@edit_array and then copying that array to an anonymous array. If you
lexically scope @edit_array to the foreach loop you can push its
reference instead of copying it twice:
my @edit_array = split /\:/, $line;
push @member_array, \@edit_array;
Or you could just copy the list directly and avoid using a named array:
push @member_array, [ split /\:/, $line ];
Is $x used anywhere?> $x++;
Or you could declare and assign to @member_array using the map function:> } #loop
my @member_array = map [ split /\:/ ], @database_array;
Perhaps you want something like this:> and then play around with the array a bit, then I would like to load
> it back into the file with
>
> while($count<$x){
> $newline = join("\:",$member_array[$count],[@edit_array]);
> chomp($newline);
> print DAT "$newline\n";
> $count++;
> }
foreach my $line ( @member_array ) {
print DAT join ':', @$line;
}
Or with the map function:
print DAT map join( ':', @$_ ), @member_array;
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
R. Joseph Newton #5
Re: Writing to file
Support wrote:
See if that is closer to what you want.> Hi all
> I have a little bit of code you may be able to help with.
> I load a file into an array with
>
> foreach $lines(@database_array) {
> @edit_array = split(/\:/,$lines);
> push(@member_array,\@edit_array);
> $x++;
> } #loop
>
> and then play around with the array a bit, then I would
> like to load it back into the file with
>
> while($count<$x){
> $newline = join('\:',@{$member_array[$count]});
> chomp($newline);
> print DAT "$newline\n";
> $count++;
> }
Joseph
R. Joseph Newton Guest



Reply With Quote

