PHP to PERL

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
chaser010
Forum Newbie
Posts: 7
Joined: Thu Jun 21, 2007 8:20 am

PHP to PERL

Post 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";
   
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
chaser010
Forum Newbie
Posts: 7
Joined: Thu Jun 21, 2007 8:20 am

Post by chaser010 »

Thank you for your reply, but is doesn't work:

$VAR1 = 'ARRAY(0x8627d24)';
<br />
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
chaser010
Forum Newbie
Posts: 7
Joined: Thu Jun 21, 2007 8:20 am

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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

Code: Select all

print Dumper(@result_array);
print?
chaser010
Forum Newbie
Posts: 7
Joined: Thu Jun 21, 2007 8:20 am

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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";
}
chaser010
Forum Newbie
Posts: 7
Joined: Thu Jun 21, 2007 8:20 am

Post by chaser010 »

:D

Thank you for all your help!

It works!!!!!
chaser010
Forum Newbie
Posts: 7
Joined: Thu Jun 21, 2007 8:20 am

Post 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';
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
chaser010
Forum Newbie
Posts: 7
Joined: Thu Jun 21, 2007 8:20 am

Post by chaser010 »

Thank you!!

That did the trick! :lol:
Post Reply