foreach in my

Discussion in 'Perl Misc' started by thecento@gmail.com, May 18, 2005.

  1. Guest

    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'
    }
    };
     
    , May 18, 2005
    #1
    1. Advertising

  2. wrote in news:1116434444.944503.138760
    @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
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 18, 2005
    #2
    1. Advertising

  3. Cento Guest

    > > 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 :)

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

    >
    > 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.
     
    Cento, May 18, 2005
    #3
  4. "Cento" <> wrote in news:1116436608.611355.94170
    @g47g2000cwa.googlegroups.com:

    >> 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!).


    No it would not.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 18, 2005
    #4
  5. wrote in news:1116434444.944503.138760
    @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
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 18, 2005
    #5
  6. Juha Laiho Guest

    "Cento" <> said:
    >"A. Sinan Unur" <> said:
    >> wrote in news:1116434444.944503.138760
    >> @g14g2000cwa.googlegroups.com:


    >> > 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?


    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.

    >> 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!).


    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.
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
     
    Juha Laiho, May 18, 2005
    #6
  7. Cento Guest

    > 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.

    > >
    > > 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.


    This is definitely my problem, however not my intention. I was trying
    to have that code executing to populat the 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.


    Thanks Wolf!
     
    Cento, May 18, 2005
    #7
  8. Joe Smith Guest

    wrote:
    > 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
     
    Joe Smith, May 19, 2005
    #8
  9. Anno Siegel Guest

    Joe Smith <> wrote in comp.lang.perl.misc:
    > wrote:
    > > 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
     
    Anno Siegel, May 19, 2005
    #9
  10. Cento Guest

    > 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.
     
    Cento, May 19, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: foreach loop error

    Kevin Spencer, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    364
    Kevin Spencer
    Aug 22, 2003
  2. Luc Kumps

    Re: foreach loop error

    Luc Kumps, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    382
    Luc Kumps
    Aug 22, 2003
  3. =?Utf-8?B?SnVzdGlu?=

    Problem with foreach loop

    =?Utf-8?B?SnVzdGlu?=, Oct 22, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    326
    Hans Kesting
    Oct 22, 2004
  4. Islam Elkhayat

    loop througth Literials using foreach!!

    Islam Elkhayat, Feb 10, 2005, in forum: ASP .Net
    Replies:
    16
    Views:
    747
    Scott Allen
    Feb 12, 2005
  5. caldera
    Replies:
    1
    Views:
    4,407
    Scott Allen
    Feb 22, 2005
Loading...

Share This Page