How to populate values taken from text file in PERL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • doraima29
    New Member
    • Sep 2008
    • 2

    How to populate values taken from text file in PERL

    Hi,

    I am a newbie at PERL and really wanted to understand how server-side programming really works and operates since I use at the workplace. I use ASP and wanted to learn more about server-side programming since I am in the field of web design, development and multimedia.

    Here are some tips and need some help how to program:

    The way I learn is that I need an detailed explanation why and how we need use certain functions in PERL:

    I wonder if there is a website that shows source code and detailed explanations that is easy to understand.

    But anyway, I just need help how to program in PERL: Here are some tips and help on how to program:

    Code:
    #!/usr/bin/perl
    
    =comment
    	The script assigns gen# accounts to a tab separated list of names and 
    	id numbers provided at the bottom of the code under __DATA--
    
    	The following few lines explains some of the necessary syntax to 
    	accomplish this task:
    =cut
    
    #The first two lines of the data below the __DATA__ syntax can be read with the following:
    
    $line = <DATA>;
    chomp $line;
    print "first line: $line\n";
    $line =  <DATA>; 
    chomp $line;
    print "second line: $line\n";
    
    #The 'split'  function is useful for splitting a string from a given character:
    #In this case the username is being split from  the number
    #by specifying the tab in regexp notation (will learn later)  in the first argument 
    #of the split function:
    
    ($name, $number) = split(/\t/, $line);
    
    #You could use the following to read the whole list:
    #	the <DATA> command will keep reading one line at time until
    #	the internal pointer for <DATA> reads EOF, at which point, 
    #	the 'while' loop is excausted
    
    while ($line = <DATA>) {
            print "line is: $line";
    }
    
    =comment
    to do:
    
    modify the while loop above to include a 'split' function to
    separate the name from the number into a hash.
    
    The hash should be  designed to store the id number by using
    the full name as key.
    
    After populating the hash, save the keys (sorted) into an array. 
    The reason for saving the keys into an array is to help printing 
    a new list, sorted by name.
    
    Print the hash data using a 'for' loop (use the loop index to generate the gen numbers) 
    and coordinating with the array to produce a sorted list in the following format:
    
    	gen1|Croft, Amy|4853
    	gen2|Fitzgerald, Brandon|7465
    	gen3|Fong, Ji-Men|2365
    	gen4|Fung, Kei Y.|8563
    	etc.
    
    Last important example to demonstrate concatenation:
    	$gen = "gen" . $i;	same as "gen3" if $i is equal to 3
    
    Use the same technique to concatenate the data above, which is separated by 'pipes' (|)
    
    =cut
    
    __DATA__
    Fong, Ji-Men	2365
    Lwin, Pyi-Soe	2367
    Lanferman, Alexander	3425
    Wells, Shou	3669
    Sira, Flavio	3756
    Croft, Amy	4853
    Todorova, Elina	5978
    Han, Jason	6574
    Tang, Michael	6892
    Fitzgerald, Brandon	7465
    Stout, John	8397
    Fung, Kei	8563
    Hoang, Hop	8657
    Sundara, Rhama	9154
    Last edited by eWish; Sep 29 '08, 09:47 PM. Reason: Please use code tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Did you have a question? Is that your school/class/course work you have posted?

    Comment

    • Icecrack
      Recognized Expert New Member
      • Sep 2008
      • 174

      #3
      Originally posted by KevinADC
      Did you have a question? Is that your school/class/course work you have posted?
      i agree with KevinADC whats your question, and why post the howto

      i don't think any one will get what you need unless you ask.

      also have you tried to do this your self if so post some code, in code tags.



      note: his question is in his post title.
      Last edited by Icecrack; Sep 29 '08, 12:32 AM. Reason: Found question

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        The question may be in the title, but its a funny way to ask it. Usually its a good idea to reiterate the question in the body of the post.

        Also, I doubt this code would even work. They are referencing a file handle (<DATA>), yet there is no open statement anywhere in the code. Where do they expect the file handle to come from?

        Regards,

        Jeff

        Comment

        • Icecrack
          Recognized Expert New Member
          • Sep 2008
          • 174

          #5
          Originally posted by numberwhun
          The question may be in the title, but its a funny way to ask it. Usually its a good idea to reiterate the question in the body of the post.
          Yes i agree with that,


          and for his post i think his posted it from a website or something.

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Originally posted by numberwhun
            The question may be in the title, but its a funny way to ask it. Usually its a good idea to reiterate the question in the body of the post.

            Also, I doubt this code would even work. They are referencing a file handle (<DATA>), yet there is no open statement anywhere in the code. Where do they expect the file handle to come from?

            Regards,

            Jeff

            Code:
            while(<DATA>){
               print;
            }
            __DATA__
            these lines
            will be printed
            __DATA__ is a special token perl uses, <DATA> reads the text after __DATA__ in a perl script just like it would a regular file. For whatever reason it does not have to be opened before reading it. There is some discussion of its use here:

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              I wonder if there is a website that shows source code and detailed explanations that is easy to understand.
              There are many such websites, google will find them for you. Try searching for:

              perl beginner
              perl tutorial
              how to write perl programs
              beginning perl

              Comment

              • doraima29
                New Member
                • Sep 2008
                • 2

                #8
                I guess this is not the right place to ask questions for a newbie at PERL here.

                I'm trying to learn how to do a program that uses an external file such as a text that has names, ID numbers and their fictitious names. Also,I want to separate with | (pipes).

                I am just new programming with PERL.

                Do you know of websites that provide source code but at the same time has detailed explanations about it?

                It's pretty challenging to program when I am a designer and have a strong background in design but need to keep pace of technology and the challenges of being a web developer.

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  Originally posted by doraima29
                  I guess this is not the right place to ask questions for a newbie at PERL here.

                  I'm trying to learn how to do a program that uses an external file such as a text that has names, ID numbers and their fictitious names. Also,I want to separate with | (pipes).

                  I am just new programming with PERL.

                  Do you know of websites that provide source code but at the same time has detailed explanations about it?

                  It's pretty challenging to program when I am a designer and have a strong background in design but need to keep pace of technology and the challenges of being a web developer.
                  Read my previous post about using google. If you are new at perl you need to start from the beginning. The lesson you posted is several chapters into any beginners perl book.

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    Originally posted by doraima29
                    It's pretty challenging to program when I am a designer and have a strong background in design but need to keep pace of technology and the challenges of being a web developer.
                    Perl is hardly considered keeping pace with web/internet technologies. Its one of the oldest programming languages to be used on the internet.

                    PHP is the language most designers with no programming experience would want to look at.

                    You can ask beginner questions here but this forum, and no forum, is the hold-my-hand-and-teach-me-perl forum.

                    Comment

                    • Icecrack
                      Recognized Expert New Member
                      • Sep 2008
                      • 174

                      #11
                      Originally posted by doraima29
                      I guess this is not the right place to ask questions for a newbie at PERL here.

                      I'm trying to learn how to do a program that uses an external file such as a text that has names, ID numbers and their fictitious names. Also,I want to separate with | (pipes).

                      I am just new programming with PERL.

                      Do you know of websites that provide source code but at the same time has detailed explanations about it?

                      It's pretty challenging to program when I am a designer and have a strong background in design but need to keep pace of technology and the challenges of being a web developer.

                      Try Googling the above in Kevin's Post you will find lots of them


                      Sorry Forgot to refresh

                      Note: May be deleted.
                      Last edited by Icecrack; Sep 29 '08, 06:47 AM. Reason: forgot to refresh the page

                      Comment

                      • numberwhun
                        Recognized Expert Moderator Specialist
                        • May 2007
                        • 3467

                        #12
                        Originally posted by KevinADC
                        Code:
                        while(<DATA>){
                           print;
                        }
                        __DATA__
                        these lines
                        will be printed
                        __DATA__ is a special token perl uses, <DATA> reads the text after __DATA__ in a perl script just like it would a regular file. For whatever reason it does not have to be opened before reading it. There is some discussion of its use here:

                        http://perldoc.perl.org/SelfLoader.html
                        Wow, I did not know that. Nothing like a little light reading. Thanks for the quick lesson Kevin!

                        Regards,

                        Jeff

                        Comment

                        Working...