Gossamer Forum
Home : General : Perl Programming :

perl question

Quote Reply
perl question
i have no idea what

(${$modify_rec{$2}}{$1} = $in{$key});

is doing. please see code in dbman customization my last post.l

thanks!
Quote Reply
Re: [delicia] perl question In reply to
Do you have a bit more code around it? It looks like its just setting a multi dimensional hash, with a value passed in from STDIN ($in)

http://perlmaven.com/...i-dimensional-hashes

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] perl question In reply to
this is the entire sub

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); ### new line 20151219 ########################################################## my (%modify_list2); ######################################################### # 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); ### new line 20151219 %modify_list2 = %modify_list; #### 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}) { $status = &validate_record(%{$modify_rec{$key}}); if ($status eq "ok") { $output .= &join_encode(%{$modify_rec{$key}}); $modify_list{$key} = 0; } else { $errors{$key} = $status; $output .= "$_\n"; } } else { $output .= "$_\n"; } } close DB; # Reprint out the database. 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, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!"); } print DB $output; close DB; # automatically removes file lock # Let's display an error message if we were unable to modify a record # for some reason. foreach $key (keys %modify_list) { if ($modify_list{$key}) { ($errors{$key}) ? ($errstr .= "$key: $errors{$key}") : ($errstr .= "$key: not found"); } else { $succstr .= qq~<a href="$db_script_link_url&view_records=1&$db_key=$key&ww=1">$key</a>,~; } } chop($succstr); # Remove trailing delimeter ########## new lines 20151219 ############################################################# if ($db_after_modify_record) { my ($db2_file_name) = "$db_after_add_db"; ## i don't understand this but if it works for adding ## it should work here my ($output2); open (DB, "<$db2_file_name") or &cgierr("error in modify_records. unable to open db file: $db2_file_name.\nReason: $!"); if ($db_use_flock) { flock(DB, 1); } LINE: while (<DB>) { (/^#/) and ($output2 .= $_ and next LINE); (/^\s*$/) and next LINE; chomp; @data = &split_decode($_); $key = $data[$db_key_pos]; ##### 20151219 change this if key pos not same in db and db2!!! # 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_list2{$key}) { # not validating records in db2 $output2 .= &join_encode(%{$modify_rec{$key}}); $modify_list2{$key} = 0; } else { $output2 .= "$_\n"; } } close DB; # Reprint out the database. open (DB, ">$db2_file_name") or &cgierr("error in modify_records. unable to open db file: $db2_file_name.\nReason: $!"); if ($db_use_flock) { flock(DB, 2) or &cgierr("unable to get exclusive lock on $db2_file_name.\nReason: $!"); } print DB $output2; close DB; # automatically removes file lock } #################################################### &html_modify_mult_results($succstr, $errstr); }
Quote Reply
Re: [delicia] perl question In reply to
Can't read it like that , all on one line Whistle

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] perl question In reply to
ha ha . sorry, i should have previewed. i didn't know it would do that.

see http://www.gossamer-threads.com/forum/?do=post_attachment;postatt_id=2537

it has several subs but look at
sub modify_mult_record
Quote Reply
Re: [delicia] perl question In reply to
Hi,

Ok, so this bit:

Code:
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});
}


The part in red, is capturing the values of the field name... ie. foo-bar. Then, if it matches that - its setting ${$modify_rec{$2}}{$1} = $in{$key}

($1 and $2 are the captured values from the regex just before)

Hopefully that explains it Angelic

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] perl question In reply to
let me see if i understand.

$key is the field names because the form is capturing field names and values? so $in{$key} is the value of the field captured in the form?

but i don't understand what $2 and $1 are.
Quote Reply
Re: [delicia] perl question In reply to
Hi,


Code:
foreach $key (keys %in) {

$key comes from there. That is basically the value of the key. So if in %in, we had:

Code:
my %in = {
foo => "bar",
test => "something"
}

..then this code:

Code:
foreach $key (keys %in) {
print "KEY: $key \n"
}

..would print out:

Quote:
KEY foo
KEY bar

Quote:
but i don't understand what $2 and $1 are.

$2 and $1 are captured in the regex:

Code:
$key =~ /^(.*)-(.+)$/

If you have () wrapped around something in a regex, it will capture it to $1, $2, $3 etc (in the order they are captured). There is more here: http://stackoverflow.com/...-regular-expressions

In this case, you could kinda achieve the same thing with:

Code:
my @tmp = split $key "-";
if ($tmp[1]) {
${$modify_rec{$tmp[1]}}{$tmp[0]} = $in{$key}
}


Its just a bit more cumbersome :)

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!

Last edited by:

Andy: Dec 23, 2015, 8:11 AM
Quote Reply
Re: [Andy] perl question In reply to
thanks. i'm still pondering!