Page 1 of 1
PHP to PERL
Posted: Thu Jun 21, 2007 8:29 am
by chaser010
How will the following PHP code look like in PERL?
Code: Select all
foreach ($arrResults as $result)
{
print_r(explode('||', $result));
echo("<br />");
}
=======================
This is what I have, but its not working.
Code: Select all
@result_array = $result;
foreach $line ( @result_array )
{
$line2 = split('\|\|', $line);
print "$line2\n";
}
Posted: Thu Jun 21, 2007 12:15 pm
by volka
try
Code: Select all
use Data::Dumper;
@result_array = ('a||b', 'c||d||e', '||f');
foreach $line ( @result_array )
{
print Dumper(split('\|\|', $line));
print "<br />\n";
}
see also:
http://search.cpan.org/~ilyam/Data-Dump ... /Dumper.pm
Posted: Fri Jun 22, 2007 1:32 am
by chaser010
Thank you for your reply, but is doesn't work:
$VAR1 = 'ARRAY(0x8627d24)';
<br />
Posted: Fri Jun 22, 2007 4:57 am
by volka
Works fine for me.
$VAR1 = 'a';
$VAR2 = 'b';
<br />
$VAR1 = 'c';
$VAR2 = 'd';
$VAR3 = 'e';
<br />
$VAR1 = '';
$VAR2 = 'f';
<br />
Your @result_array probably isn't an array of strings. But how would I know.
Posted: Fri Jun 22, 2007 5:06 am
by chaser010
It returns a one-dimensional array. Each array element is a string.
I am using Soap::Lite to call a function and then get a result back that I am unable to read using Perl.
Posted: Fri Jun 22, 2007 5:19 am
by volka
chaser010 wrote:It returns a one-dimensional array. Each array element is a string.
But the array in my example also is an one-dimensional array of strings, no?
What does
print?
Posted: Fri Jun 22, 2007 5:30 am
by chaser010
Excellent!!
$VAR1 = [
'Result||-1',
'TransactionIndex||sdds-we-2334-dsf2-sdfdsf',
'AcquirerDateTime||2007/06/22 12:28:20 PM'
];
I will need to use some of the return results, any idea how to asign the values?
Posted: Fri Jun 22, 2007 5:59 am
by volka
ah, the square brackets indicate an array reference not an array (did I mention I never got comfortable with perl?)
Code: Select all
use Data::Dumper;
$a = [ # note the $a instead of @a
'Result||-1',
'TransactionIndex||sdds-we-2334-dsf2-sdfdsf',
'AcquirerDateTime||2007/06/22 12:28:20 PM'
];
foreach $l ( @$a ) { # note the @$a
print Dumper(split('\|\|', $l)), "\n";
}
Posted: Fri Jun 22, 2007 6:07 am
by chaser010
Thank you for all your help!
It works!!!!!
Posted: Fri Jun 22, 2007 6:51 am
by chaser010
One more thing.
If I just want the value of TransactionIndex and Result assigned to different variables?
At the moment the Dumper prints the following:
$VAR1 = 'Result';
$VAR2 = '-1';
$VAR1 = 'TransactionIndex';
$VAR2 = '929dwe-23djs-ds2dsd-234sddw';
Posted: Fri Jun 22, 2007 7:50 am
by volka
Code: Select all
use Data::Dumper;
$a = [ # note the $a instead of @a
'Result||-1',
'TransactionIndex||sdds-we-2334-dsf2-sdfdsf',
'AcquirerDateTime||2007/06/22 12:28:20 PM'
];
%hash = map { split('\|\|', $_) } @$a;
print 'Result: ', $hash{Result}, "\n";
print 'TI: ', $hash{TransactionIndex}, "\n";
ask me in a month and i won't understand the code

Posted: Fri Jun 22, 2007 8:04 am
by chaser010
Thank you!!
That did the trick!
