From nobody Sun Aug 25 10:05:00 2002 Newsgroups: comp.lang.perl.misc Subject: Re: Code for comparing two txt files & extracting/writing line(s) not matching References: Date: 25 Aug 2002 10:04:59 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 13 gamo writes: > There are no people intending to raise confusion. > People usually prefers more information than less. > I.e. if they probably need to remove spaces inside > the $id field may I tell something about or not? Now I'm confused, and I don't even remember the original question. -- Aaron From nobody Sun Aug 25 14:24:54 2002 Newsgroups: comp.lang.perl.misc Subject: Re: extract only lines beginning with double-quotes References: <3D63EBB6.F357B935@oracle.com> Date: 25 Aug 2002 14:24:52 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 22 tadmc@augustmail.com (Tad McClellan) writes: > while ( ) { > next unless /^"/; > my @fields = parse_line( ' ', 1, $_ ); > print "@fields[0, 1]\n"; His examples all have five fields, but he never said his data would always have five fields; he just said he wants to drop the final three fields. This will drop the final three whitespace-separated fields, regardless of how many there are: while(){ if( /^(".*\S)\s+\S+\s+\S+\s+\S+\s*$/ ){ print "$1\n"; } } -- Aaron From nobody Sun Aug 25 14:32:55 2002 Newsgroups: comp.lang.perl.misc Subject: Re: how to figure out what shell you are running inside of perl References: <508d0094.0208211538.7b082f9c@posting.google.com> Date: 25 Aug 2002 14:32:55 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 20 esp5@pge.com (Edward) writes: > I was trying to figure out what shell the perl script was running > in, > and so forth, but this *seems* to be impossible. No shells add an > environmental variable telling which shell they are, Mine do: [abaugher@linux]$ perl -e 'print $ENV{SHELL}, "\n"' /bin/bash [abaugher@freebsd]$ perl -e 'print $ENV{SHELL}, "\n"' /bin/sh -- Aaron From nobody Mon Sep 2 07:29:55 2002 Newsgroups: comp.lang.perl.misc Subject: Re: newbie regex question References: <3d72e0b5$1@news.eol.ca> Date: 02 Sep 2002 07:29:53 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 23 "Alex Shi" writes: > How to match an IP address in perl regex? An IP address is cmposed > by four 3-digit decimals separated by dot, and each of the four > numbers is within [0-255]. The last requirement made me sick. Any > one can help me? Thanks in advance! If this is homework and you're required to get it all in one regex, I'll let you do it yourself. :-) As a practical matter, I'd do it like this: if( /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ and $1 < 256 and $2 < 256 and $3 < 256 and $4 < 256 ){ print "$_ is an IP\n"; } else { print "$_ is NOT an IP\n"; } -- Aaron From nobody Tue Sep 3 06:49:51 2002 Newsgroups: comp.lang.perl.misc Subject: Re: Another newbie question... References: <3D749BC6.2B5743A9@iinet.net.au> Date: 03 Sep 2002 06:49:51 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 23 wabbit@iinet.net.au writes: > open(MAIL,"|/usr/sbin/sendmail -t") ||&ErrorMessage("Sendmail"); > print MAIL "Bcc: @addresses\n"; > print MAIL "From: $formdata{'from'}\n"; > print MAIL "Reply-To: $formdata{'from'}\n"; > print MAIL "Subject: $formdata{'subject'}\n\n"; > print MAIL "$formdata{'msgtext'}\n\n"; > to send my mail, the "from", "reply-to" and "subject" appear as text > in the message not in the header. Is there something wrong with > what I am doing? My guess would be that your last element in @addresses has a \n at the end of it, so that is causing \n\n there. Stick in a line like this to debug it: print join ':', @addresses, "\n"; -- Aaron From nobody Wed Sep 4 07:36:29 2002 Newsgroups: comp.lang.perl.misc Subject: Re: generating bar graph with perl References: <3d74fad4.214370758@news1.on.sympatico.ca> <3D756F58.58CEE307@purlgurl.net> Date: 04 Sep 2002 07:36:29 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 20 Purl Gurl writes: > Well gosh, your logic is totally whacked. > Adding a new bar each month tracks passage > of months, not passage of money through hands. > It would be most logical of you to add a new > bar for each unit of dollars collected, such > as, a new bar for every ten dollars or a new > bar for every one-hundred dollars. That would be a progress bar, but I think what the original poster wants is a 2-dimensional bar graph, with months along the X-axis and dollars on the Y-axis. GD::Graph should be a good tool for this. -- Aaron From nobody Wed Sep 4 07:43:19 2002 Newsgroups: comp.lang.perl.misc Subject: Re: Can this be done in perl? References: <8c028cb8.0209031944.6d0066a@posting.google.com> <3D75CEB3.1490FA53@schaffhausen.de> Date: 04 Sep 2002 07:43:19 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 19 "Tassilo v. Parseval" writes: > Zipping a file that is located on a remote FTP server? I'd like to > see that. Some ftp servers will compress files on the fly. If the file "myreport.txt" exists on the server, doing a "get myreport.txt.gz" will cause it to be compressed on the fly and sent as "myreport.txt.gz". Whether this is desirable depends on whether the saved bandwidth makes up for the extra CPU load of compressing and decompressing. It usually does. Really, I'm surprised that a 20MB file being made avaiable for download isn't compressed already anyway. -- Aaron From nobody Wed Sep 4 17:00:47 2002 Newsgroups: comp.lang.perl.misc Subject: Re: unexpected UNPACK results, why? References: <18sd9.127330$kp.763440@rwcrnsc52.ops.asp.att.net> Date: 04 Sep 2002 17:00:45 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 34 "Dick Penny" writes: > Am on a Win2K box, and don't understand the following output & > snippet. Can someone plse explain and tell me how to get integers > from my string? > string to unpack >0114200< > fmt=A7 >> pr=0114200 > fmt=I7 >> pr=875639088 > fmt=i7 >> pr=875639088 > fmt=L7 >> pr=875639088 > fmt=l7 >> pr=875639088 What result do you want? If you just want the individual integers in the string (0,1,1,4,2,0,0), just use (split //) as someone else suggested. If you want what I think of as ASCII character values, use 'C' in your unpack. Also, unpack returns a list, so by assigning it to a scalar, you're just getting the first value returned. use strict; use warnings; my $data='0114200'; print "string to unpack >$data<\n"; my $fmt = 'C*'; my @pr = unpack($fmt,$data); print "fmt=$fmt >> pr=@pr\n"; If that's not what you want, show us your real problem and what you've tried. -- Aaron From nobody Wed Sep 4 17:04:35 2002 Newsgroups: comp.lang.perl.misc Subject: Re: using split to grab a single field References: Date: 04 Sep 2002 17:04:35 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 23 mkirsch1@rochester.rr.com (Mathew Kirsch) writes: > Is there a less klugy way than this: > my $thing_that_I_want; > while (<>) { > my @temporary_variable = split; > $thing_that_I_want = @temporary_variable[2]; > } > All I want is the third field in the string, no matter what it > is. The data is of variable length, so substr() won't work. I can't > think of any other way to do this besides split(). split() is fine, but you can skip the temporary array: $thing_that_I_want = (split)[2]; -- Aaron From nobody Thu Sep 5 06:56:30 2002 Newsgroups: comp.lang.perl.misc Subject: Re: Array within an array References: Date: 05 Sep 2002 06:56:27 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 62 "Skarch" writes: > Say I have the following array... > > @data = "1,2,3,4,5,&,a,b,c,d,e,&,6,7,8,9,0,&,f,g,h,i,j," That's not an array. You either mean: $data = "1,2,3,4,5,&,a,b,c,d,e,&,6,7,8,9,0,&,f,g,h,i,j,"; or: @data = qw( 1 2 3 4 5 & a b c d e & 6 7 8 9 0 & f g h i j ); > I know that I can split it into 4 elements with the "&" but how do I > assign each element as a new seperate array? The number of > sub-arrays within @data will vary. > I'm guessing I need to use a foreach loop, but so far I can't get it > to work. The first case is simple. Just split the whole string on your delimiter, and then split each section on your sub-delimiter, to create a 2-D array (the Data::Dumper stuff below is so you can see the results nicely laid out). You'll get some empty strings in your final array, since you've got places where there are two delimiters in a row. use strict; use warnings; use Data::Dumper; my @final; my $data = "1,2,3,4,5,&,a,b,c,d,e,&,6,7,8,9,0,&,f,g,h,i,j,"; for (split '&', $data){ push @final, [split ',']; } print Dumper([@final]); print "\n\$final[1][1] = $final[1][1]\n"; The second one is a little different. You can't use split on an array, so do something like: use strict; use warnings; use Data::Dumper; my @final; my @data = qw( 1 2 3 4 5 & a b c d e & 6 7 8 9 0 & f g h i j ); my $c = 0; for (@data){ if( /&/ ){ $c++; next; } push @{$final[$c]}, $_; } print Dumper([@final]); print "\n\$final[1][1] = $final[1][1]\n"; -- Aaron From nobody Thu Sep 5 07:09:58 2002 Newsgroups: comp.lang.perl.misc Subject: Re: Hashes and Memory References: Date: 05 Sep 2002 07:09:56 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 33 jennykelly@hotmail.com (Jen Kelly) writes: > I have a problem with memory when using a hash. My hash is in the > form: [snip declaration of hash of arrays and code] > This is the error I get: > "Out of memory during request for 36 bytes, total sbrk() is > 263581696 bytes!" > I have no experience of memory allocation, and I have only been > using perl for a couple of weeks. I am going crazy! Is there > anything I can do? Back up a bit. Rather than turn one of your huge files into a long hash declaration in your perl script, just deal with the files as-is. Open one file (probably the second one, from your description) and take it line by line. For each line in the second file, search the first file for a match, and then make your change in the second file. If that takes too long because of the size of the files, another option would be to convert one or both files into a database for faster searching. You might also post a few lines of sample data from *both* files, and a consise description of what you want to happen to it. -- Aaron From nobody Fri Sep 6 06:57:56 2002 Newsgroups: comp.lang.perl.misc Subject: Re: Help with unpack References: <3D778A66.7260AB14@yahoo.com> Date: 06 Sep 2002 06:57:54 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 45 Mike Mimic writes: > I have such binary structure (which I read from file written by C++ > program): > > That is one byte which is ID (0x00, 0x01 ...), lenght of the string > which follows (0x00 is 0, 0x01 is 1 ...) and than the string whith > Lenght bytes. First, here's the data file I created to test with. The second byte is 07, so the string should be just the next seven bytes. $ hd d 00000000: 01 07 61 62, 61 75 67 68, 65 72 31 32, 33 34 35 36 ;..abaugher123456 $ perl open FILE, "d" or die $!; my $bytes; read FILE, $bytes, 2; my( $id, $len ) = unpack 'CC', $bytes; read FILE, $bytes, $len; my( $string ) = unpack 'A*', $bytes; printf "0x%02x %s\n", $id, $string; ^D 1 abaughe > What is the "best" way to get ID and String fields? There is no 'best' way in perl, but generally when you want to pull fixed-length stuff out of a chunk of data, unpack works well. > And, by the way, how can I convert a byte to its integer value in > Perl (in C++ is only int('a'))? I'm no C++ programmer, but I'm pretty sure that rounds off a value. To take the numerical value of a single byte, I think you use atoi() in C, and perl can do it with unpack with the 'C' template character. -- Aaron From nobody Sat Sep 7 06:28:45 2002 Newsgroups: comp.lang.perl.misc Subject: Re: NEWBIE: Hash question References: Date: 07 Sep 2002 06:28:43 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 34 "Nick Porter" writes: > I've been trying to get some form fields into a hash but the only > way I seem to be able to do it is like this; > if ($ENV{'REQUEST_METHOD'} eq "POST") { > read (STDIN, $mybuff, $ENV{'CONTENT_LENGTH'}); There's no need to do your own parsing, when the stock module CGI.pm will do it for you. > %passed_in = ( > $names[0] => $values[0], > $names[1] => $values[1], > $names[2] => $values[2], > ); > This looks terribly inefficient to me. Can anybody suggest a better > method. Yes, it is. Try something like: use CGI; # Use it, you'll like it! my $q = new CGI; # Create a CGI object my %f; # hash to hold form values for ($q->param()){ # iterate through the form parameters $f{$_} = $q->param($_); # assign each parameter value to the hash } # now %f holds all parameter names and values -- Aaron From nobody Sat Sep 7 08:39:23 2002 Newsgroups: comp.unix.questions Followup-To: comp.lang.perl.misc Subject: Re: Perl Question References: <9C8e9.25569$w51.7072592@news2.news.adelphia.net> Date: 07 Sep 2002 08:39:21 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 105 "Quantum Systems" writes: > I am trying to learn Perl scripting and is a bit (ok a lot) vague on > formatting. I am trying to write a script that with take the "who" > command as input, and outputs a formatted report on my screen. > Unfortunately, despite my best effort I am unable to get it to work. > Who someone please tell me what I am doing wrong? Sure. By the way, this is the wrong group for this; so followups will be set to comp.lang.perl.misc. > Here is the script. Thank for any input > #!/usr/local/bin/perl -w use strict; > system "who"; This runs the 'who' command in a sub-shell, sending the output to STDOUT. Probably not what you want. Try: open INFO, "who |" or die $!; > while ($line = ) { Where does INFO come from? Before you can read from a file descriptor, you have to create it somehow, like I do in the open above. When you ran your script, it died with "readline on unopened filehandle INFO" at that line. > ($userid, $terminal, $month, $day, $time, $location,) = split / /, $line; This splits line into fields divided by a single space. That doesn't work for me, since my 'who' returns multiple spaces between some things for formatting. Drop the temporary variable $line, and let split use the default of splitting on whitespace: while(){ ($userid, $terminal, $month, $day, $time, $location,) = split; } Or if you want to keep $line for some reason, split on /\s+/, which means "a chunk of whitespace." > chomp $location; Move this up before the split, and just chomp the line: chomp; > format STDOUT_TOP= > WHO COMMAND OUTPUT > USER TERMINAL DATE & TIME LOCATION > . > > format STDOUT = > > @<<<<<<< @<<<<<<<<< @<<< @<< @<<<<< @<<<<<<<<<<<< > $userid, $terminal, $month, $day, $time, > $location, > write STDOUT; > . You've got your write command inside your format declaration, so it's not being seen as a command. Switch these last two lines. Also, there's no need for all that extra spacing between your variables. Format/write takes care of that for you; if it didn't, you might as well use printf. Also, since you put your write() outside your while() loop, it'll only get executed once, with the values for $userid and so on that they got in the last loop. Move your format()s up before the loop, and move the write statement inside the loop, if you want to see all the users that 'who' returns. My working script is: -------------------------------------------------------- #!/usr/bin/perl -w format STDOUT_TOP= WHO COMMAND OUTPUT USER TERMINAL DATE & TIME LOCATION . format STDOUT = @<<<<<<< @<<<<<<<<< @<<< @<< @<<<<< @<<<<<<<<<<<< $userid, $terminal, $month, $day, $time, $location . open INFO, "who |" or die $!; while(){ ($userid, $terminal, $month, $day, $time, $location) = split; chomp $location; write STDOUT; } ----------------------------------------------------------- -- Aaron From nobody Mon Sep 9 07:07:55 2002 Newsgroups: comp.lang.perl.misc Subject: Re: The only thing I really regret about this forum.... References: Date: 09 Sep 2002 07:07:52 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 112 Carlos C. Gonzalez writes: > The one thing I don't like is the inability to see the posts one > after another as in a forum type of view. Where one can read each > post in context and look at all of them on the same web page as > though reading an online book. It sounds like you're using a newsreader that tries to make a newsgroup act like a set of web pages. I can imagine how that would be painful. Usenet is a threaded medium. The most obvious way to translate that to the web is to have a web page for each message, with links up and down the thread. Way too much clicking (and waiting, if you're still stuck with modem dialup like I am). Add to that the fact that most web-based forums wrap each message in a page of advertisements and other junk, and it's just way too slow even on a high-speed connection. > Instead I have to click here and there, click back here and there, > and view the posts through my little post viewing window of my > newsreader (it's not my reader - they are all like that). No they aren't. I never click with my newsreader (Gnus). I press 'space' to page through a message, 'n' to jump to the next message, 'f' to followup to a message, and 'Laep' to say "please don't ever show me this person's posts again." A threaded view of the group shows in a few lines above the message view, and all views are extremely configurable. It's exponentially faster than anything click-based. (Yet you can run it with a mouse, if you are keyboard-deficient in some way.) > At forums there is little of any quoting of previous posts in a > thread. Simply because it is very easy to scroll up and view each > of the previous posts. So there is no iratation resulting from > one's top posting or not quoting in their replies. That's ok for short threads, but it breaks down quickly on longer, many-layered threads. Let's say I start a thread, and it continues as follows, with indentation indicating the tree structure: Aaron Bart Carl Dave Evan Fred Gail Hank Ivan Jack In a forum, with these posts displayed in order without quoting for context, how can I tell that Ivan is replying to Carl's post from 6 posts above on the page, and not to Hank's post just above? How about if I read up to Gail's post a few days ago, and now Ivan's post shows up. I probably won't remember the context well enough to guess which previous post he was responding to, and the two posts may not even be on the same web page. In contrast, any decent newsreader (ADN) will show the thread structure clearly, so it's always obvious who is replying to whom. ADN will quote correctly, so Ivan's post will say something like "On such-and-such-a-day, Carl said:" and then quote the relevant portions of Carl's text. ADN will let you move up the thread to the parent of the current post (I hit '^' to do it) in cases where someone didn't quote or did it poorly. > The other thing is the public nature of this newsgroup which does > not require a sign in. A forum construct with log in still gets > it's posts into search engines - so there would no diminishing of > the posts for the Internet at large. But there would be a > corresponding decrease I think in the opportunity given to spammers > to collect email addresses and otherwise harass the newsgroup. That may be true if the forum provides no way to view the users' real addresses. Unfortunately, that means that to contact another user, you'd have to use the forum's message service, if there is one. And the only thing that sucks worse than web-based newsgroups is web-based email. > Just a few days ago I changed my email address to a real one at my > domain and wouldn't you know it? I started getting spammed almost > immediately. Can't say it happend at this newsgroup but it was > definitely a newsgroup of the one's I post to. It may have. I've always used my real address on Usenet, and I get quite a bit of spam (sometimes I get the same spam to 2-3 different addresses I have). Luckily I don't pay per-byte or per-minute for my Internet access, so it's not a big problem for me. My mail client (Gnus again) lets me easily shunt most Spam to a special folder based on a few key attributes, and I can also increase the score of authors I know, so spam ends up getting sorted to the bottom. The thing is, if you're trying to do business on the Internet -- or for any reason want people to be able to contact you -- you're going to have to put your email address out there somewhere. So for many of us, hiding our real addresses isn't an option. > I wonder if it might be good to consider moving this newsgroup into > a forum type of construct. I believe it would do a world of good > and increase the value of this newsgroup all the more. Add in a few > volunteer moderators (say one month for each moderator of 12) and > this newsgroup would almost certainly avoid most spam posts. Or > rude and obnoxious trolls and other type of malicious posters (of > which I have rarely experienced any thank goodness). To sum up my comments above: web-based forums suck. Some suck less than others, but compared to a Usenet newsgroup, they all suck. -- Aaron From nobody Fri Sep 27 06:54:30 2002 Newsgroups: comp.lang.perl.misc Subject: Re: Why does `format' remove trailing whitespace? References: <20020925220046.20741.00007276@mb-mo.aol.com> Date: 27 Sep 2002 06:54:30 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 14 "David K. Wall" writes: > That's reassuring. I've rarely used formats and didn't especially > enjoy the few times I did use them. I thought maybe it was only me > that thought they're usually more trouble than they're worth. Yep. I've always felt like I should use formats once in a while, just to expand my perlish-ness as much as possible. But they just never seemed that useful. -- Aaron From nobody Thu Oct 3 07:25:06 2002 Newsgroups: comp.lang.perl.misc Subject: Re: using Net::SMTP, server reports, "Software caused connection abort" References: <3d942b26.0@juno.wiesbaden.netsurf.de> Date: 03 Oct 2002 07:25:05 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 47 coder_1024@hotmail.com (coder_1024) writes: > > > if ($smtp == null) > Good point. I should change that. It does work though, but you're > right, its probably not the best style in the Perl world. In this case, it ends up doing what you want, but it doesn't 'work.' When you do a numeric comparison with things that aren't numeric, perl turns them all into zeroes first. So if $smtp is a valid reference, it gets turned into zero, and null, which perl decides you meant was a string, is also zero. So the == says 'yes.' The only way your if() would fail would be if $smtp had a numeric non-zero value. See: $ perl for ( 'stuff', undef, 0, 1 ){ if( $_ == null ){ print "true\n"; } else { print "false\n"; } } ^D true true true false $ So if your Net::SMTP constructor fails, and $smtp ends up undefined, your clause above will still be true. > I prefer printf because it doesn't go adding a \n to my string. Neither does print. > Actually, I have gotten it working both with and without the headers. > The headers actually aren't necessary. I did find that if I wanted to > provide a non-empty subject, that I did have to provide the headers. Right. The headers are only necessary if you want them to show up in the message itself. They aren't necessary to get it to deliver. -- Aaron From nobody Mon Jul 21 19:53:14 2003 Newsgroups: comp.lang.perl.misc Subject: Effective Group ID not changing Organization: ESC Date: Mon, 21 Jul 2003 19:53:14 -0500 Message-ID: <86wueb1ix1.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 25 I have a program that runs as root and accepts logins and commands through a network socket. On each login, it changes the effective uid and gid to that of the user, like so: my( $uid, $gid ) = (getpwnam($user))[2,3]; $> = $uid; $) = $gid; In most cases, $gid will be 1001, and $uid will be > 1000. This worked fine on FreeBSD, and files created after this point would be created with the uid/gid ownership assigned here. I recently tried running this program on RedHat (perl 5.6.1), and the gid doesn't change, although the uid does. If I print $), it still contains a list of root's groups, and files created get root's group ownership. Is there something I should be doing differently to get this to work? My program has to be able to reset these values repeatedly; that's why I don't set $( (Real gid). I figured I wouldn't be able to set it back after that. Thanks, -- Aaron From nobody Mon Mar 22 19:57:16 2004 Newsgroups: comp.lang.perl.misc Subject: Re: working example File::Taill References: <2gI6c.41274$HQ6.2880664@phobos.telenet-ops.be> <20040319152900.T21521@dishwasher.cs.rpi.edu> Organization: ESC Date: Mon, 22 Mar 2004 19:57:16 -0600 Message-ID: <86d6748gw3.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 15 Paul Lalli writes: > The only time the defined() would return a different response than > the simple boolean test is if the last line of the file contained > just the string "0" (without a newline). In this case, the boolean > test would return false, but a defined() test would return true. The last line could also be blank (just a newline), or "000000" or "0000.0000", or anything else that evaluates as false. Probably best to stick with defined() here. -- Aaron From nobody Mon Mar 22 21:25:52 2004 Newsgroups: comp.lang.perl.misc Subject: Re: Regular Expressions Question References: Organization: ESC Date: Mon, 22 Mar 2004 21:25:52 -0600 Message-ID: <868yhs8csf.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 20 Tore Aursand writes: > This one will print all lines, except those beginning with 'Diaplay': > > while ( ) { > next unless ( /^Display/ ); > print; > } That prints only lines that match. To print the ones that don't match: while ( ) { print unless ( /^Display/ ); } -- Aaron From nobody Mon Mar 22 21:31:30 2004 Newsgroups: comp.lang.perl.misc Subject: Re: FIFO problem - yet another .sig rot script... References: <7s7k501nlspece1k34henniocs9qqkj7vo@4ax.com> <405ABE29.36E4083D@acm.org> <3ptl5018889hcgkhml62auqe82ppmvtkl9@4ax.com> Organization: ESC Date: Mon, 22 Mar 2004 21:31:30 -0600 Message-ID: <864qsg8cj1.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 21 Michele Dondi writes: > Also, it *seems* that .signature is not checked to be a regular > file, since a symlink works too, but of course it may explicitly > check that it is *not* a FIFO, even if I can't understand why the > hell it should!! I've had that problem with Apache. It refuses to read from a FIFO, and just gives an error in the log about the object not being a regular file or directory. Irritating. I suppose one of these days I'll patch my copy to fix that. > I even tried a mid-hearted attempt at using a symlink to the actual > FIFO as the .sig file, but it fails quite as miserably... I tried that with Apache too. Didn't help. -- Aaron From nobody Tue Mar 23 12:51:00 2004 Newsgroups: comp.lang.perl.misc Subject: Re: working example File::Taill References: <2gI6c.41274$HQ6.2880664@phobos.telenet-ops.be> <20040319152900.T21521@dishwasher.cs.rpi.edu> <86d6748gw3.fsf@cail.baugher.pike.il.us> Organization: ESC Date: Tue, 23 Mar 2004 12:51:00 -0600 Message-ID: <86wu5b5re3.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 13 anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) writes: > None of these evaluate to false. A single "0" without a terminating > newline is the only type of line that may be missed. That is really > a malformed line, so it is customary to simply check for truth. Whoops; learned something today. I thought anything which equaled zero, like "0000.000", was false. Just tested it, and you're right. -- Aaron From nobody Fri Feb 3 12:43:50 2006 Newsgroups: comp.lang.perl.misc Subject: Re: Rename with wildcards? References: <1138987477.279248.301880@z14g2000cwz.googlegroups.com> Organization: ESC Date: Fri, 03 Feb 2006 12:43:50 -0600 Message-ID: <86d5i4ffkp.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 15 "Bill H" writes: > Can the following be done in Perl using the rename command (I looked > in the perldoc and it doesn't state if you can or can't use > wildcards). > rename "OLD*.*","NEW*.*"; Why not try it and see what happens? It doesn't work on my FreeBSD system, but see what it does on your DOS-based system. -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Sun Feb 5 10:08:56 2006 Newsgroups: comp.lang.perl.misc Subject: Re: Why does renaming a file not work when cron'd References: Organization: ESC Date: Sun, 05 Feb 2006 10:08:56 -0600 Message-ID: <86slqxdbzb.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 26 "gd" writes: > Can anyone tell me why the script will rename and save the file > properly when I run it from the command line but when I cron it it > does not When things work from the command line but not from cron, it usually means the cron job is running with different permissions or a different $PATH than you have when you're logged in. So first, are you running it as the same user as the owner of the cron job? If so, do you call any external programs or open any files without using the full path? > the line of code to rename the file is > > rename("wind.png","$data.png"); rename("wind.png","$data.png") or die $!; Are you sure your script has already chdir'd to the right place before doing this? -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Mon Feb 6 11:37:50 2006 Newsgroups: comp.lang.perl.misc Subject: Re: Search capability of web Personals is really horrible References: <1139182441.648596.323310@o13g2000cwo.googlegroups.com> Organization: ESC Date: Mon, 06 Feb 2006 11:37:49 -0600 Message-ID: <863biwcrrm.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 37 "surf" writes: > Some web sites like Yahoo have tons of people in their personals, > but the ability to search the profiles of people is so horribly > limited. Has anyone written some kind of Perl interface or some way > to do more advanced searches of personals ? I haven't for Yahoo, but I once wrote my own system for some other personals sites that had much worse search and display capabilities. The mining program broke down like this: Get a list of new profiles since the last run. (This part varies a lot from site to site.) Foreach profile, check it against my requirements (age, weight, whatever) If it matches, save the profile text to a file (generally with the extraneous ads and stuff stripped). Parse out the person's vital statistics and save them to a database. Fetch any pictures belonging to the profile and save them with a naming scheme matching them to the profile, and add links to them to the profile. This resulted in a nice database on my local system holding only the important information, an html file for each profile, and a set of images. Then I had a second program I'd run to look through them and delete the ones I wasn't interested in. None of it was that complicated. The hardest part was parsing through the HTML mess each site used on its profile pages -- figuring out what tags and text I could count on staying the same in every profile so I could parse out the right info. -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Tue Feb 7 09:24:40 2006 Newsgroups: comp.lang.perl.misc,comp.unix.shell Subject: Re: Regular Expression for 001-999 References: Organization: ESC Date: Tue, 07 Feb 2006 09:24:36 -0600 Message-ID: <8664nrb39n.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 23 Armin Gajda writes: > can someone tell me how to define a regular expression that matches > > L001-L999 OR P001-P009? > > I found: ^[LP]\d{3}$ > > But here the L000 and P000 is allowed. If this is a practical situation and not a homework assignment, I'd stick with your simple regex, and skip those two special cases with a second simple regex, something like: ( /^[LP]\d{3}$/ and not /000/ ) I don't know whether that'll run faster or slower, but it was certainly faster to code. -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Wed Feb 8 17:37:28 2006 Newsgroups: comp.lang.perl.misc Subject: Re: Help: How do I get the lists of groups a unix user is in? References: Organization: ESC Date: Wed, 08 Feb 2006 17:37:25 -0600 Message-ID: <863bita0cq.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 26 Brandon Hoppe writes: > I need the list of groups that a user is in, similar to calling > "groups" at a command prompt. The easy answer would be: my @groups = split / /, `groups $username`; If you want to do it all with perl functions, take a look at getgrent(). It'll iterate through the group file, so you can do something like this (untested): my $user = 'foobar'; # username we want the groups for my @groups; # list of groups to which $user belongs while(my($group, $passwd, $gid, $users) = getgrent ){ if( $users =~ /\b$user\b/ ){ push @groups, $group; } } -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Wed Feb 15 18:24:33 2006 Newsgroups: comp.lang.perl.misc Subject: Re: How to substitute "@" for "\@" in a string? References: <1139521072.793480.303560@z14g2000cwz.googlegroups.com> <090220061556453588%jgibson@mail.arc.nasa.gov> <1139530124.285534.167980@g14g2000cwa.googlegroups.com> <1139601914.785038.48410@z14g2000cwz.googlegroups.com> <1139945003.104324.168170@o13g2000cwo.googlegroups.com> Organization: ESC Date: Wed, 15 Feb 2006 18:24:33 -0600 Message-ID: <86y80c5ewu.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 21 "jbauza" writes: > $SendMail = "mail -s ' some text here' $MailUser"; > open( MAIL, "| $SendMail ") or die "$$: Can't run $SendMail: $!"; > select MAIL; > > $MailUser is read from a web form, I am not setting it. > Please let me know if there is a better way of doing this. In that case, you may want to consider what will happen if someone types something like this into that form field: someone@somewhere.com | rm -rf / Quoting your @ isn't your biggest problem. -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Wed Feb 15 18:34:25 2006 Newsgroups: comp.lang.perl.misc Subject: Re: use library References: <1139585729.767825.90970@f14g2000cwb.googlegroups.com> <1139589561.133273.87900@g43g2000cwa.googlegroups.com> <1139935881.277610.45480@f14g2000cwb.googlegroups.com> Organization: ESC Date: Wed, 15 Feb 2006 18:34:25 -0600 Message-ID: <86u0b05ege.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 14 Samwyse writes: > I wonder how hard it would be to auto-rate certain posters.... Easy, with Gnus: L a e p That lowers the score for the author of the current post. You could even lower the score of all posts have googlegroups.com in their references header: L r s p .*googlegroups.com.* -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Sun Feb 19 22:48:58 2006 Newsgroups: comp.lang.perl.misc Subject: Re: gunzip while downloading via ftp References: <1140316587.609593.223100@g47g2000cwa.googlegroups.com> <20060219140018.464$0e@newsreader.com> Organization: ESC Date: Sun, 19 Feb 2006 22:48:58 -0600 Message-ID: <86psli4oud.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 21 "John W. Kennedy" writes: > What hardware do you use, where gunzipping an 11GB file takes > negligible time? The point is that long uncompression time has nothing to do with whether your perl code is correct, so you're confusing the issue by even bringing it up. Even so, if your program will be run interactively (especially if as a CGI), you would be better off *not* adding that delay to your program. Better would be to spawn off a background process to decompress it, or dump it into a directory where a crontask occasionally decompresses any new files found there. Many FTP servers will decompress files on the fly as you download them, but that will cost CPU at the server end and bandwidth at both ends, so I wouldn't do that unless you own the FTP server. -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Mon Feb 20 08:47:16 2006 Newsgroups: comp.lang.perl.misc Subject: Re: How to make Perl's regex engine "halt" after a match References: <1140277710.726703.320690@z14g2000cwz.googlegroups.com> <1140278140.133686.96210@g14g2000cwa.googlegroups.com> <1140326607.026531.115690@o13g2000cwo.googlegroups.com> Organization: ESC Date: Mon, 20 Feb 2006 08:47:16 -0600 Message-ID: <86lkw63x57.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 24 anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) writes: > If you are happy with capturing only the first letter of each pair, > this will do: > > my @stack = $line =~ /(.)(?=\1)/g; > print "@stack\n"; > > c r t o r That was my idea: keep the regex simple by only having one capture, and then double them: my @stack = $line =~ /(\w)\1/g; $_ x= 2 for @stack; Not sure whether that would be faster than the other solutions. It makes the regex simpler, but adds a foreach loop instead of the maps and greps of the other solutions. -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Mon Feb 20 09:11:24 2006 Newsgroups: comp.lang.perl.misc Subject: Re: Looking for a trick with strings References: Organization: ESC Date: Mon, 20 Feb 2006 09:11:24 -0600 Message-ID: <86fyme3w0z.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 44 January Weiner writes: > I have strings (protein sequences, to be precise) that contain > letters + '-': > > my $s = 'Y---ERI-TTKDIV----EIKRHLDYLQAPRITNNDLE' ; # for example > my $s_nogaps = $s ; > $s_nogaps =~ s/-//g ; > > Then I get two numbers: > > my ($from, $length) = (0, 5) ; > > These are the indices of a substring within $s_nogaps (and not $s). > The above indices correspond to a string 'YERIT'. > > Now I would like to have fragments of $s corresponding to this > substring. For example, given the above indices, I should get > 'Y--ERI-T'. I'd start out the way you are: strip the hyphens from the line and get your substring. Then turn the substring into a search regex that will accept any number of hyphens between each letter. Tested: #!/usr/bin/perl -w use strict; my( $from, $length ) = (0,5); my $s = 'Y---ERI-TTKDIV----EIKRHLDYLQAPRITNNDLE'; ( my $nogaps = $s ) =~ s/-//g; my $string = substr( $nogaps, $from, $length ); my $search = join '-*', split //, $string; print "Search: $search\n"; $s =~ /($search)/; my $result = $1; print "Result: $result\n"; That could be simplified to use fewer lines and variables. -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Mon Feb 20 09:13:18 2006 Newsgroups: comp.lang.perl.misc Subject: Re: Pattern Matching on Case References: Organization: ESC Date: Mon, 20 Feb 2006 09:13:17 -0600 Message-ID: <86bqx23vxu.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 16 "Matt Garrish" writes: > But then consider: > I like toFormat everythingInside > cellsOn one line > Never underestimate a bad html parsing job... : ) Yep. When I really want to get the visible text of a page without the html, `lynx -dump $url` comes in handy. -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Mon Feb 20 09:22:01 2006 Newsgroups: comp.lang.perl.misc Subject: Re: File search & replace References: <1140434184.603951.228050@z14g2000cwz.googlegroups.com> Organization: ESC Date: Mon, 20 Feb 2006 09:22:00 -0600 Message-ID: <867j7q3vjb.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 26 axl.rajesh@gmail.com writes: > I have a requirement, where I need to do recursive search and > replace for files of around 50,000 lines of code. currently I wrote > the following perl script and using it for searching the file and > replace. I don't know how easily this translates to Windows, but on Unix, it's very simple: perl -i -p -e 's/search/replace/g' * That assumes, like your sample code does, that the search pattern never spans multiple lines, and that all the files are in one directory. If you aren't extremely sure that your search/replace will do what you expect, you can make a backup of each file: perl -i.bak -p -e 's/search/replace/g' * and then easily delete all the *.bak files after you check the changed ones to make sure you didn't screw them up. -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Mon Feb 20 09:28:29 2006 Newsgroups: comp.lang.perl.misc Subject: Re: gunzip while downloading via ftp References: <1140316587.609593.223100@g47g2000cwa.googlegroups.com> <20060219140018.464$0e@newsreader.com> <20060219154313.930$xG@newsreader.com> Organization: ESC Date: Mon, 20 Feb 2006 09:28:29 -0600 Message-ID: <863bie3v8i.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 18 "A. Sinan Unur" <1usa@llenroc.ude.invalid> writes: > Can gunzip start decompressing before it has seen the whole file? I > don't know the format very well. Yes. That's why you can do either of these: gunzip file.gz gunzip -c file The first one eliminates file.gz after creating file; the second one does not, since gunzip is getting file.gz streamed from stdin and doesn't even know it exists as a file. -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Tue Feb 21 09:16:30 2006 Newsgroups: comp.lang.perl.misc Subject: Re: Text file splitter, date/time field References: <1138702258.075157.277250@g44g2000cwa.googlegroups.com> <1140187205.361247.173780@g43g2000cwa.googlegroups.com> Organization: ESC Date: Tue, 21 Feb 2006 09:16:29 -0600 Message-ID: <86oe103foy.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 39 "Throw" writes: > The solution given to this question is exactly what I'm looking for, > except I need to split a concatenated PHP file. Basically, I have one > large text file into which I have copied PHP file after PHP file, and > now I want to split them up again. The PHP file always begins with > > > and always ends with > > ?> There are two main ways to do this. Either read the entire file into one variable, and then do a regex within a while loop on the entire thing, treating it as one line and looking for your sample text (what I think of as the brute force approach, since it is the quickest to code but requires reading the entire file into memory at once, which could be bad for a very large file): my $line = join '', ; my $count = 1; while( $line =~ /(<\?php.+?\?>\s*)/gs ){ my $chunk = $1; open my $out, ">", "$count.php" or die $!; print $out $chunk; close $out or die $!; $count++; } The other option would be to read through the original file line by line, starting a new file when you hit a , writing all the lines between to said file. It's similar to the above, so you can probably work it out for yourself. -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Tue Feb 21 11:25:56 2006 Newsgroups: comp.lang.perl.misc Subject: Re: Padding removal went wrong References: <1140538092.457551.242820@f14g2000cwb.googlegroups.com> Organization: ESC Date: Tue, 21 Feb 2006 11:25:55 -0600 Message-ID: <86k6bo39p8.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 61 ofer@ilunix.org writes: > The problem is that I want to encrypt strings which are not exactly 8 > bytes, or a whole multiplication of 8. > What I did was to pad the last, incomplete buffer. > If I have this string: > XXXX > I just pad the buffer like this: > XXXX4444 > some more examples: > XX -> XX666666 > XXX -> XXX55555 > X -> X7777777 > XXXXXXX -> XXXXXXX1 > > I hope you get it. > > Okay, so that went great. > Now I want to decrypt it. So first I decrypt, and then I remove the > padding. Your code that removes the padding works fine: my $block_out = 'XXX55555'; # my line my $pad_len = substr( $block_out, -1 ); substr( $block_out, -$pad_len ) = ""; print "\$pad_len = $pad_len, \$block_out = $block_out\n"; # my line results in: $pad_len = 5, $block_out = XXX So either $block_out doesn't contain what you think it does, or $last_block isn't getting set and your code isn't getting executed. Stick a print line in there, like 'print "Removing padding from $block_out\n"', and see if/what it prints. I'm guessing your problem is that $last_block isn't being set in this earlier code when you think it is: my $did_read = read( IN, $block, $block_size - length( $block ), length( $block ) ); defined( $did_read ) || die( "Reading from $fn failed ($!)" ); $did_read == 0 && do { $last_block = 1; }; because your file contains an even multiple of 8 bytes, so end-of-file will be reached *after* you read that last block of 8. By the way, that's some pretty painful code. It'd be much more perlish to replace the last three lines with: $last_block = 1 unless $did_read; However, it'd still be broken. In fact, that whole part is a mess; I'd scrap it and start over. -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Wed Feb 22 07:25:53 2006 Newsgroups: comp.lang.perl.misc Subject: Re: Split, variable delimiter References: <1140545516.252582.175540@f14g2000cwb.googlegroups.com> <1140549958.900887.213140@f14g2000cwb.googlegroups.com> <1140555006.143228.64070@g47g2000cwa.googlegroups.com> Organization: ESC Date: Wed, 22 Feb 2006 07:25:52 -0600 Message-ID: <86fymb34pr.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 28 Uri Guttman writes: > rtfm some more please. first off you want \s+ to get something like > split ' '. and ' ' is not only special cased for splitting on any > whitespace but it emulates awk's behavior of skipping leading white > space. The documentation explains this quite well. However, it never says that ' ' can't be passed in a variable, as far as I could find. Since this is such a special case (Is there any other case in perl when this is true?), perhaps the documentation should have a few lines added to make that clear. Something like: This special case only works when a single space is given to split literally. Passing a single space in a variable causes split to split on a single space, instead of using this special behavior. $delim = ' '; @s = split $delim, ' A B CD E'; Results in @s = ( '', 'A', 'B', 'CD', 'E' ); I'm sure that could be worded more accurately. -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Wed Feb 22 07:34:47 2006 Newsgroups: comp.lang.perl.misc Subject: Re: savely change permission and group on files References: <43fc54b1$0$12109$ba624c82@nntp02.dk.telia.net> Organization: ESC Date: Wed, 22 Feb 2006 07:34:46 -0600 Message-ID: <86bqwz34ax.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 21 Lars Madsen writes: > Solution: Put the necessary people in a special group and make sure > that the files are writable for that group. That's fine, but people > tend to forget setting permissions or changing groups, so I'd like > to have a cron job that goes through all files in a specific > directory and set the group and permissions on all files in this > directory-tree. Not really a perl problem: #!/bin/sh if cd /wherever; then chgrp -R ourgroup . chmod -R g+rw . fi -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Wed Feb 22 16:19:11 2006 Newsgroups: comp.lang.perl.misc Subject: Re: savely change permission and group on files References: <43fc54b1$0$12109$ba624c82@nntp02.dk.telia.net> <86bqwz34ax.fsf@cail.baugher.pike.il.us> <43fcc0dc$0$12399$ba624c82@nntp02.dk.telia.net> Organization: ESC Date: Wed, 22 Feb 2006 16:19:11 -0600 Message-ID: <867j7n2g0w.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 25 Lars Madsen writes: > Aaron Baugher wrote: >> Not really a perl problem: >> #!/bin/sh >> if cd /wherever; then >> chgrp -R ourgroup . >> chmod -R g+rw . >> fi >> > yes that's true, but real life is of cource not as simple as the > case I described. > One problem with this is that it allows users to create directories > in /whatever (creating them in subdirectories are fine) which we > don't want. So add the line 'chmod g-w .' right before the 'fi' line to remove group write permissions from the top directory. -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Thu Feb 23 10:58:42 2006 Newsgroups: comp.lang.perl.misc Subject: Re: Split, variable delimiter References: <1140545516.252582.175540@f14g2000cwb.googlegroups.com> <1140549958.900887.213140@f14g2000cwb.googlegroups.com> <1140555006.143228.64070@g47g2000cwa.googlegroups.com> <1140701020.844871.43260@g43g2000cwa.googlegroups.com> <1140701292.719524.277130@z34g2000cwc.googlegroups.com> <1140701572.486901.251970@f14g2000cwb.googlegroups.com> Organization: ESC Date: Thu, 23 Feb 2006 10:58:41 -0600 Message-ID: <86fyma2eri.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 29 "Paul Lalli" writes: > it_says_BALLS_on_your_forehead wrote: >> Paul Lalli wrote: >> > Uri Guttman wrote: >> > > s/your/you're/. please spell your insults correctly if you want them to >> > > have the maximum effect. >> > >> > While I do, of course, agree whole-heartily with your entire post, I >> > can help but point out my amusement that someone who flat out refuses >> > to use the shift key to correctly capitalize his sentences is >> > complaining about someone else's spelling... ;-) >> >> you "can help" but choose not to, apparently ;-). > > You know... I went over that post 3 times before submitting, because > I *really* wanted to avoid anyone calling me on my calling someone > else on their calling someone else on grammatical and spelling > mistakes.... and I completely missed an entire lack of "'t". > Sheesh. That's one of the immutable laws of nature: when posting a grammar or spelling flame, you're guaranteed to make a similar error of your own. Usually a worse one. -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Wed Mar 1 00:18:57 2006 Newsgroups: comp.lang.perl.misc Subject: Re: Perl array and ASP session variable References: <1141026336.991393.106990@v46g2000cwv.googlegroups.com> <25LMf.2877$972.81139@news20.bellglobal.com> <1141092013.886377.120100@u72g2000cwu.googlegroups.com> Organization: ESC Date: Wed, 01 Mar 2006 00:18:57 -0600 Message-ID: <86hd6iwuum.fsf@cail.baugher.pike.il.us> berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 19 "Chris Johnson" writes: > Matt Garrish wrote: >> Not if it's the MS flavour; you can't store a reference to anything >> inside a session variable (which may not be true for Apache::ASP, >> but I've only used the former). > Well then I've no idea. As far as I know that's the only way to > store an array in a scalar. Storable.pm will let you store a complex data structure in a scalar. Maybe then ASP would let you save that scalar, which could then be retrieved and converted back into the original structure (array, in this case) with Storable. -- Aaron -- aaron_baugher@yahoo.com http://360.yahoo.com/aaron_baugher From nobody Mon Jan 18 18:37:09 2016 Newsgroups: comp.lang.perl.misc Subject: Re: Small utility: agedir Organization: Baugher Consulting References: Date: Mon, 18 Jan 2016 18:37:09 -0600 Message-ID: <86powyo156.fsf@bannor.baugher.biz> MIME-Version: 1.0 Content-Type: text/plain Lines: 15 tmcd@panix.com (Tim McDaniel) writes: > To show the newest item and oldest item in a directory, I prefer > > ls -lt | head -n 2 > ls -lt | tail -n 1 If you'd prefer a single command, there's: ls -lt | awk 'NR == 2 {print} END {print}' -- From nobody Mon Mar 21 09:48:38 2016 Newsgroups: comp.lang.perl.misc Subject: Re: Add multiple values of hash key Organization: Baugher Consulting References: <07eb4c7b-9485-4e98-a2f8-9f9988e2a803@googlegroups.com> Date: Mon, 21 Mar 2016 09:48:37 -0500 Message-ID: <86twjznayi.fsf@bannor.baugher.biz> MIME-Version: 1.0 Content-Type: text/plain Lines: 41 F Massion writes: > I have built a hash with multiple values per key. The values are numeric and > I would like to add all the values per key get a list of key with the sum of > values. When it comes to add the values I get stuck. Anyone has a hint? Her > my code: You could write a simple function that returns the sum of its arguments (or use sum() from List::Util), and pass each key's array to it. For example: #!/usr/bin/env perl use 5.010; use warnings; use strict; my %hash; foreach my $line () { chomp $line; my ($key1, $key2, $value) = split /\t/, $line; push @{$hash{"$key1\t$key2"}}, $value; } foreach my $key (sort keys %hash) { say "$key: ", sum(@{$hash{$key}}); } sub sum { my $n = shift @_; return $n unless @_; return $n + sum(@_); } __DATA__ RecordID001 adjustment washer 100 RecordID001 adjustment washer 1 RecordID001 adjustment washer 2 RecordID001 adjustment washer 2 RecordID002 scraper plate 0 RecordID003 scraper plate, lh side 6 RecordID004 scraper plate, rh side 50 RecordID004 scraper plate, rh side 51 -- From nobody Wed Apr 6 10:09:43 2016 Newsgroups: comp.lang.perl.misc Subject: Re: Substitution and alignment Organization: Baugher Consulting References: Date: Wed, 06 Apr 2016 10:09:43 -0500 Message-ID: <86zit6db9k.fsf@bannor.baugher.biz> MIME-Version: 1.0 Content-Type: text/plain Lines: 16 hymie! writes: > If you don't, then you probably can't do this in a one-liner. Well, you *shouldn't* do this with a one-liner unless you have to. So, presumed homework achievement unlocked (all this on one line): perl -ane 'for (0..@F){$l=length($F[$_]);$w[$_]=$w[$_]<$l?$l:$w[$_];} push @a,[@F]; END{ for $l (@a){ print join(" | ", map {sprintf "%$w[$_]s",$l->[$_]} 0..@$l), "\n"; }}' Date: Sun, 24 Apr 2016 04:39:44 -0500 Message-ID: <86h9erpcpr.fsf@bannor.baugher.biz> MIME-Version: 1.0 Content-Type: text/plain Lines: 65 DFS writes: > Thanks DollarBill. That does help. I actually expected perl to handle a > list like that a lot more gracefully than what you described. It can be as elegant as you want; that's up to you. Perl's not really a "here, you (the language) figure it all out for me" type of language, though there might be a module that would do that in this case. But for what you describe, I'd pull the records into an array of hashes. That way you don't require any particular field or combination of fields to be unique. Then you can make a subroutine that sorts on any field, in either direction. See the code below for an example which sorts your data ascending on the zip-code field. Since you talked about 'objects': you certainly could wrap it up in object-oriented code, with methods to add/sort/print records. That seems like overkill for this task, but it's an option. #!/usr/bin/env perl use 5.010; use warnings; use strict; my @a; while(){ my %h; chomp; ($h{name},$h{addr},$h{city},$h{st},$h{zip}) = split /,\s*/; push @a, \%h; } printf( "%-30s %-30s %-15s %-3s %-6s\n", $_->{name}, $_->{addr}, $_->{city}, $_->{st}, $_->{zip}) for sort_on_field(\@a, 'zip', 1); # <-- set sort key and direction # sort array of hashes # arguments: # $array - reference to array of hashes to be sorted # $field - name of hash key of field to sort on # @asc - direction to sort: 1=ascending, 0=descending sub sort_on_field { my $array = shift; my $field = shift; my $asc = shift; $asc = $asc ? 1 : -1; return sort { $asc * ($a->{$field} cmp $b->{$field}) } @$array; } __DATA__ Johnny's Pizza,2023 Grayson Hwy Ste 110,Grayson, GA, 30027 KFC,1855 Grayson Hwy,Grayson, GA, 30017 KFC,1855 Grayson Hwy,Grayson, GA, 30027 Little Caesars Pizza,1950 Grayson Hwy,Grayson, GA, 30017 Little Caesars Pizza,1950 Grayson Hwy,Tucker, GA, 30027 Little Caesars Pizza Ltd,1950 Grayson Hwy,Grayson, GA, 30017 McDonald's,1881 Grayson Hwy,Des Moines, IA, 30017 McDonald's,1881 Grayson Hwy,Grayson, GA, 30017 My Pizza Buffet,2944 Rosebud Rd Ste D,Loganville, GA, 30052 -- From nobody Wed Apr 27 08:31:42 2016 Newsgroups: comp.lang.perl.misc Subject: Re: perl equivalent of a MS Recordset object Organization: Baugher Consulting References: <86h9erpcpr.fsf@bannor.baugher.biz> <87potbc10j.fsf@stemsystems.com> Date: Wed, 27 Apr 2016 08:31:41 -0500 Message-ID: <86h9enmb42.fsf@bannor.baugher.biz> MIME-Version: 1.0 Content-Type: text/plain Lines: 20 Uri Guttman writes: > > AB> while(){ > AB> my %h; > AB> chomp; > AB> ($h{name},$h{addr},$h{city},$h{st},$h{zip}) = split /,\s*/; > > @h{ qw( name addr city st zip ) } = split /,\s*/; > > isn't that nicer? it is why slices are in perl. :) Yep. Much more elegant than mine. When it's a hash reference, though, instead a hash (as mine was lower down in the code), I always have to stop and think: wait, where do I put the curly brackets again to make this work? -- From nobody Wed Apr 27 12:37:11 2016 Newsgroups: comp.lang.perl.misc Subject: Re: perl equivalent of a MS Recordset object Organization: Baugher Consulting References: <86h9erpcpr.fsf@bannor.baugher.biz> <87potbc10j.fsf@stemsystems.com> <86h9enmb42.fsf@bannor.baugher.biz> <878tzz5a1g.fsf@doppelsaurus.mobileactivedefense.com> Date: Wed, 27 Apr 2016 12:37:11 -0500 Message-ID: <868tzzlzqw.fsf@bannor.baugher.biz> MIME-Version: 1.0 Content-Type: text/plain Lines: 30 Rainer Weikusat writes: > As per 'using references' rule #1: A simple, scalar variable holding a > references of an appropriate type can be used instead of an identifier > in an expression: > > [rw@doppelsaurus]/tmp#perl -e '@$h{qw(a b c)} = (1,2,3); > print("$_ -> $h->{$_}\n") for keys %$h' > c -> 3 a -> 1 b -> 2 Interesting. I could have sworn I had to use curly brackets around the hashref, like this: my %h = ( a => 1, b => 2, c => 3, d => 4 ); my $r = \%h; say $_ for @h{qw/ a b c /}; say $_ for @{$r}{qw/ a b c /}; But as per your example, this works just as well: say $_ for @$r{qw/ a b c /}; Maybe I had a situation where the hashref was more complicated, like the member of a larger structure, so I needed the curly brackets for precedence. Thanks for correcting me there. -- From nobody Wed Apr 27 12:45:58 2016 Newsgroups: comp.lang.perl.misc Subject: Re: perl equivalent of a MS Recordset object Organization: Baugher Consulting References: <86h9erpcpr.fsf@bannor.baugher.biz> <87oa8xnaq9.fsf@doppelsaurus.mobileactivedefense.com> Date: Wed, 27 Apr 2016 12:45:58 -0500 Message-ID: <864manlzc9.fsf@bannor.baugher.biz> MIME-Version: 1.0 Content-Type: text/plain Lines: 21 John Black writes: > Also, does each loop iteration of "my %h" create a new %h which does not > overwrite the previous one called %h such that the references pushed into > @a all point to different hashes? It must, but I thought the only way to > do that was to explictly do something like this: h_ref = {}; to construct > a new hash each time? A new %h is created inside each loop, but since I push a reference to that hash onto an array which exists outside the loop, Perl doesn't destroy it when the loop finishes. The local name goes away when each loop ends, but the data still exists and is maintained because there's still a reference to it. So you can't access each hash by the name %h outside the loop; you can only get it it through the array of references. I may be butchering the terminology a bit, but that's the gist. There's more info on references in 'perldoc perlref'. -- From nobody Wed Apr 27 17:07:03 2016 Newsgroups: comp.lang.perl.misc Subject: Re: perl equivalent of a MS Recordset object Organization: Baugher Consulting References: <86h9erpcpr.fsf@bannor.baugher.biz> <87potbc10j.fsf@stemsystems.com> <86h9enmb42.fsf@bannor.baugher.biz> <878tzz5a1g.fsf@doppelsaurus.mobileactivedefense.com> <868tzzlzqw.fsf@bannor.baugher.biz> <87r3dq5073.fsf@doppelsaurus.mobileactivedefense.com> Date: Wed, 27 Apr 2016 17:07:03 -0500 Message-ID: <86lh3yln94.fsf@bannor.baugher.biz> MIME-Version: 1.0 Content-Type: text/plain Lines: 18 Rainer Weikusat writes: > Perl doesn't really have a dereferencing operator, hence, derefencing > syntax doesn't have a precedence. I was thinking of something like this, where the hash reference is a sub-element of a larger structure, in this case $a->[0]. The first attempt without curly brackets around the reference itself throws an error because perl applies the @ before the arrow; the second line clears it up and works. my $a = [{ a=>1, b=>2, c=>3 }, { d=>4, e=>5, f=>6 }]; say $_ for @$a->[0]{qw/ a b /}; say $_ for @{$a->[0]}{qw/ a b /};