Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have created an array from input from users, this array is then stored. I want to create a file with the contents of the array and sent it to the users. I am able to sent on data to the user, but what syntax could I use to create a file from an array: e.g. foreach $element ( @DATA ) { #create a $file of these elements } mailx $file I also what the $file created to be stored in the current dir. any ideas? Thanks!

Replies are listed 'Best First'.
Re: create a file from an array
by Ovid (Cardinal) on Jun 24, 2002 at 16:20 UTC
    open FILE, "> somefile.txt" or die $!; print FILE join "\n", @array; close FILE;

    Or:

    { local $, = "\n"; open FILE, "> somefile.txt" or die $!; print FILE @array; close FILE; }

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: create a file from an array
by kvale (Monsignor) on Jun 24, 2002 at 16:20 UTC
    To write a file of your elements, first open the file. Then write each element using a print statement. Then close the file. Then use a system statement or a perl module to mail the file. If any of this sounds confusing, lookup the open, print, close, and system functions in your perl documentation.

    -Mark
Re: create a file from an array
by strat (Canon) on Jun 24, 2002 at 16:20 UTC
    my $outfile = "userfile.txt"; # or whatever .... unless (open (OUTFILE, ">$outfile")) { # open file for writing die "Error: couldn't write to file $outfile: $!"; } # unless else { foreach my $element (@DATA) { print (OUTFILE "$element\n"); } # foreach # or: print (OUTFILE "$_\n") foreach @DATA; # or: print (OUTFILE map { "$_\n" } @DATA); close (OUTFILE); } # else
    or shorter:
    open (OUTFILE, ">$outfile") or die "Error: couldn't write to file $outfile: $!"; print (OUTFILE "$_\n") foreach @DATA; close (OUTFILE);

    With mailing, I can't help you until I know which operating system you are working with. If you use Unix or Linux, have a look at sendmail, or if you are working with Windows, I prefer using Blat.

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"