Colin Johnstone wrote:
>
> Gidday all,
Hello,
> Im having trouble reading and writing to this file, It's probably just
> something silly.
> Heres my code. I have set the permissions on the file to 775.
>
> <code>
> my ($filename);
>
> $filename =
> "/home/bdweb8083m/johnstonefamily.com/cgi-bin/hp_data/participants.txt";
You can declare and assign in one statement:
my $filename =
'/home/bdweb8083m/johnstonefamily.com/cgi-bin/hp_data/participants.txt';
> if( -e $filename ){
> print "Yeah - File exists<br>";
> }
>
> my @participants;
>
> open IN, "<$filename" || die( "Cannot Open: $!" );
The high precedence of || means that that in interpreted as:
open IN, ( "<$filename" || die( "Cannot Open: $!" ) );
Which is not what you want. You should use the low precedence or
instead:
open IN, "<$filename" or die "Cannot Open: $!";
Or use parenthesis for the open function:
open( IN, "<$filename" ) || die "Cannot Open: $!";
> while( my $line = <IN> ){
> chomp $line;
> push( @participants, $line );
> }
You can declare and assign to the array in one statement:
chomp( my @participants = <IN> );
> close IN;
>
> print "Num elements array participants = ".scalar( @participants )."<br>";
>
> my $outString;
> $outString = "";
> $outString .= $fields{'SelectCity'} . ",";
> $outString .= $fields{'Workshop1'} . ",";
> $outString .= $fields{'Workshop2'} . ",";
> $outString .= $fields{'Salutation'} . ",";
> $outString .= $fields{'FirstName'} . ",";
> $outString .= $fields{'LastName'} . ",";
> $outString .= $fields{'Title'} . ",";
> $outString .= $fields{'CompanyName'} . ",";
> $outString .= $fields{'CompanyAddress'} . ",";
> $outString .= $fields{'Suburb'} . ",";
> $outString .= $fields{'State'} . ",";
> $outString .= $fields{'PostCode'} . ",";
> $outString .= $fields{'PhoneNumber'} . ",";
> $outString .= $fields{'Mobile'} . ",";
> $outString .= $fields{'EmailAddress'};
It looks like you need a join here:
my $outString = join ',',
@fields{ qw( SelectCity Workshop1 Workshop2 Salutation FirstName
LastName Title CompanyName CompanyAddress Suburb State
PostCode PhoneNumber Mobile EmailAddress ) };
> print "Out string =$outString<br>";
>
> push( @participants, $outString );
>
> print "Num elements array participants = ".scalar( @participants )."<br>";
>
> print "@participants\n\n";
>
> open( OUTFILE, '>$filename') or die( "Cannot open file: $!");
> while( @participants ){
> my $val = shift( @participants );
> print( OUTFILE "$val\n" );
> }
A foreach loop is more efficient then shifting each element from an
array:
foreach( @participants ) {
print OUTFILE "$_\n" ;
}
> close( OUTFILE ) or die( "Cannot close file: $!");
> </code>
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]