Gossamer Forum
Home : General : Perl Programming :

extracting from an array

Quote Reply
extracting from an array
I want to take out a line from a file, edit it, and then place it back.
But how can I do this? I just want to extract the one line.
Is there any way to delete a line from a file while you are extracting it out?
So I can just save it at the end afterwards?
Without having to use the whole file, all the lines I mean?
This is how the form will be, which I will edit the line with:

Code:

<input type=\"text\" name=\"number\" value=\"$number\"> <input type=\"text\" name=\"name\" value=\"$name\">


The lines in the file are like this:
number|name|url|rating and more.

Thanks for any help.

- perlman
Quote Reply
Re: [perlman] extracting from an array In reply to
the easiest way to do that is to call open() twice, once for your database for reading, and the second for writing the data back to another file. then run a while loop on the incoming filehandle. for any line that doesn't match your criteria (ie, $id !~ /^$id\|/) print the line to the outgoing filehandle.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] extracting from an array In reply to
I'm not really sure what you mean.
If it wont be much trouble to you, can you show an example to me?
I'm using flat-file database.

Thanks.

- perlman
Quote Reply
Re: [perlman] extracting from an array In reply to
This should be an good enough example (hopefully);

Code:
# this is the ID number you want to look for
my $ID = $IN->param('ID');

# now loop through
open (WRITE,">file2.txt") || die $!;
open(READ,"file.txt") || die "Error: $!";
while (<READ>) {
my @cut = split /\|/, $_;
if ($cut[0] == $ID) {
# do whatever you want to this entry now...
} else {
print WRITE $_;
}
}
close(READ);
close(WRITE);

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] extracting from an array In reply to
except you don't want to waste time and resources spliting the line into an array if all you're gong to do is delete the line...

Code:
print WRITE $_ unless (/^$ID\|/);

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] extracting from an array In reply to
I don't just want to delete the line. I thought I had to do that to change it: extracting it out from the array and therefor deleting it, and changing it, and putting it back.

Thank you Andy for replying with the code. I am trying to use it, but I'm not sure what to do.
This is what I am testing so far:

Code:
#!D:/perl/bin/perl
# this is the ID number you want to look for
# my $ID = $IN->param('ID');
print "Content-type:text/html\n\n";
my $ID = 2; # Just chose a number.
# now loop through
open (WRITE,">data3.db") || die $!;
open(READ,"data.db") || die "Error: $!";
while (<READ>) {
my @cut = split /\|/, $_;
if ($cut[0] == $ID) {
foreach $element (@cut) {
print "<input type=\"text\" name=\"field name\" value=\"$element\"> \n";

}
# do whatever you want to this entry now...
} else {
print WRITE $_ unless (/^$ID\|/);
}
}
close(READ);
close(WRITE);

I get this printed:
Code:
<input type="text" name="number" value="2">
<input type="text" name="number" value="Link number 2">
<input type="text" name="number" value="http://www.link2.com">
<input type="text" name="number" value="http://www.link2.com/pics/link.gif">
<input type="text" name="number" value="A link site.">
<input type="text" name="number" value="7"> # rating
<input type="text" name="number" value="39"> # number of clicks
<input type="text" name="number" value="Link"> #category
<input type="text" name="number" value="Saturday March 20 2004. 20:23:43
">

You see. This is what I had as nr. 2 in the db file.

How can I get the names on the field to match, just print each field by writing them in the file and set the value for each? I could do that.

But how do I now add this back to the file? The line is still in the file?

Thanks for the help so far.

- perlman