Read MongoDB array into perl and walk data - arrays

I am trying to capture an array from my MongoDB database into my Perl script and read each element. This is something that I thought would be simple, but for some dumb reason it is kicking my rearend.
My MongoDB Document (in part)
"members" : [
"5713b2d46d210e51836de591",
"me",
"you",
"him",
"her"
],
Perl code
$document = $database -> get_collection('my_collection')->find_one({_id => $oid});
#members = $document->{'members'};
print Dumper #members;
foreach $member (#members)
{
print "member = $member\n";
}
exit;
Output I am getting:
$VAR1 = [
'5713b2d46d210e51836de591',
'me',
'you',
'him',
'her'
];
member = ARRAY(0x47fa398)
Looking at the last line I see that I am being passed a reference to the array instead of the values. So I tried accessing via $member[0] or $member[1] but that just returns the same ARRAY(0x*****).
PLEASE HELP, I am sure it is something stupid.
Thanks!
Steven

I'm not familiar with Mongo, but looking at the output, your #members array has one element - an array ref (as you suspected). Since Mongo is returning an arrayref, you're best to store that in a scalar and access it like so;
my $members = $document->{'members'};
print "second item returned is: ", $members->[1];
print "The complete contents:\n";
for my $item ( #$members ) {
print " ", $item;
}

Related

in_array() expects parameter 2 to be array, object given

i know this is similar with other question, but.
how if we get the data array from db and try to compare those data ?
$x = Video::where('kursus_id', $data)->pluck('slug');
$z = Video::where('kelas_id', $data_id_kelas)
->where('mapel_id', $data_id_mapel)
->pluck('slug');
foreach ($z as $value) {
if (in_array($value, $x)) {
} else {
echo $value.'<br>';
}
}
this throw me an error like in_array() expects parameter 2 to be array, object given.
but when i try to change $x and $z with
$x=["2"]
$z=["1","2","3"]
Thats work. and output is 1 & 3
i think when i use db and give it pluck, this will became the same output when i use regular array like ["1","2","3"].
please correct me if my opinion goes wrong. cz im in study. Thanks before mates
in_array() it 2nd param req. array and you are giving object that's why this error
to fix this you need to use toArray() function in laravel
$x = Video::where('kursus_id', $data)->pluck('slug')->toArray();
then you can use in_array($value, $x)

Directly access nested JSON data in perl?

I'm not familiar with hash/reference syntax with Perl and it makes my eyes hurt trying.
I have the following JSON:
{
"Arg":"Custom_Light state alias protocol",
"Results": [
{
"Name":"Custom_Light",
"Internals": { },
"Readings": {
"protocol": { "Value":"V3", "Time":"2017-01-14 18:49:18" },
"state": { "Value":"off", "Time":"2017-03-05 10:39:50" }
},
"Attributes": { "alias": "Kitchen light" }
} ],
"totalResultsReturned":1
}
How do I directly get the Reading > Protocol Value and Reading > state Value as well as the Attributes > Alias?
I am using the default JSON encoder/decoder and it works splendid. Using Dumper($json) I get all the JSON, but I have no clue how to directly access it without using foreach with all the arrays within arrays in this.
I have tried the following:
my $json = from_json( $readout, { utf8 => 1 } );
print "No. Entries:", scalar(keys($json)); #works, returns 3
my #results = %$json{Results};
Dumper(#results[1]); #I get the Results array
From here it already is ugly. What's that %$ doing there? I thought I could do something like print ${ $json->{'Results'}->[1] }{'Readings'}; but that leads me nowhere.
Give me wisdom. How do I access the Protocol value directly? How do I access the state value directly? And finally, how to get to the alias Attribute?
I don't know what I'm doing but I'm getting somewhere with my $test = %{${%$json{Results}}[0]}{Name}; #I get "Custom_Light", nice. Is this the way to go with a gazillion of weird % and $ just randomly thrown in?
You want
$json->{Results}[0]{Readings}{protocol}{Value}
$json->{Results}[0]{Readings}{state}{Value}
$json->{Results}[0]{Attributes}{alias}
However, since the Results item is an array, you are likely to want to iterate over all of its elements, although in this case there is only one element
I find it useful to extract one level of reference at a time into temporary variables. It would look like this
my $results = $json->{Results};
for my $result ( #$results ) {
my $readings = $result->{Readings};
my $attributes = $result->{Attributes};
printf "Protocol: %s\n", $readings->{protocol}{Value};
printf "State: %s\n", $readings->{state}{Value};
printf "Alias: %s\n", $attributes->{alias};
print "\n";
}
Have a look at perlreftut, perldsc, and perlref, it will help you understand how to access deeply nested structures in Perl.
print "No. Entries:", scalar(keys($json)); #works, returns 3
Actually, this will no longer work. Using keys on a scalar, was an experimental feature added in Perl 5.14 that allowed each, keys, push, pop, shift, splice, unshift, and values to be called with a scalar argument. This experiment was considered unsuccessful, and was removed in 5.23. See also Experimental values on scalar is now forbidden. So, you should dereference the hash reference $json before applying keys:
print "No. Entries:", scalar keys %$json;
As described in perlref, %$ref dereferences the hash reference $ref. Next, lets look at this line:
my #results = %$json{Results};
This actually first creates a new (anonymous) hash ( Result => $json->{Result} ) and then assigns this to #results making #result = ( 'Result', $json->{Result} ). So that is why you now can refer to $json->{Result}[0] as $result[1].
But this is obscure coding, and probably not intended as well. So to return to your question, to get the Value field you could write:
my $value = $json->{Results}[0]{Readings}{state}{Value};
And to get the alias field:
my $alias = $json->{Results}[0]{Attributes}{alias};

Associative array - PERL

I need to know the way of creating the Associative array in Perl.
Basically now I have the code which is implemented as follows:
my $getEmployeeListOfTeamQuery = "SELECT profiles.userid
FROM user_group_map,profiles
WHERE user_group_map.group_id = $teamId
AND profiles.userid = user_group_map.user_id
AND profiles.active = 'y'
AND profiles.login_name NOT LIKE 'qa_%'
AND profiles.disabledtext = ''
GROUP BY profiles.login_name
ORDER BY profiles.login_name";
my $getEmployeeListOfTeam = $dbh->prepare($getEmployeeListOfTeamQuery);
$getEmployeeListOfTeam -> execute();
my #techs = ();
while(my ($tech) - $getEmployeeListOfTeam->fetchrow_array) {
push #techs,$tech;
}
So above code will be having the query in $getEmployeeListOfTeamQuery, Created the array names as techs.
Then tried pushing the values in to the the array.
Here it is working fine.
My question here is regarding the creation of associative array.
That is I need to query as follows : "SELECT profiles.userid, profiles,username....."
Hence I need to create a Associative array with "userid" as key and "username" as value.
I worry about the resources that you are using to learn Perl. Perl programmers haven't used the term "associative array" since Perl 5 was released over twenty years ago. We now call these structures "hashes". If you're learning from resources that use the term "associative array", then they are almost certainly horribly outdated.
But, to answer your question. The code is pretty simple.
my $sql = 'select profiles.userid, profiles.username...';
my $sth = $dbh->prepare($sql);
$sth->execute;
my %techs_hash;
while (my #row = $sth->fetchrow_array) {
$techs_hash{$row[0]} = $row[1];
}
Use selectcol_arrayref() and the Columns attribute:
my $aref = $dbh->selectcol_arrayref('select userid, login_name ...', {Columns => [1, 2]});
my %hash = #$aref;
you can fetch all rows into hash of hashes by one call to selectall_hashref:
my $tech=$dbh->selectall_hashref("select profile.userid, profiles.username, ...",'userid');
or you can fetch all rows into array of hashes with selectall_arrayref with attribute {Slice=>{}} :
my $tech=$dbh->selectall_arrayref("select profile.userid, profiles.username, ...",{Slice=>{}});
and then turn it into desired hash (this is exactly what you want):
my $result;
$result->{$_->{userid}}=$_->{username} foreach #$tech;
my #l;
while(my $h = $sth->fetchrow_hashref){
push #l,$h;
}
my %hash;
map { $hash{$_->{userid}} = $_->{username} } #l;

Json creation in ruby for a list

I am new to Ruby.
I want to create a JSON file for a group of elements.
For this, I am using eachfunction to retrieve the datas. I want to create json as follows for the 4 length array,
'{
"desc":{
"1":"1st Value",
"2":"2nd value"
"3":"3rd Value",
"4":"4th value"
},
}'
This is my array iteration,
REXML::XPath.each( doc, "//time" ) { |element1|
puts element1.get_text
}
I know here is the simple code to generate a JSON,
require 'json/add/core'
class Item < Struct.new(:id, :name); end
chair = Item.new(1, 'chair')
puts JSON.pretty_generate(chair)
This syntax will generate a json as follows,
{
"json_class": "Item",
"v": [
1,
"chair"
]
}
But I'm not sure how to do that to make JSON for my elements as stated above. Google search didn't give me a proper way to do this.
Can anyone help me here?
it means this?:
require 'json'
my_arr= ["1st Value","2nd Value","3rd Value","4th Value"]
tmp_str= {}
tmp_str["desc"] = {}
my_arr.each do |x|
tmp_str["desc"]["#{x[0]}"] = x
end
puts JSON.generate(tmp_str)
you can iterate the string array ,then take the strings to hash object.JSON can easy to parse Hash objcect .

Hash in array in a hash

I'm trying to identify the output of Data::Dumper, it produces the output below when used on a hash in some code I'm trying to modify:
print Dumper(\%unholy_horror);
$VAR1 = {
'stream_details' => [
{
'file_path' => '../../../../tools/test_data/',
'test_file' => 'test_file_name'
}
]
};
Is this a hash inside an array inside a hash? If not what is it? and what is the syntax to access the "file path" and "test_file" keys, and their values.
I want to iterate over that inner hash like below, how would I do that?
while ( ($key, $value) = each %hash )
{
print "key: $key, value: $hash{$key}\n";
}
You're correct. It's a hash in an array in a hash.
my %top;
$top{'stream_details'}[0]{'file_path'} = '../../../../tools/test_data/';
$top{'stream_details'}[0]{'test_file'} = 'test_file_name';
print Dumper \%top;
You can access the elements as above, or iterate with 3 levels of for loop - assuming you want to iterate the whole thing.
foreach my $topkey ( keys %top ) {
print "$topkey\n";
foreach my $element ( #{$top{$topkey}} ) {
foreach my $subkey ( keys %$element ) {
print "$subkey = ",$element->{$subkey},"\n";
}
}
}
I would add - sometimes you get some quite odd seeming hash topologies as a result of parsing XML or JSON. It may be worth looking to see if that's what's happening, because 'working' with the parsed object might be easier.
The above might be the result of:
#JSON
{"stream_details":[{"file_path":"../../../../tools/test_data/","test_file":"test_file_name"}]}
Or something similar from an API. (I think it's unlikely to be XML, since XML doesn't implicitly have 'arrays' in the way JSON does).

Resources