CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
printing to a file

 



chakrijk
New User

Jul 30, 2010, 8:53 AM

Post #1 of 5 (336 views)
printing to a file Can't Post

Hi,

I am having trouble in printing $a_files/$b_files string into a file.

How can I do it? Please help.

Here is the code:


Code
my $a_files = " 
x0.pl
y0.pl
";

my $b_files = "
x1.pl
y1.pl
";

my @arr = qw(a b);

foreach my $ar (@arr) {
open(FL, ">$ar.files");
print FL, "$$ar_files\n";
close(FL);
}


Regards,
JK


BillKSmith
Veteran

Jul 31, 2010, 11:55 AM

Post #2 of 5 (325 views)
Re: [chakrijk] printing to a file [In reply to] Can't Post

You have several problems:

  • Always use use strict; and use warnings;.


  • You are atempting to use symbolic references. This is almost never a good idea and in fact is not allowed under strict; Use a hash to explicitly map file contents to file names


  • Always use the three argument form of open
  • .

  • Lexical form of file handles is prefered.


  • Always check open and close for errors. Yes many of us are careless about close.


  • When newlines and tabs are needed in a string, code them explicitly. I had to guess at what you meant.


  • I believe that this is what you intended.


    Code
    use strict; 
    use warnings;
    my $a_files =
    "\n"
    ."x0,pl\n"
    ."y0.pl\n"
    ;
    my $b_files =
    "\n"
    ."x1,pl\n"
    ."y1.pl\n"
    ;

    my %arr = (
    # $file_name => \$contents,
    a_files => \$a_files,
    b_files => \$b_files,
    );

    while ( my ($file_name, $contents) = each %arr) {
    open my $FH, '>', $file_name or die "Cannot open $file_name\n";
    print {$FH} "$contents\n";
    close $FH or die "Cannot close $file_name\n";
    }



    The function each is very convenient here, but it does prevent you from knowing the order the files will be created in. I doubt that this is a problem.
    Good Luck,
    Bill


    deepeshtronics
    Novice

    Aug 1, 2010, 5:44 AM

    Post #3 of 5 (313 views)
    Re: [BillKSmith] printing to a file [In reply to] Can't Post

    Hi BillKSmith,

    Your code may not generate the correct output because you have declared a hash with the following structure.


    Code
    my %arr = (  
    # $file_name => \$contents,
    a_files => \$a_files,
    b_files => \$b_files,
    );


    In place of references of scalar variable (\$a_files, \$b_files), you should have taken the scalar variables directly ($a_files, $b_files). Because at last while printing $contents you are not "de-referencing" them.

    Thanks


    chakrijk
    New User

    Aug 1, 2010, 8:49 AM

    Post #4 of 5 (308 views)
    Re: [BillKSmith] printing to a file [In reply to] Can't Post

    Thanks Bill, This code is working fine for me.


    Code
    use strict;  
    use warnings;

    my %ar = (
    a => "\n"
    ."x0.pl\n"
    ."y0.pl\n"
    ,
    b => "\n"
    ."x1.pl\n"
    ."y1.pl\n"
    ,
    );

    my @arr = qw(a b);

    foreach my $arr (@arr) {
    open(FL, ">$arr.files") or die "Can't open $arr.files\n";
    print FL $ar{$arr};
    close(FL) or die "Cannot close $arr.files\n";
    }


    Regards,
    JK


    stevenenjoy
    New User

    Aug 4, 2010, 12:53 AM

    Post #5 of 5 (286 views)
    Re: [chakrijk] printing to a file [In reply to] Can't Post

    I am trying to all the lines that has a ".c" extension using the following script,for some reason the the matching pattern "$_ =~ /^.*\.c\z/s"doesn't seem to work.Can some pls help?When i run this script nothing gets printed








    -----------------------------------------


    [url=http://www.dvdsetshop.com/products/Smallville-Seasons-1-9-DVD-Boxset-DVDS-1998.html]Smallville Seasons dvd box
    [url=http://www.dvdsetshop.com/products/The-Sopranos-Seasons-1-6-DVD-Boxset-DVDS-1304.html]The Sopranos Seasons dvd
    [url=http://www.dvdsetshop.com/products/Ally-McBeal-Seasons-1-5-DVD-Boxset-DVDS-1264.html] Ally McBeal Seasons DVD Box set

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives