473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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:

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
11 3179
KevinADC
4,059 Expert 2GB
Did you have a question? Is that your school/class/course work you have posted?
Sep 28 '08 #2
Icecrack
174 Expert 100+
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
3,509 Expert Mod 2GB
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
174 Expert 100+
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
4,059 Expert 2GB
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
4,059 Expert 2GB
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
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
4,059 Expert 2GB
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
4,059 Expert 2GB
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
174 Expert 100+
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
3,509 Expert Mod 2GB
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

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

Similar topics

11
by: alex | last post by:
Hi, I am looking for a way to populate an HTML table from an external local text file which looks like this: DATE/TIME LAT. LON. DEPTH. ML....
5
by: Miguel Dias Moura | last post by:
Hello, i have an ASP.net / VB page with a Script (submitValues) and a Form. 1. The form has 20 input texts and a "Submit" button. 2. The script declares 20 variables. The value of each...
1
by: dibblm | last post by:
I have a text file being created and need the values from the array that gets created to populate my text file. I am new and not very good. Kinda borrowing bits and pieces from here and there, so...
6
by: Karthik | last post by:
I am trying to read Http request body values NOT form values The content type is text/xml and its POST I am posting data from Infopath to an asp.net page. In ASP it works by just writing...
16
by: Preben Randhol | last post by:
Hi A short newbie question. I would like to extract some values from a given text file directly into python variables. Can this be done simply by either standard library or other libraries? Some...
1
by: vj | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file? I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to...
0
by: vijendra | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file?I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to...
5
by: olaamussah | last post by:
Hi, i just started learning perl which i would use for my uni. project unfortunately. Well, this is a simple user login page i tried to create but i cant get it to work. Can someone please check this...
5
Kelicula
by: Kelicula | last post by:
Hello all. I have a problem which seems to make no sense to me. Therefore I must be doing something wrong. I am trying to populate an array using split with a regexe. Here is the code (snippet...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.