Gossamer Forum
Home : General : Perl Programming :

modifying several records

Quote Reply
modifying several records
i have some code that will manipulate some fields in a record by looking up data in a separate db when i modify a single record. it works fine. i'm trying to do the same thing when i am modifying several records at once. what i want is -- as it processes each record to modify, it does my routine to replace certain fields before it checks that all the fields are ok.

this is my code when i modify a single record -- it works:

Code:
sub modify_record {
my ($status, $line, @lines, @data, $output, $found, %rec, $key, $num_files, @files, $file, $file_test);
my ($message, $col, $i);

&before_modify_record;
$status = &validate_record;
blah blah blah
}

sub before_modify_record {
# --------------------------------------------------------
# populates some exhibit fields from the venue db

&switch_to_venue("venues");
%rec2 = &get_record2($in{'Venue'});
$in{'Exhibit_open'} = $rec2{'Exhibit_open'};
$in{'Exhibit_close'} = $rec2{'Exhibit_close'};
$in{'Venue_name'} = $rec2{'Venue_name'};
$in{'Exhibit'} = $rec2{'Venue_name'} . ' ' . $rec2{'Exhibit_open'} . ' - ' . $rec2{'Exhibit_close'};
}

this is what i'm trying to do when i have multiple records. the modification of multiple records works fine except when i try to call my sub to get the venue stuff

Code:
sub modify_mult_record {
# --------------------------------------------------------
# This routine will update multiple records at once. It expects
# to find in %in a series of records to update. They will be of the
# form field_name-key.
#
my ($key, %modify_list, %modify_rec, $rec_to_modify, @data, $key,
$errstr, $succstr, $output, %errors, $message);

# First let's pick which records to modify and then separate them and store
# them in their own hashes.
$rec_to_modify = 0;
foreach $key (keys %in) { # Build a hash of keys to modify.
if ($in{$key} eq "modify") {
$modify_list{$key} = 1;
$rec_to_modify = 1;
}
($key =~ /^(.*)-(.+)$/) and (${$modify_rec{$2}}{$1} = $in{$key});
}
# Choke if we don't have anything to do.
$rec_to_modify or (&html_modify_failure("no records specified.") and return);

open (DB, "<$db_file_name") or &cgierr("error in modify_records. unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB>) {
(/^#/) and ($output .= $_ and next LINE);
(/^\s*$/) and next LINE;
chomp;
@data = &split_decode($_);
$key = $data[$db_key_pos];

# Now we check if this record is something we want to modify. If so, then
# we make sure the new record is ok, if so we replace it.
if ($modify_list{$key}) {

@data = &before_modify_multi_record(@data); #### here's the problem, i've tried several different things

$status = &validate_multiple_records(%{$modify_rec{$key}});
blah blah blah
}

sub before_modify_multi_record { # 02/01/2011
# --------------------------------------------------------
# populates some exhibit fields from the venue db

my (@record) = @_;
&switch_to_venue("venues");
%rec2 = &get_record2($record[23]);
$record[3] = $rec2{'Exhibit_open'};
$record[4] = $rec2{'Exhibit_close'};
$record[2] = $rec2{'Venue_name'};
$record[30] = $rec2{'Venue_name'} . ' ' . $rec2{'Exhibit_open'} . ' - ' . $rec2{'Exhibit_close'};
return (@record);
}

i'm sure it's something simple, but i'm not too good passing stuff back and forth. thanks!
Quote Reply
Re: [delicia] modifying several records In reply to
Hi,

It looks like the problem with how you are returning the values

What happens if you "dump" the data before:

Code:
return (@record);

i.e do:

Code:
print $IN->header;
use Data::Dumper;
print Dumper(@record);

...what does the value of @record show?

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] modifying several records In reply to
@ record looks good:
$VAR1 = '1653'; $VAR2 = '1193'; etc -- and it has the correct values from my sub

now that i look at it, i don't think the next line in the main sub is looking at @data:

$status = &validate_multiple_records(%{$modify_rec{$key}});

so somehow i need to be working on %{$modify_rec{$key}} ??

Last edited by:

delicia: Feb 1, 2011, 9:59 AM
Quote Reply
Re: [delicia] modifying several records In reply to
Mmm ok, and what happens if you do similar code after:

Code:
@data = &before_modify_multi_record(@data); #### here's the problem, i've tried several different things

(so you can see the values from @data)

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] modifying several records In reply to
the two are identical


$status = &validate_multiple_records(%{$modify_rec{$key}});

so somehow i need to be working on %{$modify_rec{$key}} which has the fieldnames, or could i call my subroutine from the validate_multiple_records sub

Last edited by:

delicia: Feb 1, 2011, 10:36 AM
Quote Reply
Re: [delicia] modifying several records In reply to
Hi,

Mmm sorry, not sure I can be much more help then :( I've not really got a huge amount of experience with DBMan, so don't really know the ins-and-outs of how it all works.

Maybe someone @ GT could shed a bit more light Smile

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] modifying several records In reply to
don't think GT will be any help because this is part of a major hack of dbman that i got off this board long ago - modify multiple records. i've added a hook to do something before i modify a single record. now i want to be able to use the hook before i modify multiple records. if i can just figure out how to pass my record to my sub and back, it should work.

i added right before the validate call:

Code:
&html_page_top;
print %{$modify_rec{$key}};
print "<br>";
print $modify_rec{$key};
it prints:
Exhibit_close28-Feb-2011RecordID1653CityLawrencevilleStateGASize20 x 26Zip30044
HASH(0x6d02c4)

i removed most of fields above; but i counted the fields and it only lists 22 of them and they're not in the same order as the db.
i'm guessing that $modify_rec{$key} is HASH(0x6d02c4) - is that right?

how can i pass my key or whatever i need to do to retrieve the record i want and make the change?
Quote Reply
Re: [delicia] modifying several records In reply to
ok, i figured it out:

call to sub:

%test = &before_modify_multi_record(%{$modify_rec{$key}});

sub:
my (%rec3) = @_;

i kept having a problem and thought this was the cause. however, finally traced my problem to something else so this was actually not complicated at all. all seems to be working now. if you see something that looks wrong, pls let me know!
Quote Reply
Re: [delicia] modifying several records In reply to
Aaah ok, yeah that looks ok. As long as it works =)

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!