foreach in my

T

thecento

Hi Folks,

I have the following block of code (at bottom of message).

The part that is throwing an error is:

foreach my $record (@$a_records) {
print " {
'content' => $record->{host},
'name' => $record->{name},
'type' => 'A'
},\n";
};

syntax error at ./Zoneparse.pl line 72, near "foreach "
Execution of ./Zoneparse.pl aborted due to compilation errors.

I assume because it's inside the my $req section.

Two questions:
1. Any idea why I get this error?
2. Will the print return the data to the $req?

Thanks!

Code:

my $req = {
'protocol' => 'TPP',
'object' => 'order',
'version' => '1.4.0',
'action' => 'create',
'attributes' => {
'contacts' => [
{
'id' => $user_contact,
}
],
'create_items' => [
{
'product_data' => {
'period' => '1',
'pool' => {
'name' => 'default',
},
'zone' => {
'records' => [
foreach my $record (@$a_records) {
print " {
'content' => $record->{host},
'name' => $record->{name},
'type' => 'A'
},\n";
};
],
'name' => $zone
}
},
'object_type' => 'managed',
'contact_set' => {
'admin' => '0',
'tech' => '0',
'billing' => '0'
},
'service' => 'dns',
'order_item_type' => 'new',
}
],
'handling' => 'process'
}
};
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g14g2000cwa.googlegroups.com:
I have the following block of code (at bottom of message).

Having looked at that, I cannot fathom what on G-d's green-brown-blue
planet you are trying to accomplish.

....
I assume because it's inside the my $req section.
....

Two questions:
1. Any idea why I get this error?

Because it is not valid Perl syntax.
2. Will the print return the data to the $req?

What does this even mean? I am serious. I cannot begin to imagine to
think that I might one day possibly understand what you mean by:
'records' => [

So, $req->{records} is a reference to an array
foreach my $record (@$a_records) {
print " {
'content' => $record->{host},
'name' => $record->{name},
'type' => 'A'
},\n";
};
],

But you put a for loop in the array?

Are you, by any chance, looking for

perldoc -f map

If not, then please post a short but complete script which others can
run without modification, and which still exhibits your problem.

Please do read the posting guidelines for this group for information on
how to help yourself and help others help you.

Sinan
 
C

Cento

I have the following block of code (at bottom of message).
Having looked at that, I cannot fathom what on G-d's green-brown-blue
planet you are trying to accomplish.

I realize it's different :)
Because it is not valid Perl syntax.

All of the syntax gets passed to a custom parser which converts it to
XML.
2. Will the print return the data to the $req?

What does this even mean? I am serious. I cannot begin to imagine to
think that I might one day possibly understand what you mean by:
'records' => [

That's part of the data that gets passed to the custom parser.
So, $req->{records} is a reference to an array
foreach my $record (@$a_records) {
print " {
'content' => $record->{host},
'name' => $record->{name},
'type' => 'A'
},\n";
};
],

But you put a for loop in the array?

This is straight from ZoneParser.pm docs. This executes:

foreach my $record (@$mx_records) {
print " {
'content' => $record->{host},
'name' => $record->{name},
'priority' => $record->{priority},
'type' => 'MX'
},\n";

And prints what I want. This output has to be part of the $req data.
However when I use this inside $req I get that error.
Are you, by any chance, looking for

perldoc -f map

It doesn't look like it...
If not, then please post a short but complete script which others can
run without modification, and which still exhibits your problem.

It would require installing and configuring a custom application
(pitb!).
Please do read the posting guidelines for this group for information on
how to help yourself and help others help you.

Sorry, will do.
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g14g2000cwa.googlegroups.com:
my $req = { ....
'zone' => {
'records' => [ ],
....
};

Untested:

for my $record (@$a_records) {
push @{ $req->{zone}->{records} }, {
content => $record->{host},
name => $record->{name},
type => 'A',
}
}

Implementing this using map is left as an exercise.

You really need to go back to basics, and learn some Perl first, instead
of copying and pasting code willy-nilly.

Sinan
 
J

Juha Laiho

print will print out data to your standard output, as it always does.
Just like
print "Foo";
nothing less, nothing more. Ok, print does have a return value - what
it is you can find out from perl documentation - but trust me, you
wouldn't use that return value to build the array.
'records' => [
foreach my $record (@$a_records) {
print " {
'content' => $record->{host},
'name' => $record->{name},
'type' => 'A'
},\n";
};
],


So, $req->{records} is a reference to an array ....
But you put a for loop in the array?

Read again the above, beause it is the key to your problem.
Whatever you put inside the "[ ... ]" construct above, should
be an array. And a code block is not an array.
This is straight from ZoneParser.pm docs. This executes:

foreach my $record (@$mx_records) {
print " {
'content' => $record->{host},
'name' => $record->{name},
'priority' => $record->{priority},
'type' => 'MX'
},\n";

Apparently yes, because you're not trying to tell perl that the piece of
code is an array.
It would require installing and configuring a custom application
(pitb!).

Apparently not. You're having an issue with understanding basic perl
syntax. A minimal, self-contained example could be written, which
only addresses the perl issue at hand - for which no modules or custom
applications are needed. By solving that example, also a large part
of your problem with this larger piece of software would be solved.
 
C

Cento

print will print out data to your standard output, as it always does.
Just like
print "Foo";
nothing less, nothing more. Ok, print does have a return value - what
it is you can find out from perl documentation - but trust me, you
wouldn't use that return value to build the array.

You're on the money with what I'm trying to do :)
'records' => [
foreach my $record (@$a_records) {
print " {
'content' => $record->{host},
'name' => $record->{name},
'type' => 'A'
},\n";
};
],


So, $req->{records} is a reference to an array ...
But you put a for loop in the array?

Read again the above, beause it is the key to your problem.
Whatever you put inside the "[ ... ]" construct above, should
be an array. And a code block is not an array.

It was my intention to populate the array with the data 'printed'.

For example, that array should look like:

'records' => [
{
'priority' => '5',
'content' => 'mail.somedmn.com.',
'name' => 'www',
'type' => 'MX'
},
{
'priority' => '10',
'content' => 'mail.somedmn.com.',
'name' => 'www',
'type' => 'MX'
},
],

This was one of my original questions - would print populate the array.
Apparently yes, because you're not trying to tell perl that the piece of
code is an array.

This is definitely my problem, however not my intention. I was trying
to have that code executing to populat the array.

Apparently not. You're having an issue with understanding basic perl
syntax. A minimal, self-contained example could be written, which
only addresses the perl issue at hand - for which no modules or custom
applications are needed. By solving that example, also a large part
of your problem with this larger piece of software would be solved.

Thanks Wolf!
 
J

Joe Smith

The part that is throwing an error is:
foreach my $record (@$a_records) {
print " {
'content' => $record->{host},
'name' => $record->{name},
'type' => 'A'
},\n";
};

If you copy-and-paste that to a separate script, it compiles OK.
This, however, fails with a syntax error near "foreach ":

[
foreach my $record (@ARGV) {print "$record\n";}
]

Try

$r=$req->{attributes}{create_items}[0]{product_data}{zone}{records};
push @$r,{content=>$_->{host},name=$_->{name},type=>'A'} foreach
@$a_records;

to populate the 'records' array after creating the structure.

Or, pre-calculate a temporary array and use a reference to that
array when building the structure.
-Joe
 
A

Anno Siegel

Joe Smith said:
The part that is throwing an error is:
foreach my $record (@$a_records) {
print " {
'content' => $record->{host},
'name' => $record->{name},
'type' => 'A'
},\n";
};

If you copy-and-paste that to a separate script, it compiles OK.
This, however, fails with a syntax error near "foreach ":

[
foreach my $record (@ARGV) {print "$record\n";}
]

Try

$r=$req->{attributes}{create_items}[0]{product_data}{zone}{records};
push @$r,{content=>$_->{host},name=$_->{name},type=>'A'} foreach
^
There's a ">" missing
@$a_records;

to populate the 'records' array after creating the structure.

Well, whatever the OP says, this *is* a case for map:

$r = [ map {
content => $_->{host},
name => $_->{name},
type=>'A',
}, @$a_records
];

Anno
 
C

Cento

If you copy-and-paste that to a separate script, it compiles OK.
This, however, fails with a syntax error near "foreach ":

[
foreach my $record (@ARGV) {print "$record\n";}
]
Yep.

Try

$r=$req->{attributes}{create_items}[0]{product_data}{zone}{records};
push @$r,{content=>$_->{host},name=$_->{name},type=>'A'} foreach
@$a_records;

to populate the 'records' array after creating the structure.

Thank you! This was the key. I was not sure how to populate the data,
so I was using print - duh. I appreciate this very very much, I've
never spent too much time with perl, but I can generally find my way
around.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top