Re: Fwd: Parsing web pages
The submodules WWW::Mechanize::Firefox or WWW::Mechanize::PhantomJS are worth a look too, depending on the complexity/js-heaviness of the pages you're parsing and what your setup looks like exactly (full headless; on your computer, etc). On Fri, Mar 3, 2017 at 1:39 AM, Lars Noodénwrote: > On 03/03/2017 02:15 AM, kavita kulkarni wrote: > > Hello, > > > > Can you suggest some effective ways to parse multiple web pages from the > > web site. > > I cannot use web crawling as the format of the pages is not same. I am > > interested in the data from specific table on each page. > > > > Thanks in advance. > > Kavita > > > > Once you have acquired the page using either WWW:Mechanize, LWP, or even > just wget you can extract the table. > > The modules HTML::TreeBuilder and HTML::TreeBuilder::XPath do extraction > rather easily if there is some consistent way to identify the table. > > Regards, > Lars > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > >
Re: How to split a large string with repeating delimiters into multiple substrings
Hi Michael, On 5/23/07, Michael Goopta [EMAIL PROTECTED] wrote: How can I split the below string and get the multiple web-addresses in a list: (i.e. the strings between upsl-url and /upsl-url upsl-urlhttp://view-preprod.admission.net/abc/mactive/_NJMG_0002029003-01/i-1.JPG?t=tr/m:FitPad/w:199/h:124t=ts/r:199x199/upsl-urlupsl-urlhttp://view-preprod.admission.net/abc/mactive/_NJMG_0002029003-01/i-1.JPG?t=tr/m:FitPad/w:199/h:124t=ts/r:199x199/upsl-url /ad-type Any time you can avoid using regular expressions to parse xml-ish data, it's usually easier to do so. #!/usr/bin/perl use strict; use warnings; use XML::Simple; use Data::Dumper; my $xml; { local $/ = undef; $xml = DATA; } # fixup unescaped ampersands $xml =~ s//amp;/g; my $parsed = XMLin($xml, ForceArray = 1); print Dumper($parsed); __DATA__ ad-type upsl-urlhttp://view-preprod.admission.net/abc/mactive/_NJMG_0002029003-01/i-1.JPG?t=tr/m:FitPad/w:199/h:124t=ts/r:199x199/upsl-urlupsl-urlhttp://view-preprod.admission.net/abc/mactive/_NJMG_0002029003-01/i-1.JPG?t=tr/m:FitPad/w:199/h:124t=ts/r:199x199/upsl-url /ad-type -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Retrieving a web resource (GET) for parsing
On 4/6/07, yitzle [EMAIL PROTECTED] wrote: If I want to parse a few web pages, what's the best way to retrieve them? Should I just run `wget $url`? LWP can do this for you. http://search.cpan.org/~gaas/libwww-perl-5.805/lib/LWP.pm If you want more complicated interactions, WWW::Mechanize is good also. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: removing special characters
On 4/4/07, Michael Gargiullo [EMAIL PROTECTED] wrote: I have a log file I'm parsing that has special characters at the end of each row. In vi it appears to be ^@ I've already tried chomp and s/\^\@// Neither work. Does any one have any ideas? You can match what vi(m) displays as '^@' by typing ctrl-v ctrl-shift-2 in your substitution regex. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: File::Find again
On 3/25/07, Alan [EMAIL PROTECTED] wrote: On Sunday 25 March 2007 18:14, Matt Herzog wrote: This is all I needed. I swear I had /($searchstring)/; in there at some point before . . . so if I pass it -s \.properties$ at the command line, it works as expetcted. Nice. That might be a shell thing? In Linux bash shell those quotes (I think) tell the shell to not interpret anything inside the quotes. bash quotes work like perl quotes: echo $PS1 echo '$PS1' -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Checking for infinite loops
On 1/8/07, hOURS [EMAIL PROTECTED] wrote: Hi everyone, Jay offered me the following code to help with something. I don't undertand it, but tried to use it anyway to see if it would work. The computer told me there was a syntax error in the area I highlighted in color. I can't find it - maybe that's because I don't understand the code fully, but if anyone can show me where it is I'd be grateful. Jay Savage [EMAIL PROTECTED] wrote:my $timeout = 3600; # 1 hour eval { local $SIG{ALRM} = sub { die longRunningModule timed out\n }; alarm $timeout; require longRunningModule; alarm 0; } in case this isn't fixed yet, eval blocks need to be ended with a semicolon, like eval { }; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: HoHoH
On 1/9/07, oryann9 [EMAIL PROTECTED] wrote: I have a HoHoH structure that looks like after running print Dumper (\%hash); 'prlhNSSA' = { '1499' = { 'gecos' = 'First Name Last Name,CIS,location, 'host' = '/var/tmp/passwd.tgpdrpp1.hpux', 'gid' = '205' } } }; My 1st hash is keyed by name, that accesses 2nd hash keyed by UID, that accesses a 3rd hash keyed by remaining items: gid,gecos and hostname. [snip] my question is how do I access and print all values? I am trying: foreach (keys %dub_hash) { print %{$dub_hash-{$name}-{$uid}-{%gid}-{$gecos} }; } Do I need three loops? What am I doing wrong? Yup, three loops. I'll get you started: for my $name (keys %dub_hash) { for my $uid (keys %{$dub_hash{$name}}) { # do stuff, note the %{...} above to force precedence } } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: win32::guitest
On 1/5/07, MGautam [EMAIL PROTECTED] wrote: Hi, How do we use win32::guitest to get a particular content of the webpage into a variable. I have a task: 1) open a browser 2) hit the url 3) hit few tabs and fill the required information then submit the form 4) after submit, it leads to new page. (Here i need to check for particular msg. (success/failure) to a variable, so that i can run regexp on that variable and come to know whether the operation was success or not. Here I can do till step 3. But can somebody help in how to do the step 4? (and how to get the whole content of the webpage ?) I'm not familiar with win32::guitest, maybe someone else can answer your specific question, but I would recommend WWW::Mechanize or http://www.openqa.org/selenium/ instead of win32::guitest if you have a choice of method. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: time limited STDIN
On 1/4/07, Saurabh Singhvi [EMAIL PROTECTED] wrote: Hi all, I wanted to know a way of taking a time limited STDIN. Say for eg after 3 seconds, the STDIN should stop waiting and move forward. How do I do this?? I was trying with unless ($child) { sleep($waittime); `echo \n`; exit 0; } $ans = STDIN; You can do this using the alarm function and an eval block as described here: http://perldoc.perl.org/functions/alarm.html -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: How to pull Text from a PDF using Perl?
On 1/4/07, Wagner, David --- Senior Programmer Analyst --- WGO [EMAIL PROTECTED] wrote: I need to look at the text from page 1 of a couple of thousand pdf's and do a regex on searching for the data. Before sending I tried a number of other things, but either died or showed me data like the above. Any insight or simple script which will display the text would be greatly appreciated. I had to do this the other day and got frustrated with the modules I found and ended up using pdftotext which comes with xpdf, like so: my @pages = split /^L/, `$pdftotext -layout $inputfile -`; for my $page (@pages) { # do stuff } Without the -layout switch, parsing any sort of tabular data becomes a lot more annoying. Cheers, Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Other ways to assign a filehandle to a variable?
For posterity: http://perl.plover.com/local.html#3_The_First_Class_Filehandle_Tr -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: MySQL question
On 7/21/06, Karjala [EMAIL PROTECTED] wrote: Maybe I should ask this question on a database list, but it's related to DBI, so I'm asking here also: I have a field in a record in the MySQL database that contains a number. I increase it by one with $dbh-do(update table set myfield = myfield + 1 where mykey = 10); I was wondering whether there's a way to find out the value of myfield right after the update. Running a select myfield right after the update is not a good enough solution to my problem, since this is a web-based application, and therefore it's possible that myfield might increase again between the time of the first update and the select. Do you know of any solution to this problem? http://dev.mysql.com/doc/refman/4.1/en/ansi-diff-transactions.html -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Randal L. Schwartz is Wrong (WAS:write out filenames of files existing on a filesystem into afile)
On 7/13/06, Chad Perrin [EMAIL PROTECTED] wrote: On Thu, Jul 13, 2006 at 01:44:40AM -0400, Mr. Shawn H. Corey wrote: It comes down to this, either he will post an apology for stating a third party should not post his comments, or I'll leave this list, never to return. 1. You may have been able to garner some sympathy if you hadn't used the I'm taking my ball and going home! line. That wasn't well advised. 2. I really wish everyone would shut up about this and talk about Perl. If you really don't like what he has to say, don't read his emails or respond to him. I'm about ready to /dev/null YOUR emails, by now. See point 1. for why. I kind of wish I could say that I didn't read all of that, but I actually did. Thanks for the laughs, guys. I ESPECIALLY ENJOYED ALL THE CAPITAL LETTERS. OT penance: Don't splice into an arrayref returned from $sth-fetchrow_arrayref() (unless you make a copy of it first) when using DBD::mysql or you will probably segfault the next time around the loop. Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Slurping a big file (WAS: Netiquette)
On 6/23/06, Omega -1911 [EMAIL PROTECTED] wrote: On 6/23/06, Dave Gray [EMAIL PROTECTED] wrote: On 6/23/06, Omega -1911 [EMAIL PROTECTED] wrote: Shawn, I modified your example like so, was this correct? chomp( my $data1 = IN ); # line 1 chomp( my $data2 = IN ); # line 2 chomp( my $data3 = IN ); # line 3 chomp( my $data4 = IN ); # line 4 chomp( my $data5 = IN ); # line 5 while( IN ){ chomp; push @past_bids, $_; # push remaining lines into array } close FILE; You still haven't told us WHY you want to keep the entire rest of the file you're reading in an array. Actually, for this particular sub, I don't need the rest of the file and I wasn't sure of the best method to extract the first five lines without re-writing the file structure. Your advice is welcomed and I would appreciate any clues or insight on an efficient style of code. Simply reading lines from a file does nothing to affect the file itself, unless it happens to be a quantum file (ha ha), which it isn't. You can omit the while loop and have exactly the same behavior without wasting a lot of memory. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Slurping a big file (WAS: Netiquette)
On 6/23/06, Omega -1911 [EMAIL PROTECTED] wrote: Shawn, I modified your example like so, was this correct? chomp( my $data1 = IN ); # line 1 chomp( my $data2 = IN ); # line 2 chomp( my $data3 = IN ); # line 3 chomp( my $data4 = IN ); # line 4 chomp( my $data5 = IN ); # line 5 while( IN ){ chomp; push @past_bids, $_; # push remaining lines into array } close FILE; You still haven't told us WHY you want to keep the entire rest of the file you're reading in an array. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: An array like line that I do not understand
On 6/16/06, Alan_C [EMAIL PROTECTED] wrote: [snip] 1 'build' slack build slackbuild sbuild pkg 2 'build' slack build slackbuild sbuild pkg 3 'build' slack build slackbuild sbuild pkg 4 'kernel' slackware 2.6 kernel howto 5 'kernel' kernel compile install 2.6 6 'build' building a linux kernel from source 7 'kernel' building a linux kernel from source [snip] for my $keyline ( @lines ) { my $filename = shift @$keyline; for my $search ( @search4 ) { for ( @$keyline ) { if ( /$search/ ) { # How to not have duplicated keyword lines? # the next line I don't understand. How to get it hash like? push @{ $data{ $filename } }, $_; # I think this line cause #print ++$found_tally . . $search,@$keyline, \n; # prints keyline 4 ea found print ++$found_tally, '$search' @$keyline\n\n; last; } } } } Notice that the first three matches are the same, and the word build occurs 3 times in that one @$keyline array. Sounds like you want to stop looping over [EMAIL PROTECTED] after your first match. Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Servlet
On 5/30/06, Tom Allison [EMAIL PROTECTED] wrote: How can I impliment a Servlet in Perl without writing my own http server or running apache? What exactly are you trying to do? I assume you didn't get a response because you weren't very specific. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Counting arrays
On 5/22/06, Michael Gargiullo [EMAIL PROTECTED] wrote: It's been a while since I've used Perl and I need some help with a multidimensional array. I have a file that I need to compile some stats on. I need to keep track of 'actions' and 'rules'. Yes, stats from a firewall. Both 'actions' and 'rules' need to be dynamic so if a rule is added, it's automatically dealt with. As I loop through the file I have both the current action and rule as a variable. I've tried to store them several ways... my @actionrule; $actionrule[$action][$rule]++; In order for this to work as you expect, $action and $rule need to be numeric. Is that the case? If not, do you need to preserve the order? I need some kind of loop to extract the data. I also need to know what the action and rule of each count is. I've tried a few while and foreach loops without success. Within the loop I need to build a query (per record). $query=insert into tmpfw set action=$action, rule=$rule, count=$count; foreach $first (@actionrule){ foreach(@first){ print $_.\n; } } You need to add 'use strict;' at the top of your program. Not only will that help you see if you've made a typo, it will help us be able to debug your program better. Also read this: http://perldoc.perl.org/perlreftut.html for my $rule (@actionrule) { for my $i (@$rule) { # do stuff } } Not only does it not print the counts, but how do I get the action and rule associated with it? Thanks, Mike -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Python - NOT TROLLING
On 4/25/06, Paul D. Kraus [EMAIL PROTECTED] wrote: I am picking up python and messing around with it and I always come running back to perl :) At any rate I am curious what the more experienced programmers think of the language and its uses. One thing that a lot of people like about Python is the encapsulation. I personally hate that feature... everyone says that Python is easy for programmers to read and understand, but if I have to do the following a bunch of times import pprint dump = pprint.PrettyPrinter(indent=4).pformat # ... dump(obj) # nothing useful dump(obj.__dict__) # more useful in order to see what's going on inside an object, that really makes me miss Data::Dumper and the sort of manual introspection Perl allows. That wasn't really what you asked for, I guess, but whatever :) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Very Basic Web Scrape
On 4/7/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: On Fri, 07 Apr 2006 16:02:53 -0400, Oliver Block [EMAIL PROTECTED] wrote: I understand regex, but the following fails: open PAGE, 'c://redcross.htm'; while( my $line = PAGE ) { $line =~ /Health and Safety Classes/ print $1\n; } What fails? Your forget a ';' after the regex but I guess that's not what you mean!? :) Now that was pretty basic. So now that script runs, but I get the full page. I was trying to limit the result to the words /Health and Safety Classes/ that appear on the page. How do I get there? $1 refers to the first parenthesized group in your regular expression. So if you change your code to look like: if ($line =~ /(Health and Safety Classes)/) { print found a match: [$1]\n; } then $1 will refer to the literal text inside the parens. Obviously, $1 matches more interesting things once you include some meta-characters. http://perldoc.perl.org/perlre.html -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: regex one liner
On 3/21/06, Kevin Viel [EMAIL PROTECTED] wrote: BTW, out here in the real world (that would be UNIX), *.pl stands for Perl Library file, not a script. What extension do you suggest using, if any, in the real world? .ps for perl script /snicker -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: new for regular expression in Perl
On 1/4/06, chen li [EMAIL PROTECTED] wrote: I think it might be natural for me to read the file line by line and get the return position looks like these(just an example), similar to do the word search in microsoft Word, which is what I really want: match in line 1 and the end of matching position is 4 match in line 4 and the end of matching position is 24 perldoc -f pos http://perldoc.perl.org/functions/pos.html #!/usr/bin/perl while (DATA) { /(?=bleh)/g print line $. matches [.pos($_).]\n; print substr($_, pos($_)-4, 4),\n; } __DATA__ slkdfjslkdfjdksfjdbleh sdfkjdblehblkdfjsldkfj sldfkjdklfdskfdblehasfkjdsklf slkdfjdkblehsldkfjdkfj -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: pack an array
On 1/2/06, John W. Krahn [EMAIL PROTECTED] wrote: Gerard Robin wrote: Hello, Hello, I guess that one can write in more perlish fashion that I did: the part between the of this script to pack the array @array. Please, can someone give me some hint ? You don't need to use an array for that: my $string = [EMAIL PROTECTED] n??e#w [?! \$ \% y]e{?]a##r? 2\*0\$0'\6\# ! \^; $string =~ tr/ [EMAIL PROTECTED]^'][*{/ /sd; print $string\n; I find it's easier to specify what I want to keep instead of what I need to delete: $string =~ s/[^A-Za-z0-9 ]//g; So the above is pronounced get rid of everything that isn't A-Z or a-z or 0-9 or a space. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: having problems with index of a date string
On 11/11/05, kathyjjja [EMAIL PROTECTED] wrote: I am trying to add the date to a file name. [snip] Usually when I want to do something like this, it's to make a uniquely named temp file, so the date doesn't need to be human readable, so something like this works just fine: # filename_processid_epoch seconds my $tmpfile = '/var/tmp/prefix_'. $$ .'_'. time; Just a thought. Simpler (than having to parse a string-formatted date), which is always good. Cheers, Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: combining array ref's
On 11/13/05, Tom Allison [EMAIL PROTECTED] wrote: my $A = [1,2,3,4,5,6,7,8,9]; my $B = [11,12,13,14,15,16,17,18,19]; timethese(1000, { 'plain' = 'my $array = [$A, $B];', 'loopy' = 'push @$A, $_ foreach @$B;' }); Another important thing to notice is that John's benchmarks operate entirely on temporary variables while the ones above modify the main variables every single time, leading to a very large array in $A and most likely not demonstrating the specific performance measurement desired. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Thank you for your answer. And yesterday i was write from Nahid`s email address
On 10/17/05, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: This is a file is info.xml and it constraint below info Personal NAMEUlfet/NAME SURNAMETANRIVERDIYEV/SURNAME AGE24/AGE ADDRESSBAKU/ADDRESS /Personal Education SCHOOLXetai 191/SCHOOL UNIVERSITYQafqaz University/UNIVERSITY /Education /info So, i wrote script in PERL. And it is below #* #!/usr/bin/perl open(FILE,info.xml) or xeta(File can not open...); @b = FILE; close(FILE); $search = ''; @results = grep(/$search/,@b); print @results\n; #* And have a other script and its below #* #!/usr/bin/perl -w use XML::Simple; my $infile = 'info.xml'; my $xml= XMLin(); print XMLout($xml), \n; #* So, i need to get that information from indo.xml (Ulfet, Tanriverdiyev, 24, Baku,) I want to get without tags information. I read that in PERL has a module which can read files(XML) and get from information whitout tags. Please help me, how can i get. I wrote script which can find and get ftom file that information without XML tags. Thank you very much in advance the documentation (available online here): http://search.cpan.org/~grantm/XML-Simple-2.14/lib/XML/Simple.pm#EXAMPLES has some examples that are very similar to what you are attempting. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Alcatel CORBA 5620 OSS
On 10/17/05, Tielman Koekemoer (TNE) [EMAIL PROTECTED] wrote: We have an Alcatel CORBA 5620 gateway from which we have extract information. Does anyone know of a module that would enable Perl to access this service? I searched the CPAN's archive but could not find anything. Apologies if this is the wrong list to ask - which would be the best list? I don't know what an Alcatel CORBA 5620 gateway is, and alcatel.com doesn't help much either: http://www.alcatel.com/products/productsbyreference.jhtml?productRange=5-6pageNumber=3 Do you know what it is? Could you explain to us what it is, what it does, and how it's accessible? I'm going to assume that from which we have extract information means that you need to get some information off of the gateway thingy. Is that correct? More info is always good. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Finding directories within a tree
On 10/7/05, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I am receiving numerous amounts of mail but have not filled out any questionnaires giving away any sort of information. If these letters continue being sent to me I will be forced to report you to AOL as spam and possibly other forms to get me taken off of whatever list that I am on. PLEASE take my off of the lists on which I am to receive these annoying messages. thank you very much and have a nice day. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response Now /that's/ comedy. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Finding directories within a tree
On 10/7/05, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Good Afternoon I am attempting to develop a script that will parse a directory listing and return only directory names that match a given expression. It would make sense to me to use File::Find to do this but based on the dir structure I am parsing, the amount of overhead to do this is enourmous ! Basically, I have a file structure similar to: Dir1\Dir2\Support\119404\dirx\diry Dir1\Dir3\Support\119893\dirx Dir1\Dir4\Support\14\dirx\diry\dirz . Dir1\Dir1000\Support\100858 I am simply interested in finding the directories directley under the Support dir (ex.119404 from the 1st example) . There is no consistancy to the naming convention other then Dir1 and Support. Dir2 can be many different values. I tried functionality similar to the following that did work on a much smaller test bed: my $dirs=I:\\ID_00_000999; find sub { push @dirs, $File::Find::dir if $File::Find::dir =~ m/.+[Ss]upport\/\d+$/;}, $dirs; This is not only processing directories, but any files present as well. It will probably be a considerable (although untested) speedup to do something like: my $dir=I:\\ID_00_000999; our @dirs; sub callback { push @dirs, $_ if /.+[Ss]upport\/\d+$/; } sub filter { # we only care about directories return grep(-d $_, @_); } find { wanted=\callback, preprocess=\filter, no_chdir=1 }, $dir; print @dirs; Read up on File::Find, and consider putting some prints in your callbacks to see what's getting called and exactly what it's doing. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: WELCOME to beginners@perl.org
On 9/28/05, Ryan Frantz [EMAIL PROTECTED] wrote: Just for giggles, I'm gonna assume that you're using PPM (you gave nothing else to go on)... I've only installed this particular module using ActiveState's 'ppm' myself; it seems to have problems with the '::' in module names. If ppm can't find what it believes the module name is, it will default to a search and probably return something like this: ppm search spreadsheet::parseexcel Searching in Active Repositories 1. Spreadsheet-ParseExcel [0.2603] Get information from Excel file 2. Spreadsheet-ParseExcel-Sim~ [1.02] A simple interface to Excel data 3. Spreadsheet-ParseExcel_XLH~ [0.02] Parse Excel Spreadsheets using xlhtml Use the 'install' command and substitute '-' for '::' like so: ppm search Spreadsheet-ParseExcel-Simple You can use the numbers instead of typing all that IIRC, so: ppm install 2 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: naming subroutine reference parameter?
On 9/6/05, Jayvee Vibar [EMAIL PROTECTED] wrote: How do you name subroutine reference parameter in perl? Naming a local or pass by value is by simply using my ($param1, $param2) = @_ ; How about by reference? I think it would be harder if I'll be using $_[0], $_[1] direct method. Is it possible? Those are two separate questions... you can name function parameters like so: sub myfunc { my %args = @_; if ($args{debug}) { print debug!\n; } # ... } myfunc(debug = 1, foo = 'bar'); And you can pass by reference by using, er, references. #!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub nosoup { my %args = @_; if (defined $args{soupline} and ref $args{soupline} eq 'HASH') { if (defined $args{nosoupfor}) { delete $args{soupline}{$args{nosoupfor}}; } return $args{soupline}; } else { return (); } } my %soupline = (fred=1,dave=2); print Dumper(\%soupline); nosoup(soupline = \%soupline, nosoupfor = 'dave'); print Dumper(\%soupline); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: firing an external program and exiting
On 8/2/05, Ram [EMAIL PROTECTED] wrote: I have a situation where a CGI script has to start an independent perl script and exit without capturing the output or waiting for the exit code. It doesnt make any practicle sense to me as this perl script takes good 6 hours to run and my CGI script obviously shouldnt keep the user waiting. exec command doesnt seem to work, nor system command combined with ampersand() in command. I tried exec(command); and also system(command ); Niether seemed to work, for the reason which I assume is, since it is a CGI script and webserver waits until this script exits and then displays the results to the browser. What was thought to be a simple thing is becoming a huge problem. Let's try this again :) You were most of the way there the first time, and the shell script thing I suggested is unnecessary. system(command /dev/null ); # works for me Specifically: sleep.pl: #!/usr/bin/perl $|++; print STDERR time. start of sleep\n; sleep(5); print STDERR time. end of sleep\n; sleep.cgi: #!/usr/bin/perl system('/path/to/sleep.pl /dev/null '); print Content-type: text/html\n\n; print h1done printing/h1.time; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Backup of a subroutine
On 8/3/05, John W. Krahn [EMAIL PROTECTED] wrote: marcos rebelo wrote: I need to redefine localy one subroutine and have it correctlly after. How do I do it? $ perl -e' my $myPrint = sub { print Teste 1\n }; sub test { $myPrint = sub { print Teste 2\n } } $myPrint-(); test; $myPrint-(); exit; ' Teste 1 Teste 2 If you combine this solution with local variables, you can do exactly what you need: #!/usr/bin/perl use strict; use warnings; our $print = sub { print test 1\n }; sub test { $print = sub { print test 2\n } } $print-(); { local $print = $print; $print-(); test(); $print-(); } $print-(); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: reg exp using \G
On 8/2/05, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I think I am on the right track as far as what assertion to use. I need to print from one string to another using .. with \G Why do you want to use \G? My goal is to capture from allsets down. Here is my code: #!/usr/bin/perl use strict; use warnings; $ENV{PATH} = qq(/opt/SUNWsamfs/sbin:/usr/bin); open (ARC, archiver -lv |) or die $!; foreach (ARC) { ##-- Tape --## if (/allsets/ .. /fs_clinical.1/) You need to escape dots in regular expressions '\.' { print $_; my $tape =$1; Your regex isn't capturing anything, so $1 will always be undefined. } else { / \G 'heartlab.1'/ What are you trying to do here? You need to escape dots in regular expressions '\.' } } close (ARC); I think you would benefit greatly from trying to generalize your problem and writing a simple test script that simulates the problem you're trying to solve. Then if you're still stuck, post that test script here, as opposed to that 5 billion line data file which is mostly irrelevant anyway. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: reg exp using \G
On 8/2/05, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: technically you are correct about escaping the dot, but in this particular situation my regexp is working. I will escape the dot. my goal again is to print from one sting to another from allsets down. I apologize for the long output if data, but I want to reflect 100% accuracy. Any ideas on a simple for loop with a regexp? for (ARC) { print if /one string/ .. /another/; } You already had that solution, though. What am I missing? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: reg exp using \G
On 8/2/05, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: yes but the problem is my start point and end point have identical entries under them. It is stopping at original1.1 when I need for it to stop at the end of original1.1 and print media: sf Volumes: STK000 Total space available: 60.8G as opposed to just allsets to original1.1 Here's a test program that demonstrates how to do that. #!/usr/bin/perl use strict; use warnings; while (DATA) { if (/AA/ ... /(CC)/) { print; if ($1) { while (DATA) { # this regex defines when your section is done last if /^\s*$/; print; } } } } __DATA__ AA BB CC DD EE FF GG HH II -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: reg exp using \G
On 8/2/05, Dave Gray [EMAIL PROTECTED] wrote: On 8/2/05, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: yes but the problem is my start point and end point have identical entries under them. It is stopping at original1.1 when I need for it to stop at the end of original1.1 and print media: sf Volumes: STK000 Total space available: 60.8G as opposed to just allsets to original1.1 Here's a test program that demonstrates how to do that. #!/usr/bin/perl use strict; use warnings; while (DATA) { if (/AA/ ... /(CC)/) { print; if ($1) { while (DATA) { # this regex defines when your section is done last if /^\s*$/; print; } # so you don't have to spin over the rest of the file last; } } } __DATA__ AA BB CC DD EE FF GG HH II -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: firing an external program and exiting
On 8/2/05, Ram [EMAIL PROTECTED] wrote: I have a situation where a CGI script has to start an independent perl script and exit without capturing the output or waiting for the exit code. It doesnt make any practicle sense to me as this perl script takes good 6 hours to run and my CGI script obviously shouldnt keep the user waiting. exec command doesnt seem to work, nor system command combined with ampersand() in command. I tried exec(command); and also system(command ); Niether seemed to work, for the reason which I assume is, since it is a CGI script and webserver waits until this script exits and then displays the results to the browser. What was thought to be a simple thing is becoming a huge problem. What does it do exactly? Does it hang? What are the permissions on this other perl script? Does the user the cgi script is running as (presumably 'nobody') have permission to execute that script? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: firing an external program and exiting
On 8/2/05, Ram [EMAIL PROTECTED] wrote: I tried exec(command); and also system(command ); Niether seemed to work, for the reason which I assume is, since it is a CGI script and webserver waits until this script exits and then displays the results to the browser. What was thought to be a simple thing is becoming a huge problem. What does it do exactly? Does it hang? What are the permissions on this other perl script? Does the user the cgi script is running as (presumably 'nobody') have permission to execute that script? Yes the user cgi script is running have permissions to execute that script... Infact with the above mentioned methods I am able to execute the script very well but my cgi script is waiting until it is getting finished, which I dont want. I only want my cgi script to call the external perl script and exit. You're going to need to write a shell script that calls your program like so: CGI: system('/path/to/start_program.sh') SH: perl /path/to/perl_script.pl -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Using Config files and variables.
On 7/31/05, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: problem is that the variables in the config are not being translated into there actual values before they get used. So you have a plaintext config file with variable names in it that you want perl to interpolate at some point once the config data is loaded. my $sth = $dbh-prepare(qq{$QUERY}); #print DEBUG: $QUERY\n; What does this debug print give you? query= END SELECT N.IP_ADDRESS, [snip] AND CAA.ADDRESS_ID = AD.ADDRESS_ID AND P.NODE_ADDRESS = $dslam AND N.IP_ADDRESS= $ip_address END -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: 'Use Lib' problem...
On 7/26/05, Tony Frasketi [EMAIL PROTECTED] wrote: I'm trying to use the following 'use lib' statement as described at http://www.unix.org.ua/orelly/perl/prog3/ch31_13.htm It's not nice to link to pirated copies of books. BAD. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Atmoic operations in Multi-threaded Perl?
On 7/6/05, Siegfried Heintze [EMAIL PROTECTED] wrote: Can I assume that an auto-increment operation on an integer value is atomic (that is, cannot be interrupted by another thread)? This is a common assumption in C/C++. The perl debugger I use leads me to believe that perl stores all integers as strings however, in spite of my efforts to force them to integer by adding zero. This would mean that an auto-increment is probably not atomic. What about a string copy: is that atomic? these should get you started: http://www.perlmonks.org/index.pl?node_id=288022 http://search.cpan.org/~nwclark/perl-5.8.7/ext/threads/shared/shared.pm -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: usage of do {}
On 6/30/05, Peter Rabbitson [EMAIL PROTECTED] wrote: Here and there on the web I encounter claims that the do {} operator is depreciated. However I find it convenient to do things like: eval { some stuff } or do { some multiline error handling }; is this a bad practice? No, that's not bad practice IMO. It evidently works for you, and I think it's clear enough what's going on with that code. For the sake of code reuse, though, you might want to consider generalizing the code and sticking it into a module. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: usage of do {}
On 6/30/05, Jay Savage [EMAIL PROTECTED] wrote: Checking for $@, though, is a must no matter what you're using [eval] for. +1 despite my completely missing that omission earlier! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Run a subroutine, then replace subroutine and run again
On 6/27/05, Wagner, David --- Senior Programmer Analyst --- WGO [EMAIL PROTECTED] wrote: Sorry, but I have two subroutines with the same name which is fine because I have not had a need to use the hashes which these two similar subroutines created. How do I run the subroutine and generate my set of hashes, then replace the sub with the other and run again, but into different hashes. What I have is: a) use termareg and b) use termregall which is called by termareg( ... ); # where ... are the hashes passed Does 'use'-ing the modules call the subroutines that generate the hashes? If so, you could 'use' the first one and then 'require' the second one, so you can control when the second subroutine gets declared (runtime as opposed to compile time). You'll get a 'subroutine redefined' warning, but it should work fine. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: deleting ldap users question
On 6/27/05, Graeme McLaren [EMAIL PROTECTED] wrote: Hi all, I am trying to delete entries from an LDAP server and I don't understand why my code is not deleting the relevant entries. [snip] here is my code: # #!/usr/bin/perl use Net::LDAP; [snip] eval{$ldap-delete($dn)}; if($@){ print error: $@ \n; }else{ print username $_ deleted \n; } Net::LDAP[1] doesn't call die when it encounters an error. You need to check for an error condition like they do in the example, something like: $msg = $ldap-delete($dn); die $msg-error if $msg-code; [1] http://search.cpan.org/~gbarr/perl-ldap-0.33/lib/Net/LDAP.pod -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Logger output behavior not wanted
On 6/27/05, Pablo Wolter [EMAIL PROTECTED] wrote: Hi, I have some troubles to figuring out by myself a way to add the output of a script that I run by system function in perl into a logger. The code I have is: [snip] print LOG -- $hostName Database backup\n; print LOG -- Database backup started at: $timeStamp\n\n; system($makeBackup $errorLogFile); # this i want on the logger You can do: print LOG qx$makeBackup; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: How to handle Null-Charakters
On 6/22/05, Angerstein [EMAIL PROTECTED] wrote: I have a problem reading strings out of a binaery file. The last 128 Byte of the File contains a String I want to work with. (sorry, this code is windows, feel free to flame me ^^) my $tsize = 128; my $fsize = (-s d:\\mp3\\forseti.mp3); my $offset = ($fsize - $tsize); open(INF, d:\\mp3\\forseti.mp3); seek(INF, $offset, 0); $tag = INF; @id3v1 = split (//, $tag); $id3v_t = unpack(B8,$id3v1[125]); print $id3v_t; I get: If I print the whole string it looks like: This is whatI get 2002 ^^ ^^^ ^ The Parts I markt are no whitespaces. They are binaery . Is there something I could do to transform or remove this null-chars? CPAN has modules for working with ID3 tags[1]. That said, you can replace nulls with a space with: $id3v_t =~ s/\0+/ /g; [1] http://search.cpan.org/search?query=id3mode=all -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: creating html interface for the perl code
On 6/22/05, Aditi Gupta [EMAIL PROTECTED] wrote: I've to create html inteface for a perl code. I've to get the input from the user and the data entered in the form has to be processed and output(which is a graph) has to be displayed to the user. But i don't know how to do it... I am adviced to go for CGI, and i only know basic html.. Please guide me. How about the Template Toolkit[1]? CGI[2] is probably a good starting point. [1] http://search.cpan.org/~abw/Template-Toolkit-2.14/lib/Template.pm [2] http://search.cpan.org/~lds/CGI.pm-3.10/CGI.pm -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: how to append blocks in same file
On 6/22/05, Aditi Gupta [EMAIL PROTECTED] wrote: Since i'm using activestate perl on windows xp i don't know whether paste will work or not. I'll try the hash of array. Using a hash of arrays will not necessarily preserve the order. Below is the start of an array of arrays solution. I'll leave handling unbalanced blocks and reassembling the pieces as an exercise for the reader. my @blocks = (); my $maxlen = 0; while (DATA) { chomp; if (/^#/) { push @blocks, []; } elsif ($_) { push @{$blocks[$#blocks]}, $_; my $len = $#{$blocks[$#blocks]}; $maxlen = $len if $len $maxlen; } } __DATA__ #block1 aaa ddd #block2 bbb eee #block3 ccc fff -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Syncsave Integers 7Bit Data +1 sync bit... How do I get my value out of it?
On 6/22/05, Angerstein [EMAIL PROTECTED] wrote: The mp3 format uses something (sick) called syncsave integer. If you have a 4 Byte 32 Bit the very first bit of every Byte is used as a syncsave bit. so you can only put a 28 Bit long Number in it. Puting stuff in this format is the one thing and getting the right number out of it is the other thing I don´t know. I tried the uucode format transformer unpack(u, $buffer) but this don´t work. What you have to do is skip those ranges (between (2^7)+1 and 2^8, etc). #!/usr/bin/perl use strict; use warnings; my @data = ((2**8)-1, (2**16)-1, (2**24)-1, (2**32)-1); my @ss = (); my %gaps; for (my $i = 7; $i = 32; $i += 8) { $gaps{$i} = (2**($i+1)) - (2**$i); } # normal to synchsafe for my $n (@data) { my $ss = $n; for my $k (keys %gaps) { $ss += $gaps{$k} if $ss (2**$k); } print $n - $ss\n; push @ss, $ss; } # synchsafe to normal for my $ss (@ss) { my $n = $ss; for my $k (keys %gaps) { $n -= $gaps{$k} if $ss (2**$k); } print $ss - $n\n; } [1] http://www.id3.org/id3v2.4.0-structure.txt (section 6.2) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: how to append blocks in same file
On 6/22/05, Dan Klose [EMAIL PROTECTED] wrote: Using a hash of arrays will not necessarily preserve the order... If you don't want to worry about sorting the order of the blocks i.e. doing it yourself then use Tie::IXhash (something like that on CPAN) to preserve the order, you then don't have to worry about unbalanced blocks and reassembly by applying an if defined type block. Easy. Almost as easy as giving someone a complete working example to their homework assignment without them having to learn anything? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: hashes and loops
On 6/14/05, Karyn Williams [EMAIL PROTECTED] wrote: Below is code that I found on the web that I slightly modified. I am trying to create a script to remove from a file (tlist) the items in another file (tnames). This works but I have multiple files (tlist) I need to check against. I'm not sure how/where to put the loop and would appreciate any help. I am also wondering why the hash ? Does it work better in the script than an array and what are the keys in this case as the files (both) are just lists of e-mail addresses. If you're not set on using perl for all of this, grep (if available) can do a lot of the work for you: # create fake suppression lists perl -e 'print [EMAIL PROTECTED] for d .. p;' exclude1 perl -e 'print [EMAIL PROTECTED] for C .. F;' exclude2 # create fake email list perl -e 'print [EMAIL PROTECTED] for a .. z, A .. Z;' source # print emails in the list not in either suppression list grep -f exclude1 -f exclude2 -v source -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: @array =to= $array
my $localtime; @$localtime{qw / second minute hour mday month year weekday yearday isdst /} = localtime(time); [1] A style nit: Speaking of nitpicks: my %localtime; If you DO need to iterate across all the indices for an array ( rarely necessary if you have designed your data correctly ) do it right: my @person = ( qw / Freddy Mary Georgetta Loretta Fred Lawrence / ); ## ## UNBEARABLY LAME C ## for ( my $index = 0 ; $index @person ; $index ++ ) { print unbearably bad $person[$index]\n; } ## ## MERELY BAD PERL ## for my $index ( 0 .. $#person ) { print merely bad $person[$index]\n; } This is not Bad Perl. This is a solution to a problem that the following code will not solve. Sometimes you need $index, sometimes you don't. ## ## GETTING INTO THE PERL STATE OF MIND ## for my $person (@person) { print Hello $person\n; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: host id
On 6/10/05, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I whole heartedly agree! top post rules! Apparently, so does Lotus Notes. My deepest sympathies. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: host id
On 6/9/05, Wiggins d'Anconia [EMAIL PROTECTED] wrote: 4. Speed/Forking: because backticks causes a fork, you are using system resources in a way you wouldn't necessarily need to if you were able to use a built-in function. When Perl forks, it forks an exact copy of the running process and then transitions to the new command (at least on *nix systems) which may cause use of memory resources, file descriptors (which include open sockets to databases or possible remote locations), and other system level attributes that you wouldn't otherwise need. And the memory footprint of the running process includes all loaded modules so could be quite large. When running a forked system command, the perl interpreter has to fork, then exec the shell, the shell then has to parse the command line (which unless you have seen the parsing map you wouldn't believe how long this takes), then it has to fork and exec again into the running process, that process may then have to do its own option parsing, etc. which all could have been avoided by using the internal method. So it is almost always slower to call a system command when an internal method is available. Finally each call to system/backticks is independent, meaning that depending on the command being run and the optimizations of the alternatives there is no potential to use caching, session management, etc. to improve efficiency. Although system/backticks are written correctly in Perl 5, if you are using your own fork/exec model and don't include sufficient 'wait' code then your system may also become swamped by zombies, eventually causing a locked system, assuming you don't hit the memory limit first. You (well, I) learn something new every day. That's kind of funny, actually, (to me) that backticks load all the modules in use again just to run hostname. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: @array =to= $array
On 6/10/05, Lawrence Statton [EMAIL PROTECTED] wrote: my $localtime; @$localtime{qw / second minute hour mday month year weekday yearday isdst / } = localtime(time); [1] A style nit: Speaking of nitpicks: my %localtime; Assuming you are suggesting making that change, I'd reccomend against it, unless you wish to elicit the following error: Global symbol $localtime requires explicit package name at /tmp/try line 12. Global symbol $localtime requires explicit package name at /tmp/try line 14. Execution of /tmp/try aborted due to compilation errors. Huh. So it does... that doesn't look like it's making a reference, though. I think I would write that as @{$localtime}{ ... } for clarity. Computer software consists of only two components: ones and zeros, in roughly equal proportions. All that is required is to sort them into the correct order. Heh. I like that. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: @array =to= $array
On 6/10/05, Lawrence Statton [EMAIL PROTECTED] wrote: Huh. So it does... that doesn't look like it's making a reference, though. I think I would write that as @{$localtime}{ ... } for clarity. As to the clarity question. To my eyes, I find spurious {} tend to diminish rather than enhance, especially in common idioms, but I am wiling to accept it as a question of taste. When I mentioned clarity, I was referring to forcing precedence, even if it does not change the effect. I am, however, willing to accept the use of the word spurious as a question of opinion ;) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Favorite packages for benchmarking?
On 6/2/05, John W. Krahn [EMAIL PROTECTED] wrote: Siegfried Heintze wrote: There are lots of packages for date-time computations. What is the best one for timing computations for benchmarks? I'm thinking I want to fetch the time in 64 bit format instead of year, mo, day, hour, min, sec, nano seconds (which is what most of the date-time packages do). That should make subtraction faster. After I subtract two absolute times, then I want to display them in seconds. perldoc Benchmark perldoc Time::HiRes also check out 'perldoc -f times' -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Re: file parsers (fwd)
On 6/3/05, Chris Devers [EMAIL PROTECTED] wrote: This was really annoying. Please, please, please: do not sign up for a mailing list using one of these challenge/response email systems. It's a waste of everyone's time. +1 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Can't use subscript in angle brackets
foreach my $file ( @ARGV ){ if( my $fh = new IO::File $file ){ # Be explicit[1] if (my $fh = IO::File-new($file)) { push @infh, $fh; } else { die Failed to open input file '$file': $!; } } [1] http://www.perl.com/doc/manual/html/pod/perlobj.html#WARNING -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: efficiency of hash of hashes/lists
On 5/23/05, Peter Rabbitson [EMAIL PROTECTED] wrote: On Mon, May 23, 2005 at 01:40:08PM -0400, Zhenhai Duan wrote: I tried hash (where the members of a group are joined with :), and hash of hash. It happended that hash of hash is slower than single hash. Hash: $groups{$g1} = $member1:$member2; Hash of hash $groups{$g1}{$member1} = 1; Method 1 is faster, even I need to do a split to get the members. Can you post some code? Without it the above statement is not very credible to say the least. The 1D approach seems to be approximately 3 times as fast (on x86 Linux). Anyone get different results? #!/usr/bin/perl use strict; use warnings; my @ra = ('a' .. 'z', 'A' .. 'Z'); my ($seqlen, $hashsize) = (4, 7); my (%oned, %twod) = ((),()); my (@l1, @l2) = ((),()); $|++; print generating hashes; for my $i (1 .. $hashsize) { my $key1 = join('', @ra[map(int(rand($#ra))+1, 1 .. $seqlen)]); push @l1, $key1; my $key2 = join('', @ra[map(int(rand($#ra))+1, 1 .. $seqlen)]); push @l2, $key2; $oned{$key1:$key2}++; $twod{$key1}{$key2}++; print '.' if not $i % int($hashsize/10); } print !\n; # baseline my ($su, $ss) = times; for (1 .. $hashsize) { } my ($eu, $es) = times; my ($tu, $ts) = ($eu - $su, $es - $ss); my $tt = $tu + $ts; printf %20s %5.2f %5.2f %6.2f\n, 'base', $tu, $ts, $tt; # access test for 1d ($su, $ss) = times; for my $i (0 .. $hashsize-1) { $oned{$l1[$i]:$l2[$i]}++ } ($eu, $es) = times; ($tu, $ts) = ($eu - $su, $es - $ss); $tt = $tu + $ts; printf %20s %5.2f %5.2f %6.2f\n, '1D', $tu, $ts, $tt; # access test for 2d ($su, $ss) = times; for my $i (0 .. $hashsize-1) { $oned{$l1[$i]}{$l2[$i]}++ } ($eu, $es) = times; ($tu, $ts) = ($eu - $su, $es - $ss); $tt = $tu + $ts; printf %20s %5.2f %5.2f %6.2f\n, '2D', $tu, $ts, $tt; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: efficiency of hash of hashes/lists
# access test for 2d ($su, $ss) = times; for my $i (0 .. $hashsize-1) { $oned{$l1[$i]}{$l2[$i]}++ I think you should be operating on %twod here. LOL, thanks. Original poster take note: generating hashes..! base 0.03 0.00 0.03 1D 0.24 0.00 0.24 2D 0.22 0.00 0.22 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: A very general question to perl experts
sub readDefectData { my $defectDataFH=new FileHandle; open ($defectDataFH,$_[0]) or die Error: Cannot load defectivity data, $_[0]\n; print Loading defect data ... ; my %short; while ($_=$defectDataFH-getline) { chomp; print P8: $_\n; @_=split (/ /,$_); print P9: @_\n; if ($_[0]=~/SHORT/) { # performs some initializing kind of stuff and returns a hash to the caller. } } sub readLayerMap { } readDefectData has unbalanced braces... As far as coding style goes, I would suggest keeping everything as simple and generic as possible for maximum reusability. Also, code defensively and make sure you have a way to turn on lots of debug information about *exactly* what is happening when your code runs. Data::Dumper is your friend. One reason to choose perl over C/C++ is that you don't really have to. If you need to, you can call out to C using the XS interface if you find bottlenecks that are unacceptable. Premature optimization is the root of all evil. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Determine structure size
Is there a way to determine how much a certain data structure costs in terms of memory? In other words is there some built in command or module that takes a reference to a nested data structure and gives a ball park idea of how much memory this structure takes. http://www.perladvent.org/2002/6th/ - Devel::Size from: http://www.google.com/search?q=perl+%22data+structure%22+size+bytes -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Threads related document to refer in perl
I want to create threads using perl. So if any documents or links are there to refer please send me. Here's some pretty basic threading code: http://coding.derkeiler.com/Archive/Perl/perl.beginners/2004-10/0504.html -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Attempt to free unreferenced scalar
After running a program, I saw the following errors: Attempt to free unreferenced scalar: SV 0x38bd0a4, Perl interpreter: 0x162445c at E:/usr/lib/Errno.pm line 15 (#1) Attempt to free unreferenced scalar: SV 0x38bd0a4, Perl interpreter: 0x162445c at E:/usr/lib/Errno.pm line 15. The only time I've seen errors like that is in threading code that uses non-threadsafe modules. What does your program do? If you care enough, you might want to read up[1] and identify suspicious code, then try to write an example that consistently throws that error. [1] http://www.google.com/search?q=%22attempt+to+free+unreferenced+scalar%22+%22Perl+interpreter%22 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: looping over a tied hash problem
Dave, I've got some more code here that should explain exactly what I'm trying to do, what do you think of the structure? As you can probably tell I'm having problems accessing the keys and values from a tied hash, any ideas how I can get the keys and the values printed to the screen? Code: # sub get_users_table_and_columns{ my $self=shift; use Tie::IxHash; $self-{tables_and_columns}= Tie::IxHash-new(table='blog', column1 ='first_name', column2 = 'last_name', column3 = 'email', column4 = 'password', column5 = 'active' ); return $self-{tables_and_columns}; } in the cgi call the above method and pass it to the add_user method # $register-add_user($conf-get_users_table_and_columns, $first_name, $last_name, $email, $password); # sub add_user{ my $self=shift; my $tables_and_columns=shift; my $first_name=shift; my $last_name=shift; my $email=shift; my $password=shift; my $key=''; my $value=''; foreach my $value(@$tables_and_columns){ print value = $value\n; } } # This sounds highly overcomplicated... must you use Tie::IxHash? Could you possibly use something else? If you *must* use Tie::IxHash, you can do something like: use Data::Dumper; use Tie::IxHash; my $h = Tie::IxHash-new( table = 'blog', column1 = 'first_name', column2 = 'last_name', column3 = 'email', column4 = 'password', column5 = 'active', ); # this shows you the internal structure print Dumper($h); # this seems broken... print $_\n for keys %$h; print '-'x20, \n; # this should do what you want # breaks encapsulation tho print $_\n for @{$h-[1]}; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: looping over a tied hash problem
I'm kind of lost as to what you're actually trying to do. Instead of posting functions with nothing calling them, you should include a code snippet (as simple as possible) that can be run to demonstrate the problem. Preferably not using those functions. Fix one thing at a time. With that said, this might work: # sub add_user{ my $self=shift; my %tables_and_columns=shift; my $first_name=shift; my $last_name=shift; my $email=shift; my $password=shift; my @columns_and_tables=''; # try this: for my $value ( keys %{$self-{tables_and_columns}} ) { print $value\n; } } # -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: one-liner multi-line regex problem
I'm trying to write a perl one-liner that will edit an iCalendar format file to remove To Do items. The file contains several thousand lines, and I need to remove several multi-line blocks. The blocks to remove start with a line BEGIN:VTODO (without the quotes) and end with a line END:VTODO (also without quotes). I've tried the following one-liner, perl -p -i.bak -e 's/BEGIN:VTODO.*END:VTODO//sg' file_name_to_edit Assuming you have enough disk space: perl -ane 'print unless /^BEGIN:VTODO/ .. /^END:VTODO/' old new perldoc perlrun for more info on perl's command line paramaters -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: nested replace string
Please help me in where I am going wrong and suggest me the solution. Since you have the data in nice XML format, why not use an XML parser instead of parsing it yourself? http://perl-xml.sourceforge.net/faq/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Redirect on the pushing button
I am create edit data form, and it ask visitor: do you wish edit data? All is ok with Yes button, but I don't kniw what with No button :-) I wish that clicking NO button recirect me on some URL.. You can do this in perl by sending a Location header back to the browser before you send Content-type a la: #!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Carp 'fatalsToBrowser'; my $cgi = new CGI; my $form = $cgi-Vars; if ($form-{no}) { print Location:http://www.google.com/\n\n;; } # else proceed normally print Content-type:text/html\n\n; # ... __END__ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Redirect on the pushing button
You can do this in perl by sending a Location header back to the browser before you send Content-type a la: #!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Carp 'fatalsToBrowser'; my $cgi = new CGI; my $form = $cgi-Vars; if ($form-{no}) { print Location:http://www.google.com/\n\n;; exit; # for good measure } # else proceed normally print Content-type:text/html\n\n; # ... __END__ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: I think I need another regex...
I tried to do this with split(), but it's not working good at all The string: interface=something very long with spaces and all mac-address=00:02:6F:36:2D:31 ap=no wds=no rx-rate=11Mbps tx-rate=11Mbps packets=12623,18377 bytes=10829240,2009327 frames=12623,18377 (the above is one line, wrapped for email). I need to grab the values for all the options #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $data = 'your=data goes=here'; my %opts = (); $opts{$1} = $2 while $data =~ /([^= ]+)=([^=]+)(?= [\w-]+=)/g; print Dumper(\%opts); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: I think I need another regex...
On Thu, 17 Mar 2005 11:32:23 -0500, Dave Gray [EMAIL PROTECTED] wrote: I tried to do this with split(), but it's not working good at all The string: interface=something very long with spaces and all mac-address=00:02:6F:36:2D:31 ap=no wds=no rx-rate=11Mbps tx-rate=11Mbps packets=12623,18377 bytes=10829240,2009327 frames=12623,18377 (the above is one line, wrapped for email). I need to grab the values for all the options #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $data = 'your=data goes=here'; my %opts = (); $opts{$1} = $2 while $data =~ /([^= ]+)=([^=]+)(?= [\w-]+=)/g; print Dumper(\%opts); Er, that should be: $opts{$1} = $2 while $data =~ /([^= ]+)=([^=]+)(?:(?= [\w-]+=)|$)/g -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: [PBML] Query about perl modules
blade:~/personal/perl cat -n check.p 1 #!/usr/bin/perl 2 3 use Net::Telnet; 4 5 $timeout = 10; 6 $obj=new Net::Telnet( [Timeout = $timeout,] ); 7 blade:~/personal/perl perl check.p unknown remote host: ARRAY(0x22494) at check.p line 6 blade:~/personal/perl [Timeout = $timeout.] creates an array reference, which when printed out, in your case gave 'ARRAY(0x22494)'. If you check the docs for Net::Telnet[1], I think you'll find the answer to your problem in the first example. [1] http://search.cpan.org/~jrogers/Net-Telnet-3.03/lib/Net/Telnet.pm -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: z/OS unicode error.
Are you running with strict and warnings turned on? Because I'm getting Malformed UTF-8 character messages running this: #!/usr/bin/perl use strict; use warnings; my $u = unpackU0U, \x8a\x73; print \$u: $u\n; my $p = pack(U0U, $u); print \$p: $p\n; And I can get rid of those errors by changing the pack/unpack template to UU or U*... What are you trying to accomplish with U0U? I figured that the malformed character error that you mentioned, doesnt show up on z/OS while using 'U0U'. This is even if I run the same with 'use strict' and 'use warnings'. Also, the U0U indicates that the string of bytes passed to unpack has to be strictly in utf-8 unicode. Try U0U*, that seems to work better for me. Are you running the same on z/OS unix ? I am running this on z/OS v1R4. No, I'm running RH9... don't have access to a z/OS box. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: pack-unicode problem (z/OS)
I am running perl 5.8.6 on z/OS unix. I am doing these : $u = unpackU0U, \x8a\x73; print \n\$u : $u; $p = pack(U0U, $u); print \n\$p : $p; Are you running with strict and warnings turned on? Because I'm getting Malformed UTF-8 character messages running this: #!/usr/bin/perl use strict; use warnings; my $u = unpackU0U, \x8a\x73; print \$u: $u\n; my $p = pack(U0U, $u); print \$p: $p\n; And I can get rid of those errors by changing the pack/unpack template to UU or U*... What are you trying to accomplish with U0U? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Regex Multi line match
This is the multi line pattern in which I wish to match: tr tdb String 1.2.3.4.5.6 /b/td tdimg src=pics/green.gif alt=OK/td tr One way to solve this would be to read lines from the file and save chunks of N lines (4 in this case) in a temp variable. Then your regex would operate on enough of the file to have a chance of working. Something like (untested): my (@lines, $num) = ((), 4); while (INPUT) { push @lines, $_; shift @lines if @lines == $num+1; print 'lines '.($.-$num+1).' to '.($.). match\n if join('',@lines) =~ /regex goes here/; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Regex Multi line match
Something like (untested): my (@lines, $num) = ((), 4); while (INPUT) { push @lines, $_; shift @lines if @lines == $num+1; print 'lines '.($.-$num+1).' to '.($.). match\n if join('',@lines) =~ /regex goes here/; } That assumes that the pattern being searched for will begin 4n lines from the beginning of the file, but just because we're looking for four lines doesn't mean the file is written in four line chunks. In fact, it probably isn't. Er, no it doesn't. Read it again. It's a rolling n-line chunk of the file. Why don't you tell us what you're actually trying to do here; I'm guessing the goal isn't to search through a file for a literal string and then print it. If you knew what you were looking for, you wouldn't need to seach the file; you could just print it. So is the ultimate goal to perform a substitution? Count the number of occurrances? What? Now this I agree with. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Regex Multi line match
my (@lines, $num) = ((), 4); You are assigning the list ((), 4) to @lines and nothing to $num. Perhaps you meant: my ( $num, @lines ) = ( 4, () ); Or simply: my ( $num, @lines ) = 4; Indeed, good catch. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Compile perl code/files via STDIN
yes that is exactly what i mean. i want to do somthing like this: $ perl - c print Test\n; and now i want to get the infos of the compilation (sytax ok or errors...). after that i want to put the next perl code into the process, i don't want to close the process and reopen a new. I don't think syntax checking mode has a batch option... AFAIK it's just for one file at a time. What exactly are you doing that you're concerned about interpreter startup time when checking syntax? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Compile perl code/files via STDIN
i want to open a perl process and enter the code/files to compile via STDIN. i know it is possible. but how? does somebody know a tutorial or something like this which explains how i can compile perl code via STDIN? $ perl print Hello from STDIN\n; ^D Hello from STDIN $ If the answers so far don't solve your problem, it might help to be more specific about what exactly you're trying to do. Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: How to find regex at specific location on line
'plain_regex'= sub { if ( $string =~ /^.{38}\|[BNPG]\|/ ) { my $a = $_ } }, 'plain_regex'= sub { if ( $string =~ /^.{38}\|N\|/ ) { my $a = $_ } }, What was interesting to me was that although, predictably, the substring/regex combo was consistently the best performer for the original match, regexing the whole line was consistently the best performer when looking for |N|. This seems to fly in the face of the conventional wisdom that substr is faster than m// when you know what you're looking for and where you're looking for it. I believe the conventional (faster?) way to do a regex search when you want to start matching a fixed number of characters in is along the lines of: pos($string) = 38; print found!\n if $string =~ /\G\|[BNPG]\|/; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: PERL with grep
I would like the script to do the following: 1) List all *.java files containing following patterns: double, parseDouble Sounds to me like you don't need to resort to perl yet with this problem... egrep -rHis '\bdouble\b|\bparsedouble\b' /path/to/dir/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Dynamic pattern matching?
I know I can write an if() clause to match every possible case, but I'm wondering if there is a more general approach that would allow me to dynamically match a varying number of extra columns within a single expression. You could shove all the data points into one parenthetical group in the first regex and then process them inside the if statement. Something like: my $line = 'HGYPG5M1_LG OT 0.00E+00 2.00E-08 amps 1.000E-06 4.000E-112.000E-116.000E-114.000E-118.000E-11'; $line =~ s/[\r\n]/ /g; #line wrap # ... is everything else that i'm too lazy to type ;) if ($line =~ /^...(?:amps|volts)((?\s+[.0-9eE+-]+)+)$/) { # last backref contains all data points my (..., $datapoints) = (..., $6); my @datapoints = split //, $datapoints; # process based on (scalar @datapoints) } HTH, Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Regex help
On Mon, 17 Jan 2005 08:37:07 +0100, manfred [EMAIL PROTECTED] wrote: That leads me to a question :-) if ($num =~ /^(\d+)\#([^\#]*?)\#(?:e\+(\d+))?$/x) { What particular use has the _x_ modifier in this example? I mean the hashes are escaped? I forgot to remove the /x when I stripped the comments from the regex because if this was homework, I wanted the OP to work to understand it ;) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Substitute Varaible
On Mon, 17 Jan 2005 16:11:54 +0530, Anish Kumar K. [EMAIL PROTECTED] wrote: Hi I need help regarding substituion of varaibles with values Say I have a txt file (a.txt) which has only one line: Hi $name The PL file use strict; use warnings; open INPUT, a.txt; Always check the success of file opens! open INPUT, ' a.txt' or die couldn't read a.txt: $!\n; my $name=Anish; my $temp=; while (INPUT) { $temp=$temp.$_; This could also be written as: $temp .= $_; Personal preference, I guess. } close(INPUT); print Content is: $temp; In the output I want the $name to get as Anish..O/P like Hi Anish Check out the eval built-in: close(INPUT); # evaluate the content of $temp as perl[1] # this won't work if there are any double quotes in $temp $temp = eval qq/$temp/; There is probably a better way to do what you're trying to accomplish... can you give us a bigger picture? HTH, Dave [1] http://www.perldoc.com/perl5.8.4/pod/func/eval.html -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: regular expression question
I am new to perl, I receive some spam email with subject like st0ck, 0pportunities, gr0wth..., how can I match those words with number 0 in Something like __CODE__ use warnings; use strict; use Data::Dumper; # add to this hash to make it slower my %rep = ( 'a' = [4], 'e' = [3], 'i' = [1, '!'], 'o' = [0], # etc ); my @words = qw/stock opportunities growth/; # this generates regular expressions # you can use elsewhere for my $word (@words) { $word =~ s/$_/'(?:'. join('|', @{$rep{$_}},$_) .')'/gei for keys %rep; } print Dumper([EMAIL PROTECTED]); __END__ This will be very slow, though. HTH, Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Regex help
On Sat, 15 Jan 2005 01:25:21 -0800 (PST), Ajey Kulkarni [EMAIL PROTECTED] wrote: I'm trying to match a floating point in ada. They are normal floating points with 2 extra things.(they can or can't come) 1. an underscore is permitted between the digits and 2. An alternate numeric base may be specified surrounding the nonexponent part of the number with pound signs, precided by a base in decimal. Eg: 16#6.a7#e+2, 18.9, Sounds suspiciously like homework, but that's a fun problem. __CODE__ #!/usr/bin/perl use strict; use warnings; my @numbers = ( '16#6.f7#e+2', '18.9', '2#01013#', '16e+2', ); my @valid = (0 .. 9, 'a' .. 'z'); for my $num (@numbers) { my ($base, $n, $exp); if ($num =~ /^(\d+)\#([^\#]*?)\#(?:e\+(\d+))?$/x) { ($base, $n) = ($1, $2); $exp = defined $3 ? $3 : 1; } elsif ($num =~ /^(\d[\d._]*?)(?:e\+(\d+))?$/) { ($base, $n) = (10, $1); $exp = defined $2 ? $2 : 1; } next if not $n; my $invalid = '[^._'.join('',@valid[0..($base-1)]).']'; warn invalid base $base number [$n] detected! ($invalid)\n if $n =~ /$invalid/; print got base $base, num $n, exp $exp\n; } __END__ That should (more than) get you started! HTH, Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Regex lookahead problem
On Fri, 14 Jan 2005 07:11:30 +, Andrew Black [EMAIL PROTECTED] wrote: Can someone explain how lookaheads work and give an example of using one Cheers I find the full names of these regex constructs to be quite enlightening: zero-width (positive|negative) look-(ahead|behind) assertion. So for example, one way to commify numbers is: __CODE__ #!/usr/bin/perl use strict; use warnings; my @numbers = (100, 1000, 2536); for my $number (@numbers) { my $commified = commify($number); print before:\t$number\nafter:\t$commified\n---\n; } sub commify { my ($num) = @_; $num = reverse $num; $num =~ s/(\d{3}) # match 3 numbers (?!=,) # not followed by a comma (?=\d) # followed by another digit /$1,/gx; # stick a comma after what we matched return reverse $num; } __END__ HTH, Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Memory full
Here is an example of a program and a perl module that parses a .xls file and eats the whole memory. I have tried it under Linux and Windows and it has the same problem under both OSs, so it has big bugs. [snip] #Insert into database my $rapoarte_i = $dbh-prepare(insert ignore into temp_rapoarte values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)); foreach my $cod(sort keys %f) { foreach my $market(keys %{$f{$cod}}) { $rapoarte_i-execute(...); } } You should call $rapoarte_i-finish() here. I know if you're using Oracle, not finishing will keep cursors open and that could potentially be hogging memory like you're experiencing. Not sure about other databases. What database are you using? Let us know if this solves your problem. sub DESTROY { my $self = shift; undef $self; #I don't know if this sub helps, or if it is correctly written } This is correctly written, but it's kind of redundant... because if something is getting garbage collected, it's going to get undef'd anyway. Unless you have circular references or some other oddity (see Programming Perl v3, chapter 12). Which it doesn't look like is the case, at first glance. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Is GOTO evil?
On 6 Jan 2005 16:11:40 -, Peter Scott [EMAIL PROTECTED] wrote: The only times I've used goto in Perl have been the goto sub form, which isn't much of a goto in the first place :-) I use it the most when I'm debugging. For example, if I'm hacking on a webpage with a bunch of redirects, I'll stick a label at the very end of the page and goto that so I can print debug info when the page is in a certain state. goto sub is cool too :) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Copying a hash-of-hashes
The question is whether there is an elegant way to produce a complete copy of a hash-of-hashes-of-hashes-...-of-hashes for internal subroutine purposes and make sure that all references will be translated properly as well, leaving the subroutine no ability to modify the main hash. You might also look at Data::Dumper[1], which traverses a data structure and produces an eval-able result. [1] http://search.cpan.org/~ilyam/Data-Dumper-2.121/Dumper.pm -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Can someone translate a small .PY to Perl?
On Wed, 29 Dec 2004 12:50:21 -0500, GMane Python [EMAIL PROTECTED] wrote: while ( ) { That isn't doing what you expect, which (I assume) is an infinite loop. loops over @ARGV and attempts to open each arg as a file and iterate over the lines in each. I suppose it is functionally a somewhat infinite (heh) loop with no args, as it reads from STDIN until EOF. Except that it won't do anything until it can read something from STDIN. You probably want while (1) { ... } The rest looks ok at first glance, other than the variable declaration thing. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Can someone translate a small .PY to Perl?
was wondering if there were a translation in PERL so I could have my Netware servers send heartbeats to the heartbeat server? Title: PyHeartbeat - detecting inactive computers Submitter: Nicola Larosa # Filename: HeartbeatClient.py Heartbeat client, sends out an UDP packet periodically import socket, time SERVER_IP = '127.0.0.1'; SERVER_PORT = 43278; BEAT_PERIOD = 5 print ('Sending heartbeat to IP %s , port %d\n' 'press Ctrl-C to stop\n') % (SERVER_IP, SERVER_PORT) while True: hbSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) hbSocket.sendto('PyHB', (SERVER_IP, SERVER_PORT)) if __debug__: print 'Time: %s' % time.ctime() time.sleep(BEAT_PERIOD) Checkout Socket [1] and Time::HiRes [2], both of which should be installed already, depending on what version of perl you're working with. The rest should be fairly simple to translate by following the code examples. Let us know how that works out for you. [1] http://search.cpan.org/~nwclark/perl-5.8.6/ext/Socket/Socket.pm [2] http://search.cpan.org/~jhi/Time-HiRes-1.66/HiRes.pm -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: basic search engine
I am planning to write a simple search engine. The engine would be able to search for a word or phrases from a file and probably point the user the line number the word is found. The issue is I want the engine to be able to support more advance search techniques which means it could support keywords such as word1 and word2 word1 not word3 word1 or word4 Is there any module where I could simply plug into my problem to enable this functinality. Thanks. http://webglimpse.net/ might be more than you need, but it's worth a look. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: control loop question
Let's say I have a text file filled with: stuff stuff stuff Users sometext tom dick harry Users more sometext larry curly moe moe stuff stuff etc. First of all, can this be XML instead? Then you can pass off the parsing to XML::Simple or something similar. If not, it looks like you want to organize by whitespace. (Like Tor said, a group end token would help.) It also looks like you want to end up with something like: $VAR1 = { 'sometext' = { 'tom' = 1, 'dick' = 1, 'harry' = 1, 'more sometext' = { 'larry' = 1, 'curly' = 1, 'moe' = 1 } } }; But possibly not using hashrefs (because you have duplicates) and possibly not nesting references. Can you be more specific (in a general way) about your secret requirements? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: regexp for a blank line
Christopher Spears [mailto:[EMAIL PROTECTED] wrote: I have to write a script that processes text in a file. The text includes lots of blank lines. How can I tell Perl to skip the lines? On Tue, 14 Dec 2004 11:22:54 +0530, Mallik [EMAIL PROTECTED] wrote: next if ($line =~ /^\s*$/); +1 - this will match \n, \n, \t\n, \t \n, etc -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Should be a simple substitution?
I think the ?: must be extraneous: That construct lets the regex engine know that it doesn't need to worry about saving backreferences to the parenthesized group. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response