Perl Need help with programming assignment
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Need help with programming assignment

  1. #1
    Join Date
    Apr 2012
    Posts
    4

    Need help with programming assignment

    So I have a programming assignment where I need to parse multiple XML files and return their attributes in a readable format. Currently the script parses all of the files and stores all the details for each file at each position of an array. An example of the desired output would be:

    J MIG-49 at location "85,20,0.1" is moving to "0.5,-0.5,-300

    Below is the XML code for an example.

    Code:
    <?xml version='1.0'?>
    <threat>
            <location>"85,20,0.1"</location>
            <ftype>J</ftype>
            <fmodel>MIG-49</fmodel>
            <movement_speed>"0.5,-0.5,-300"</movement_speed>
    </threat>
    And here is my current Perl script.

    Code:
    my $count=0;
    opendir (DIR, "../folder") or die "$!";
    my @feeds = grep {/threats.*?\.xml/}  readdir DIR;
    close DIR;
    # The above 3 lines read in the XML files and store them in array feeds.
    for $threats(@feeds) {
       open (READ, $threats)||die"$!";
       flock(READ, 1) || die "Can't lock xml: $!";
       while (<READ>) {
          $all[$count].=$_
       }
       close READ;
       $count++;
    }
    print @all; # report

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,879

    Re: Need help with programming assignment

    It doesn't look like you've done any parsing yet whatsoever. A simple Google search would have given you hundreds of helps...

    http://oreilly.com/catalog/perlxml/chapter/ch03.html
    http://www.techrepublic.com/article/...simple/5363190
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Apr 2012
    Posts
    4

    Re: Need help with programming assignment

    Quote Originally Posted by PeejAvery View Post
    It doesn't look like you've done any parsing yet whatsoever. A simple Google search would have given you hundreds of helps...

    http://oreilly.com/catalog/perlxml/chapter/ch03.html
    http://www.techrepublic.com/article/...simple/5363190
    It actually does parse through. Those other examples are only for parsing a single XML file or a multi-level XML file. That's easy. This is multiple XML files within a loop.
    Last edited by AngelBladeVII; April 9th, 2012 at 03:15 PM.

  4. #4
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,879

    Re: Need help with programming assignment

    So then what's your problem if you already read through the files and already say that parsing XML is easy?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Apr 2012
    Posts
    4

    Re: Need help with programming assignment

    Quote Originally Posted by PeejAvery View Post
    So then what's your problem if you already read through the files and already say that parsing XML is easy?
    My problem is that I haven't been able to find any example that are allowing me to pull out specific attributes from each file. Honestly I'm relatively new to the syntax of Perl so I'm not quite sure how I would do this. I need to pull out the values for location, ftype, fmodel, and movement_speed for each file as that is the relevant information needed in this program. Sorry if I've been too vague and I appreciate your help.

  6. #6
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,879

    Re: Need help with programming assignment

    Quote Originally Posted by AngelBladeVII View Post
    My problem is that I haven't been able to find any example that are allowing me to pull out specific attributes from each file.
    Which is the exact problem I covered with my first reply to you. And you said that you could already do it because it was easy.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Apr 2012
    Posts
    4

    Re: Need help with programming assignment

    Quote Originally Posted by PeejAvery View Post
    Which is the exact problem I covered with my first reply to you. And you said that you could already do it because it was easy.
    I know the example there does that, but that's for only one file, as opposed to multiple files within a directory. I'm not quite sure how to set up a loop to do it for multiple files.

  8. #8
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,879

    Re: Need help with programming assignment

    Just put the parsing code inside of the loop. Then you have it executing on every file found.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center