Currently I have this:
<?php
$fn = "file.txt";
$file = file_get_contents("./$fn");
$array = array($file);
?>
An example of whats in the text file:
array(1,4,3,2),array(3,2,1,2),array(5,6,7,8)
However when I print the array or even sizeof($array) it is empty. Whats the deal?
The content of your file is plain text not array data structures. You'll have to parse the content yourself. Here is one way of doing it:
$file_content = "array(1,4,3,2),array(3,2,1,2),array(5,6,7,8)";
$matches = [];
$arrays = [];
if (preg_match_all('~array\(((?:(?:\d+),?)*)\),?~', $file_content, $matches)) {
$arrays = array_map(function ($array) { return explode(',', $array); }, $matches[1]);
}
$arrays now contains arrays as you would expect and you can use count() to check their size.
If you know that your file always contain arrays another way to do it is by using eval.
Another approach
eval('$arrays2 = [' . $file_content . '];');
Now $arrays2 contains the same arrays. There is one subtle difference. The former approach does not cast the values to integers whereas eval does.
Warning: Only use eval() if are 100% sure that you know what goes into the function. Also note that this approach will fail on bad data, whereas the former is a little more resilient.
Related
I've tried two similar bits of syntax. This first one works:
my $obj = PI::something::ObjectManipulator->new();
$obj->setValue('HELLO');
my $objList = [$object];
This, however, doesn't:
my $objList= [];
foreach my $value (#values) {
my $obj = PI::something::ObjectManipulator->new();
$obj->setValue($value);
push #$objList, $obj;
};
What is the difference between these two way of doing things? Why doesn't the second work? (By not work, it seems to be that $objList is still empty at the end of the code.)
Assuming my $objList = [$object]; is a typo for my $objList = [$obj];, and assuming, my #values = 'HELLO';, there are no differences.
Assuming you meant "#$objList is still empty" when you said "$objList is still empty", that can only happen if the loop isn't entered (i.e. #values is empty).
I have simple XML that I want to read in Perl and make hash containing all read keys.
Consider this code:
my $content = $xml->XMLin("filesmap.xml")->{Item};
my %files = map { $_->{Path} => 1 } #$content;
This snippet works great when XML file contains many Item tags. Then $content is a reference to array. But when there is only one Item I get an error when dereferencing as array. My assumption is that $content is a reference to the scalar, not array.
What is the practice to make sure I get array of values read from XML?
What you need is to not use XML::Simple and then it's really trivial. My favourite for fairly straightforward XML samples is XML::Twig
use XML::Twig;
my $twig = XML::Twig -> new -> parsefile ( 'filesmap.xml' );
my #files = map { $_ -> trimmed_text } $twig -> get_xpath ( '//Path' );
With a more detailed XML sample (and desired result) I'll be able to give you a better answer.
Part of the problem with XML::Simple is it tries to turn an XML data structure into perl data structures, and - because hashes are key-values and unordered but arrays are ordered, it has to guess. And sometimes it does it wrong, and other times it's inconsistent.
If you want it to be consistent, you can set:
my $xml = XMLin( "filesmap.xml", ForceArray => 1, KeyAttr => [], ForceContent => 1 );
But really - XML::Simple is just a route to pain. Don't use it. If you don't like XML::Twig, try XML::LibXML instead.
What I would say you need is a flatten-ing step.
my %files
= map { $_->{Path} => 1 }
# flatten...
map { ref() eq 'ARRAY' ? #$_ : $_ }
$xml->XMLin("filesmap.xml")->{Item}
;
You can do a check and force the return into an array reference if necessary:
my $content = $xml->XMLin("filesmap.xml")->{Item};
$content = ref $content eq 'ARRAY'
? $content
: [$content];
I have this code segment to put together a hash of parameters which I will pass to a function. The hash value containing the IP address is supposed to be an array reference, but the function I'm passing my parameters to thinks it's a scalar reference.
My code is:
my $paramList = "ldap_ip_addresses=['192.168.1.100']|ldap_port=389|ldap_protocol=ldap";
my #paramTuples = split(/\|/, $paramList);
my %nasProps;
foreach my $paramTuple (#paramTuples) {
my($key, $val) = split(/=/, $paramTuple, 2);
# SetProperties can also take hashes or arrays
my $eval_val = eval $val;
if (ref($eval_val) =~ /ARRAY/) {
$val = \$eval_val;
}
$nasProps{$key} = $val;
}
From the debugger, my parameter hash looks like this:
DB<18> x \%nasProps
0 HASH(0x303f8f0)
'ldap_authentication_type' => 'anonymous'
'ldap_ip_addresses' => REF(0x303fa70)
-> ARRAY(0x8284eb8)
0 '192.168.1.100'
'ldap_port' => 389
'ldap_protocol' => 'ldap'
It looks like a reference to an array so I'm not sure where I'm going wrong.
Since $eval_val is already a reference to an array, there is no need to make a reference to the reference. Change:
$val = \$eval_val;
to:
$val = $eval_val;
You are unnecessarily taking the reference to a reference with
$val = \$eval_val;
You have established on the previous line that $eval_val is a reference to an array, so you can use it as it is without taking a reference to it again.
In addition, you should ignore the result of ref $eval_val except to check that it is true — i.e. $eval_val is a reference of some sort.
Your code should look more like this. You need to fall back to the original $val value only if eval returns undef, usually meaning that the string wasn't compilable code.
Note also that you should reserve capital letters for global Perl variables, such as package names. Lexical variable identifiers should contain only lower-case letters, decimal digits and underscores.
use strict;
use warnings;
my $param_list = "ldap_ip_addresses=['192.168.1.100']|ldap_port=389|ldap_protocol=ldap";
my #param_tuples = split /\|/, $param_list;
my %nas_props;
for my $param_tuple (#param_tuples) {
my ($key, $val) = split /=/, $param_tuple, 2;
$nas_props{$key} = eval($val) // $val;
}
use Data::Dump;
dd \%nas_props;
output
{
ldap_ip_addresses => ["192.168.1.100"],
ldap_port => 389,
ldap_protocol => "ldap",
}
Here is a short alternative in functional style:
my %nasProps =
map /\[/ ? eval : $_,
split /[|=]/, $paramList;
However, it only works if you can guarantee that = is not included in any parameter values.
I am working on a database project, where I want to create an array as follows:
unsigned char* drillSize[][10] = {
{"0.3678", "23.222", "MN", "89000", ".000236", "678", "ZX", "8563", "LX", "0.678"},
{"0.3678", "23.222", "MN", "89000", ".000236", "678", "ZX", "8563", "LX", "0.678"},
.
.
.
//around 6000 rows }
I have been provided with this data in an Microsoft Word file. If I were to key in the data manually it might take weeks; is there a way to insert commas and inverted commas for each element by some means?
You can try it with regular expressions.
If your data is structured in any way like a csv file you can just import it into your program in some way and then slice it up with basic string functions.
In php I'd
$input = file_get_contents($myfile) //lets say this contains a text: 1,2,3,4,5
$sliced = explode(",",$input);
$myarray = null; //our variable
$output = "\$myarray = new array("; //creating a template for an array as a String
//some logic with the $sliced array if you need
...
$output .= implode(",",$sliced); //we put it back as string
$output .= ");"; //close the array
eval($output); //after this its in php's scope
print_r($myarray);
Basically that's what you need in a more complex form. If the text is not that structured you might need some regular expression library for C, but I'd recommend creating a text in Python, Perl or something which has more support and more flexible then copy the code manually back to C.
I'm trying to put a hash %listvol into an array #fileInfo in Perl.
#fileInfo = ($filename, $data, $index, \%listvol);
%listvol contains a list of volume: key = $vol, value = $vol.
The first $vol values are ABCDEF, then GFFFF, EEEAA - always different.
Then I put the array #fileInfo in the hash %listeAllFile:
$listeAllFile{$nameOfFile} = [#fileInfo];
Later I'm trying to get the hash %listvol without success. I'm using this code:
foreach $key (keys %listeAllFile) {
#tab = #{ $listeAllFile{$key} };
$filename = $tab[0];
%listvol = %{ $tab[3] };
foreach $vol (keys %listvol) {
print "\n vol is $vol for file $filename";
}
The file name is always different, so it is ok. But the value of the variable $vol is always the same, ABCDEF. It seems that I get get each time the same value.
Does anyone have an idea?
While you didn't include code to reproduce your problem, I'm fairly sure that the issue is that you're storing a reference to the same %listvol hash in each array.
When you change the contents of %listvol for the second entry, you're modifying the first entry at the same time. One way to fix that is to use {%listvol} instead of \%listvol. The former makes a shallow copy of the current contents of %listvol, just like [#fileInfo] makes a shallow copy of the current contents of #fileInfo.