Looping over an array to build a multidimensional array in Perl, converting from PHP, and syntactically wrong -
Work on converting an array into another array. In PHP, this is easy, but there are some syntax of Pearl that I have a hard time understanding and around my head.
This is My Loop in Perl:
Magnify My RR (@Tindex). ############## Indexed {@ $ r [0]} {'image_id'} = @ $ r [0]; #brackon% indexed {"@ $ r [0]"} {'image_id'} = @ $ r [0]; #broken}
Here's my @toindex array
$ var1 = [[3,638,584, 'aperture', 'F13'], [3,638,588, Exposure bias ',' 0 EV '], [3638588,' focal length ', '80.0mm'],];
And what I want to do here, but in PHP
foreach ($ K == $ indexrows as gt; $ v) {$ Indexed [$ V ['image_id']] ['image_id'] = $ v ['image_id']; }
It looks very simple in PHP, but it is proving to be a big challenge for me to take it to Pearl.
Update
With the help of Sinan Umore and DVK, with that last less pointers, I have the solution to work. I am posting the full script, if any To be some useful in the future. Use warnings; Use DBI; Use data: Dumper; My $ dbh = dbi- & gt; Connect ('dbi: pg: db name = database; host = server address', 'user', 'password') or die; My $ sth; My $ sql = "SELECT id, field, data from the table"; My $ offset = 0; My $ increase = 20; My $ toindex; # This loop is to solve a problem here that was not part of the # original question. I query a database of looping # to describe the syntax to include it {$ sth = $ dbh- & gt; ($. $ "Offset" $ increments "offset" $ offset ...); $ Sth- & gt; Execution or die; $ Toindex = $ sth- & gt; Fetchall_arrayref; $ Offset = $ offset + $ increase; } While (@ $ toindex == 0); # Alternatively, if you do not need the loop given below, you get # $ sth = $ dbh- & gt; Ready ($ sql) is required; # $ Sth- & gt; Execution or die; # $ Toindex = $ sth-> gt; Fetchall_arrayref; My% indexed; Give my foreign currency for R $ ($ $ tandex) {#print Dumper $ r; Mine ($ id, $ field, $ value) = @ $ r; @ {$ Indexed {$ id}} {('image_id', $ field)} = ($ id, $ value); } Print dumper% indexed; $ Dbh- & gt; Disconnected;
I speculate that you change this information to a hash table Are trying to get indexed by image identifier:
#! Use / usr / bin / perl strict; Use warnings; My $ table = [[3638584 = & gt; 'Aperture', 'F13'], [3638588 = & gt; 'Exposure bias', '0 EV'], [3638588 = & gt; 'Focal length', '80.0mm '],]; My% indexed; For my $ R (@ $ table) {@ {$ indexed {$ R- & gt; [0]}} {('image_id', $ R-> [1])} = @ $ R [0,2]; } Use YAML; Print dump \% indexed;
Output:
E: \ Home> T --- 3638584: Aperture: F13 image_ID: 3638584 3638588: Exposure bias: 0 EV focal length: 80.0 mm Image_ID: 3638588
You can write
for loop as less mysteriously:
My $ R (@ $ table) ) {My ($ id, $ field, $ value) = @ $ r; @ {$ Indexed {$ id}} {('image_id', $ field)} = ($ id, $ value); }
That can save a lot of headaches for a week.
Also see. Pearl comes with excellent documentation; Use it.
Comments
Post a Comment