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:
Help Needed to read selective data into array

 



zihao
Novice

Jan 23, 2004, 11:29 AM

Post #1 of 11 (3512 views)
Help Needed to read selective data into array Can't Post

 
@values = split(/&/, $ENV{'QUERY_STRING'});

##I have 2 variables assign to the above array.
####data in the csv file in "field1|field2|field3" format

I need help to make use of this variable to do the following:
1. Search a csv text file on the server for the first line that contain $value[0]
2. Then verify on that line that the word after the 3rd "|" is the same as $value[1],if cant find, exit program
3. If step1 and 2 is sucessful,extract that particular line of data and split them into "newvalue" array.
eg. @newvalue=split('\|', $data[0]);

My problem is I dont know how to extract the certain line of data instead of the whole chunk from the file and the part to do the search and matching to find line number.
Anyone can help me pls. Just started perl a few days ago.


davorg
Thaumaturge / Moderator

Jan 23, 2004, 2:26 PM

Post #2 of 11 (3509 views)
Re: [zihao] Help Needed to read selective data into array [In reply to] Can't Post

I think you need something lke this:

Code
open FILE, '/path/to/your/file' or die $!; 

my @newvalue;
while (<FILE>) {
if (/$value[0]/) {
my @data = split /\|/;
if ($data[3] eq $value[1]) {
@newvalue = @data;
last;
}
}
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


zihao
Novice

Jan 23, 2004, 10:08 PM

Post #3 of 11 (3507 views)
Re: [davorg] Help Needed to read selective data into array [In reply to] Can't Post

HI. thanks alot for the reply. But how to get that line that contains that variable? Each line consists of 45 variablesm field separated by "|"

and each line is a record to a person data. There are about 100 people. I need to search thru and get that line of data by ensuring the 2 variables read in by html

earlier equals to the 3rd and 7th field on that line. But the code you provided, does it do that?


davorg
Thaumaturge / Moderator

Jan 24, 2004, 2:05 AM

Post #4 of 11 (3504 views)
Re: [zihao] Help Needed to read selective data into array [In reply to] Can't Post

The code does exactly what you asked for. It searches the file for the first row that contains $value[0] anywhere on the line and $value[1] in the 4th data item. When it finds a line like that, it copies the values in @newvalues and stops searching.

If you need to do something different, then it should be simple for you to make the required changes to the code.

If you don't understand any piece of the code then please ask for clarification. It's always a very bad idea to use a piece of code that you don't understand.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


zihao
Novice

Jan 24, 2004, 7:04 AM

Post #5 of 11 (3503 views)
Re: [davorg] Help Needed to read selective data into array [In reply to] Can't Post

Thanks alot. Well, I've just started perl a few days ago. Cant say that I am a newbie. Perhaps I am worse than that. I must thank you for helping me out again. I will look thru it and try. But is it possible to read that line of values into html text box and then once the user click save, allow those values to replace back on the same line again(Including those that the user has made changes)?


zihao
Novice

Jan 24, 2004, 7:59 AM

Post #6 of 11 (3501 views)
Re: [davorg] Help Needed to read selective data into array [In reply to] Can't Post

Yap, it worked like magic. Thanks. After I got the datas I wanted into the data fields(on a form). Wonder if you can enlighten me on another code to save that data(from the text field in a form) into the same line replacing that line and not adding to it? Thanks.


davorg
Thaumaturge / Moderator

Jan 24, 2004, 9:17 AM

Post #7 of 11 (3500 views)
Re: [zihao] Help Needed to read selective data into array [In reply to] Can't Post

1/ Read the new data items from the form (best to use CGI.pm for this).

2/ Open the data file for reading and take a shared lock on it.

3/ Open new file for writing and take an exclusive lock on it.

4/ Read each line in turn from the data file and check to see if it is the record that you are editing.

5/ If it is the line that you are editing then write the new data to the new file. Otherwise write the existing data to the new file.

6/ Close both filehandles.

7/ Rename the new file to the name of the old file (thereby overwriting the old file).

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


zihao
Novice

Jan 24, 2004, 11:44 AM

Post #8 of 11 (3497 views)
Re: [davorg] Help Needed to read selective data into array [In reply to] Can't Post

Hi, Thanks again for replying. Does it mean that it is difficult to replace certain line by collected data over html?


davorg
Thaumaturge / Moderator

Jan 24, 2004, 12:01 PM

Post #9 of 11 (3496 views)
Re: [zihao] Help Needed to read selective data into array [In reply to] Can't Post

No. Not difficult at all - just follow the instructions I gave in my last message.

Of course, it would be far easier if you stored the data in a database :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


zihao
Novice

Jan 24, 2004, 12:13 PM

Post #10 of 11 (3495 views)
Re: [davorg] Help Needed to read selective data into array [In reply to] Can't Post

I think I get your message. But I was thinking about the following...wonder how it works?

http://forums.devshed.com/t80154/s.html

They did not state/highlight which string is the one that I am suppose to "add in my search value and the replace value"


zihao
Novice

Jan 24, 2004, 1:18 PM

Post #11 of 11 (3494 views)
Re: [davorg] Help Needed to read selective data into array [In reply to] Can't Post

my $LineNumber=$FORM{LineNum};
open(IN, $base_file) || error("Can't open file $base_file!");
open(OUT, ">$base_file2") || error("Can't open file $base_file2!");
while(<IN>){
if(/$data[0]/){}
else{}
}
close OUT;
close IN;



I got the above script and I got the LineNumber to replace ready as above. But what should I do next?

 
 


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

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