Expanding scalar hash element in array ref.

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--; # ;-)
 
J

John Bokma

Dmitri said:
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


Why not push the value *always*? If there is just one element, it's
single value. You make it yourself hard two times, while storing the
values (you have to check), and when retrieving (another check).
 
D

Dmitry Roslyakov

perldoc -f ref

....
if (ref($conf->{$var}) eq 'ARRAY') {
# You have an array reference
}
....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top