Re: Updating an array
Michael Bowden wrote: Hi folks: I first want to apologize for this post. I think that during the fall back, I lost a few brain cells. I am working on a Perl script to update a data file. I load my data into an array. I want to cycle through the array and make changes. Then output the array to a file. In the past, I would open the database, read in the lines, and output each line as I updated the elements in the array. In these scripts, I was only updating, one element in the array. Now I need to update several elements in the array before writing everything to a file. If I write the program as I have in the past, I would be using a lot of i/o reading the data into the array, changing one element and then outputting the data to a file and repeating the steps over and over until all the changes have been made. In some cases, I will be changing 20 to 30 elements in the array. So I need some help. I read in an array from a file. It looks something like this: 200044455669,,abcdefg,777,f00,f00,f00,f00,a01,b23,c45,d00,e10,no,no,no,no,no, 200011122333,,hikls,223,f32,f43,f00,f00,a04,b06,c08,d03,e09,no,no,no,no,no, mrgreenjeans,,pqrstuv,2244,f20,f00,f00,f00,a06,b08,c10,d10,e00,no,no,no,no,no, 20006654987,,zzzxyxz,33344434,f03,f00,f00,f00,a00,b12,c12,d13,e14,no,no,no,no,no, Each line represents one element in the array. So $array[0] equal 200044455669,,abcdefg,777,f00,f00,f00,f00,a01,b23,c45,d00,e10,no,no,no,no,no,. I want to update the 4th, 5th, and 13th subelements in $array[0], $array[1] and $array[4]. Then I want to write the updated @array back to a file. Can some one shed some light on how I can do this without updating a single line at a time? Hi Michael, Is it really too much i/o to do things as you have described? It sounds like you are reading in the entire file once, and then writing it out once. I cannot imagine how you could do it better than that without using a special data file structure (DBM files etc). Here is what I think you are doing (roughly): open(INFILE, "someotherfile"); while $line () { # perform some operation on the line of data $line =~ s|someregularexpression|somereplacement|; print OUTFILE $line; } close(INFILE); close(OUTFILE); # replace the INFILE with the OUTFILE by renaming OUTFILE. Is that what you are doing? Or something else? I think that is about as effecient as you can get with i/o without using a datafile on disk that has an index (like DBM etc). Keep in mind that by default your i/o is buffered so PERL won't be writing any data at all until it feels it has enough data to make the disk i/o worthwhile. That helps to prevent the system from having too many tiny writes to disk. -- Michael McDonnell, GCIA [EMAIL PROTECTED]
Re: automagically create browsable POD pages
Eric Lease Morgan wrote: Is there some sort of make command I can run that will read the PODs in my distribution, turn them into (X)HTML files, and save them in a specified local directory of my distribution's filesystem? Do you mean like the pod2html command or something different? -- Michael McDonnell, GCIA Winterstorm Solutions, Inc. [EMAIL PROTECTED]
Re: automagically create browsable POD pages [pod2html]
Eric Lease Morgan wrote: BTW, pod2html looks like it will already do this, but I can't figure out how to make it: 1. create a single xhtml file for each pod 2. give each file a specific name Yeah, I can't do this pod by pod, but I'm lazy. I'm still not understanding what the problem is. Why can't you build the docs pod-by-pod? Allow me to offer another suggestion that might be the middle of the road your looking for. A single command line solution based on pod2html. Assume you start in your module build directory and that you want to build your html docs from perl file in the "lib" subdirectory and put html output in a subdirectory called "ht" find lib -type f -name '*.(pm|pl)' -exec pod2html --htmldir=ht --infile={} --outfile=ht/{}.html\; Of course, if your using a makefile maybe you want to do some variation of this that only build pods for specific parts that you might be building separately. And of course you can have a separate install step that copies the contents of the "ht" subdirectory to someplace you want to install them. -- Michael McDonnell, GCIA Winterstorm Solutions, Inc. [EMAIL PROTECTED]
Re: STDIN as well as command line input
Eric Lease Morgan wrote: How do I get a Perl program to accept input from STDIN as well as command line input. I have a program (foo.pl) that is designed to read the contents of @ARGV and process each item in the array. Tastes great. Less filling. So, when I do something like this, things work just fine: %>foo.pl a b c d e f g I have another program (bar.pl) that prints to STDOUT. The output is the same sort of data needed by foo.pl. So, I thought I'd give this is a whirl: %>bar.pl | foo.pl But alas, foo.pl never seems to get the input sent from bar.pl. It does not seem to read from STDIN. This sort of situation can be dealt with with "back ticks": foo.pl `bar.pl` This is nice in that you can probably do this too: foo.pl a b c `bar.pl` d e f g h `bar.pl x y z` i j k A popular GNUism might be helpful here as well. Many GNU programs use an option command line argument of "--" to indicate that input should be taken from STDIN instead of from other command line arguments. -- Michael McDonnell, GCIA Winterstorm Solutions, Inc. [EMAIL PROTECTED]
Re: array references
Eric Lease Morgan wrote: How do I manipulate array references? In a package I'm writing I initialize an array reference (I think) through DBI like this: $self->{author_ids} = $dbh->selectall_arrayref($query); This gets tricky because selectall_arrayref returns an reference to an array. Each value of that array is a reference to an array as well. $self->{author_ids} is a reference to an array whose elements are in fact references to arrays (that contains scalars thank goodness). @{$self->{author_ids}} is the array. $#{$self->{author_ids}} is the number of elements in the array. @{$self->{authors_ids}->[0]} is the first row of data (an array). $#{$self->{authors_ids}->[0]} is the number of columsn in the first row of data. $self->{author_ids}->[0] is a reference to an array representing one row of data. $self->{author_ids}->[0]->[0] would be the first field of the first row of data. First of all, what sort of foreach loop can I write to iterate through the contents of $self->{author_ids}? foreach $i (0..$#{$self->{authors_ids}}) { foreach $j (0..$#{$self->{authors_ids}->[$i]}) { print $self->{authors_ids}->[$i]->[$j]; } print "\n"; } Second, how do I undefine the value of $self->{author_ids}? $self->{author_ids} = undef; Third, if I have a list of integers, how to I assign the items in this list to $self->{author_ids}? $self->{author_ids}->[0] = 1; $self->{author_ids}->[1] = 2; $self->{author_ids}->[2] = 3; or $self->{author_ids} = [1, 2, 3]; Or if you want something similar to what selectall_arrayref() is producing: $self->{author_ids} = [ [1,2,3], [3,7,9], [2,8,4]]; Which could be printed like so: foreach $i (0..$#{$self->{authors_ids}}) { foreach $j (0..$#{$self->{authors_ids}->[$i]}) { print $self->{authors_ids}->[$i]->[$j]; } print "\n"; } I would strongly recommend adding quotation marks to {authors_ids} so that it is {'authors_ids'} or defining it as a constant. The only reason why {authors_ids} is equivalent to {'authors_ids'} is because PERL is converting it to a constant on the fly for you. If you accidentally picked a name that was already a constant in your namespace you might end up with results that are hard to debug (imagine if a module put a constant in your name space that you didn't know about). -- Michael McDonnell, GCIA Winterstorm Solutions, Inc. [EMAIL PROTECTED]