D
Dmitri Zakharov
Hello guys,
Is there any way in Perl to convert scalar hash element value to an
array reference with the original scalar as a first element of the
array. I just
want to convert the following PHP code into Perl:
<?php
while ($buf = fgets($fp, 1024) ) {
// ... parsing code here
if (array_key_exists($var, $conf)) {
if (!is_array($conf[$var])) {
$tmpval = $conf[$var]; // grab original value
$conf[$var] = array(); // convert to array
$conf[$var][] = $tmpval; // store original value as a first
element
}
$conf[$var][] = $val;
} else {
$conf[$var] = $val;
}
// ...
} // end while
?>
The idea here is that some variable can have multiple entries in conf
file, so
I want to store them in a 'bucket' under the same key. But most of the
entries
are simple 'var = value' pair, so I want to have flexibility to
process both.
In Perl there is a way to check for existence of 'key' with exists()
function
(which is equivalent to PHP array_key_exists()). BUT the problem I
encountered
in Perl is to convert the scalar value of the key to array reference.
<perl>
while ( <$fh> ) {
# parsing code here
if ( exists($conf->{$var}) ) {
# ??? There's nothing equivalent to PHP is_array() function in
Perl
# How do I know if value is still a scalar or an already an array
ref
# If I value is already an array ref, I would do
push @{$conf->{$var}}, $val;
} else {
# var encountered for the first time
$conf->{$var} = $val;
}
} # end while
</perl>
Is there anyway to do it? Any suggestion are appreciated
$dmitri--; # ;-)
Is there any way in Perl to convert scalar hash element value to an
array reference with the original scalar as a first element of the
array. I just
want to convert the following PHP code into Perl:
<?php
while ($buf = fgets($fp, 1024) ) {
// ... parsing code here
if (array_key_exists($var, $conf)) {
if (!is_array($conf[$var])) {
$tmpval = $conf[$var]; // grab original value
$conf[$var] = array(); // convert to array
$conf[$var][] = $tmpval; // store original value as a first
element
}
$conf[$var][] = $val;
} else {
$conf[$var] = $val;
}
// ...
} // end while
?>
The idea here is that some variable can have multiple entries in conf
file, so
I want to store them in a 'bucket' under the same key. But most of the
entries
are simple 'var = value' pair, so I want to have flexibility to
process both.
In Perl there is a way to check for existence of 'key' with exists()
function
(which is equivalent to PHP array_key_exists()). BUT the problem I
encountered
in Perl is to convert the scalar value of the key to array reference.
<perl>
while ( <$fh> ) {
# parsing code here
if ( exists($conf->{$var}) ) {
# ??? There's nothing equivalent to PHP is_array() function in
Perl
# How do I know if value is still a scalar or an already an array
ref
# If I value is already an array ref, I would do
push @{$conf->{$var}}, $val;
} else {
# var encountered for the first time
$conf->{$var} = $val;
}
} # end while
</perl>
Is there anyway to do it? Any suggestion are appreciated
$dmitri--; # ;-)