By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,775 Members | 1,740 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,775 IT Pros & Developers. It's quick & easy.

How to populate values taken from text file in PERL

P: 2
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:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. =comment
  4.     The script assigns gen# accounts to a tab separated list of names and 
  5.     id numbers provided at the bottom of the code under __DATA--
  6.  
  7.     The following few lines explains some of the necessary syntax to 
  8.     accomplish this task:
  9. =cut
  10.  
  11. #The first two lines of the data below the __DATA__ syntax can be read with the following:
  12.  
  13. $line = <DATA>;
  14. chomp $line;
  15. print "first line: $line\n";
  16. $line =  <DATA>; 
  17. chomp $line;
  18. print "second line: $line\n";
  19.  
  20. #The 'split'  function is useful for splitting a string from a given character:
  21. #In this case the username is being split from  the number
  22. #by specifying the tab in regexp notation (will learn later)  in the first argument 
  23. #of the split function:
  24.  
  25. ($name, $number) = split(/\t/, $line);
  26.  
  27. #You could use the following to read the whole list:
  28. #    the <DATA> command will keep reading one line at time until
  29. #    the internal pointer for <DATA> reads EOF, at which point, 
  30. #    the 'while' loop is excausted
  31.  
  32. while ($line = <DATA>) {
  33.         print "line is: $line";
  34. }
  35.  
  36. =comment
  37. to do:
  38.  
  39. modify the while loop above to include a 'split' function to
  40. separate the name from the number into a hash.
  41.  
  42. The hash should be  designed to store the id number by using
  43. the full name as key.
  44.  
  45. After populating the hash, save the keys (sorted) into an array. 
  46. The reason for saving the keys into an array is to help printing 
  47. a new list, sorted by name.
  48.  
  49. Print the hash data using a 'for' loop (use the loop index to generate the gen numbers) 
  50. and coordinating with the array to produce a sorted list in the following format:
  51.  
  52.     gen1|Croft, Amy|4853
  53.     gen2|Fitzgerald, Brandon|7465
  54.     gen3|Fong, Ji-Men|2365
  55.     gen4|Fung, Kei Y.|8563
  56.     etc.
  57.  
  58. Last important example to demonstrate concatenation:
  59.     $gen = "gen" . $i;    same as "gen3" if $i is equal to 3
  60.  
  61. Use the same technique to concatenate the data above, which is separated by 'pipes' (|)
  62.  
  63. =cut
  64.  
  65. __DATA__
  66. Fong, Ji-Men    2365
  67. Lwin, Pyi-Soe    2367
  68. Lanferman, Alexander    3425
  69. Wells, Shou    3669
  70. Sira, Flavio    3756
  71. Croft, Amy    4853
  72. Todorova, Elina    5978
  73. Han, Jason    6574
  74. Tang, Michael    6892
  75. Fitzgerald, Brandon    7465
  76. Stout, John    8397
  77. Fung, Kei    8563
  78. Hoang, Hop    8657
  79. Sundara, Rhama    9154
Sep 28 '08 #1
Share this Question
Share on Google+
11 Replies


KevinADC
Expert 2.5K+
P: 4,059
Did you have a question? Is that your school/class/course work you have posted?
Sep 28 '08 #2

Icecrack
Expert 100+
P: 174
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.
Sep 29 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Sep 29 '08 #4

Icecrack
Expert 100+
P: 174
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.
Sep 29 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
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

Expand|Select|Wrap|Line Numbers
  1. while(<DATA>){
  2.    print;
  3. }
  4. __DATA__
  5. these lines
  6. 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
Sep 29 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
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
Sep 29 '08 #7

P: 2
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.
Sep 29 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
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.
Sep 29 '08 #9

KevinADC
Expert 2.5K+
P: 4,059
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.
Sep 29 '08 #10

Icecrack
Expert 100+
P: 174
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.
Sep 29 '08 #11

numberwhun
Expert Mod 2.5K+
P: 3,503
Expand|Select|Wrap|Line Numbers
  1. while(<DATA>){
  2.    print;
  3. }
  4. __DATA__
  5. these lines
  6. 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
Sep 29 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.