Iterating through a Perl array from an AJAX request

I am working on a Catalyst database project and am trying to execute some AJAX requests through jQuery. Parameters are sent OK, as seen in image 1.

List of parameters and values ​​received in AJAX request

Note that both the "diagnosis" and the "type_consents" (and their corresponding dates) are sent as an array of values ​​(value 1, value 2, ... value n).

Now for server-side processing Catalyst::Requestit makes it easy to retrieve data through $req->parameters, but it seems to work for me.

I do it like this:

my $params = $c->request->parameters; #Retrieving all parameters

my @type_consents         = $params->{type_consent};
my @date_consents         = $params->{date_consent};
my @diagnosis             = $params->{diagnosis};
my @date_diagnosis        = $params->{date_diagnosis};

Then I need to encode these arrays and insert for each pair of values (diagnosis|date , consent|date). In addition, I need to store and process all transactions and execute them all at once in a block eval(), so I do it as follows:

my %transactions;

# Diagnosis
my $diag_index = 0;

foreach my $key ( 0 .. $#diagnosis ) {
    $transactions{diagnosis}{$diag_index} = $diagnosis_mod->new(
        {
            subject_id          => $subject_id,
            diagnosis_date      => $date_diagnosis[$key],
            diagnosis           => $diagnosis[$key],
            diagnosis_comment   => "",
            suggested_treatment => ""
        }
    );

    print STDERR "\n" . $date_diagnosis[$diag_index];
    print STDERR "\n DEBUG: $date_diagnosis[$diag_index] | $diagnosis[$diag_index] | key: $diag_index";
    print STDERR "\n DEBUG2:" . Dumper( @date_diagnosis ) . " | " . Dumper( @diagnosis );

    $diag_index++;
}

# I'm avoiding evaluating and performing the transactions so neither eval() nor database impact are shown above.

:

Debugs of array iteration

, "" - ? , .

+4
1

, , . , $params->{type_consent} , . , (, , , , ..) , .

, $params->{type_consent}, , , .

, , my $type_consent = $params->{type_consent};, @$type_consent (, , ), - @$type_consent, %$params) , my @type_consent = @{$params->{type_consent}};.

, , , , , .

+1

Source: https://habr.com/ru/post/1667518/


All Articles