PERL Help...
Results 1 to 5 of 5

Thread: PERL Help...

  1. #1
    Banned
    Join Date
    Oct 2001
    Posts
    1,463

    PERL Help...

    Hmmm, Im trying to learn a few programming languages for the summer and im starting out with PERL because.
    1) Someone in IRC reccommended it to me and
    2) Its a good language to start on.

    Anyway, on to my problem... In the tutorial on PERL that I am reading I have to code a program to open a file, print the contents into an array and the print out the contents of that file so that every line has a prefix of #... The code I have so far is:
    Code:
    #!/usr/bin/perl
    #
    #
    $file = '/root/perl/food';
    open(INFO, $file);
    @lines = <INFO>;
    close(INFO);
    print ( "#", @lines);
    But when I run the file the text from the /root/perl/food file doesnt show at all, and when it did show it only the first line had the prefix of # (I understand that !#/usr/bin/perl already has a #, before it had 2)

  2. #2
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    Ok, think about what you did for a minute...last line of the script...

    you issued a print...

    what is @lines ? an array...so what would that translate to ?

    print ("#", "@lines[0], @lines[1], ... @lines[n]");

    see the problem yet ?

    try :

    foreach (@lines)
    {
    print ("#", "$_");
    }
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  3. #3
    Banned
    Join Date
    Oct 2001
    Posts
    1,463
    Ohhh! I just got it!!!! Thanks!

    An array is a group of sclar variables that have to be accessed by $array[1]
    Thanks!

  4. #4
    Banned
    Join Date
    Oct 2001
    Posts
    1,463
    Ok, Right now I have
    Code:
    #!/usr/bin/perl
    #
    #
    $file = '/root/perl/food';
    open(INFO, $file);
    @lines = <INFO>;
    close(INFO)
    foreach (@lines)
    {
    print ( "#", "$_");
    }
    But I get no output...... Any ideas?

  5. #5
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    /root/perl/food
    is blank or doesn't exist ? Works when I run it ...]

    Neb

    PS, probably a typo on your part ...but you don't have a semicolon after the close (INFO)
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides