Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

build array of geometry data

by Frippe (Acolyte)
on Feb 10, 2003 at 13:57 UTC ( [id://234118]=perlquestion: print w/replies, xml ) Need Help??

Frippe has asked for the wisdom of the Perl Monks concerning the following question:

Hi If a geometry data use this ex.
[1 1 1 1 1 1] [4 4 4 4 4 4]
[ 123 123 123 123 123 123 ....... ]
"P" [ -1 1 1 1 1 -1 1 1 1 ]
So what I'm look for is how to pick up a begin character and end character so I could extrate the 4 array say my $begin1 = '[' and my $end1 = ']' build a array between begin and end I have test with simple qw but I like to search and match a begin and end character to.

I could give more example but the script would pick up 'renderman rib' file and pick the data between [] and build a new data from that.

regards,
Fredrik Gustafsson

update (broquaint): changed <code> tags to formatting

Replies are listed 'Best First'.
Re: build array of geometry data
by MZSanford (Curate) on Feb 10, 2003 at 14:16 UTC

    I don't know renderman, so i am not sure how complex this is going to grow to, but i think the following will do something similar to what you are asking about : lightly-tested code ahead

    #!/usr/bin/perl my $string; { $/ = undef; $string = <DATA>; } while ($string =~ m!\[\s*([^]]+)\]!g) { my @nums = split(/\s+/,$1); print "Array : ",join(',',@nums),"\n"; } __DATA__ [1 1 1 1 1 1 ][4 4 4 4 4 4] [ 123 123 123 123 123 123 123 123 123 ] "P" [ -1 1 1 1 1 -1 1 1 1 ]

    If renderman has a very complex file format, you may want to write a more robust parser.


    from the frivolous to the serious
      Thanks for your fast reply !!! I use the open ( INPUT, .....) to read in the file and I want to seprate the 4 array's so something like this my @nloops = ...; my @lverts = ...; my @vertid = ...; my @paramslist = ...; so your code was nice to print this info but I like to asign it to 4 array ex. my @nloops = [ ... ]; my @lverts = [ ... ]; my @vertid = [ ... ]; my @params = [ ... ]; next I could count the array length this I know how to next I could build a for loop to assign 1 array and first value to new + data format ... ex. change the data 0.5 0.5 0.5 add 1 -------- first array to output 0.5 0.5 0.5 1 Print some info here !!! 4 < 1 2 3 4 Print end info !!! so in another words the 4 arrays need to be seprate. ex. print @nloops; OUT 1,1,1,1,1 print @lverts; OUT 4,4,4,4,4 ...

        I am unsure what you mean about changing the data. While i am not quite clear on that, i do understand you want to save each array of numbers seperatly. To accomplish this, i used an Array of Arrays. The code is listed below (it is untested) :

        #!/usr/bin/perl my $string; { $/ = undef; $string = <DATA>; } my @collection; while ($string =~ m!\[\s*([^]]+)\]!g) { my @nums = split(/\s+/,$1); print "Array : ",join(',',@nums),"\n"; push(@collection,\@nums); } ## Now, i have an array or arrays, like : # # @collection = ( # [1,1,1,...], # [4,4,4,...], # [123,123,123,123,...], # [-1,1,1,1,1,...], # ) print "nloops : ",join(',',@{$collection[0]}),"\n"; print "lverts : ",join(',',@{$collection[1]}),"\n"; print "vertid : ",join(',',@{$collection[2]}),"\n"; print "params : ",join(',',@{$collection[3]}),"\n"; __DATA__ [1 1 1 1 1 1 ][4 4 4 4 4 4] [ 123 123 123 123 123 123 123 123 123 ] "P" [ -1 1 1 1 1 -1 1 1 1 ]

        I do not understand what you wrote about changing the data, but from what i posted i think you should be able to work out the seperate array issue which should put you well on your way.


        from the frivolous to the serious
Re: build array of geometry data
by BrowserUk (Patriarch) on Feb 10, 2003 at 17:17 UTC

    If your files get very large, then slurping them into memory to parse can be memory intensive. This solution processes the file line-by-line and uses a running buffer to parse it.

    Code

    Output


    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://234118]
Approved by broquaint
Front-paged by jdporter
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-24 18:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found