Re: Need help with Data Structure: array of hash

2012-11-23 Thread Andy_Bach
> I am having problem with array and Hash data structure. Example is show 
below:

#CODE 1
my @list;
my %this;
$this{x} = 1;
$this{y} = 2;
$this{z} = 3;
$this{Z} = 4;

Better (maybe):
my %this = (
x => 1,
y => 2,
z => 3,
Z => 4,
  )


push @list, %this;
> The last line does not work according to what I want it to do.
> And instead, @list now contains:
$list[0] = 'x';

The problem here is the list context 'unrolls' the "this" hash to 
(x,1,y,2,z,3,Z,4)

so it's a simple list pushed into the array.


>My intention above is to have variable @list as an array of hash, ie:
$list[0]{x}
$list[0]{y}
$list[0]{z}
$list[0]{Z}

This works:
$list[0]{x} = 1;
$list[0]{y} = 2;
...
print("$_: ", $list[0]->{$_}, "\n") foreach keys %{$list[0]};



> I have resorted to:
@list[ scalar @list ] = %this;
> which I find not so "elegant"...

Er, that doesn't work for me. For one thing "scalar @list" will be zero on 
the LHS of the assignment, but it'll be 1 afterwards.  What you want is an 
"anonymous" hash as the element so
$list[0] = {
   x => 1,
   y => 2,
   z => 3,
   Z => 4,
};

Note curly brackets, or
my %this = (
   x => 1,
   y => 2,
   z => 3,
   Z => 4,
);
my @list;
@list[ 0 ] = \%this;

or:
push @list, \%this;


> Secondly, is there a way to not use the has variable %this above?
> ie, something like
push @list, {x=>1,y=>2,z=>3,Z=>4};

seems to work here:
my @list;
push @list, {x=>1,y=>2,z=>3,Z=>4};
print("k $_: ", $list[0]->{$_}, "\n") foreach keys %{$list[0]};

> And lastly, as I try to pop it out:
%this = pop @list;
printf "%d %d %d %d\n", $this{x}, $this{y}, $this{z}, $this{Z};

>it wont work. I am getting zeroes/blank on the hashes. Why?

Because you're getting back a "reference" to the anonymous hash now, in 
this - s/b
my $this = pop @list;
printf "%d %d %d %d\n", $this->{x}, $this->{y}, $this->{z}, $this->{Z};

> How should I write it "correctly" again?

You need to use the reference (the most Perl-ish method) or un-roll the 
reference on the RHS so it can assign back into the by "casting" it as a 
hash again:
my %this = %{pop @list};
printf "%d %d %d %d\n", $this{x}, $this{y}, $this{z}, $this{Z};

a

--
Andy Bach
Systems Mangler
Internet: andy_b...@wiwb.uscourts.gov
Voice: (608) 261-5738, Cell: (608) 658-1890

"If Java had true garbage collection, most programs would delete 
themselves upon execution."
Robert Sewell.___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Parsing PIX Log

2005-10-07 Thread Andy_Bach
my $entry = 'Sep 26 17:05:11 pixfw.testsec.org %PIX-3-106010: Deny inbound 
protocol 12 src outside:123.97.104.117 dst intf2:192.168.50.10';

chomp($entry);
my ($date_stuff, $min, $sec, $info, $IP1, $IP2) = split(/:/, $entry);
$date_stuff .= ":$min:$sec";

#print" ($date_stuff\n $info\n $IP1\n $IP2\n";
my ($mon, $day, $time, $machine, $dev) = split(/ /, $date_stuff);
my $IP1_name = "unknown";
$IP1_name = $1
  if ($info =~ s/(?:src|dst)\s+\w+$//);
my $IP2_name = "unknown";
$IP2_name = $1
  if ($IP1 =~ s/(src|dst\s+\w+)$//);

printf("On %s %02d %s at machine %s interface %s (ip: %s) saw '%s' from %s 
(ip:%s)\n",
 $mon, $day, $time, $machine, $IP2_name, $IP2,  $info, $IP1_name, 
$IP1);

a

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5932

" History will have to record that the greatest tragedy of this period of
social transition was not the strident clamor of the bad people, but the
appalling silence of the good people. "
Martin Luther King, Jr.
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Variable definition

2005-10-07 Thread Andy_Bach
To add a zero pad ("printf" if you just want it printed zero padded):
my $padded_name = sprintf("%02d", $month);

the 'zero' is the pad char, the '2' is the number of places to pad to

To get rid of it:
print "The month number is: ", $padded_name + 0, "\n";

that  is, use it as an integer and Perl'll dumber it.

$month = 7;
my $padded_name = sprintf("%02d", $month);

print "The month number was: ", $padded_name, "\n";
print "The month number is: ", $padded_name + 0, "\n";

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5932

" History will have to record that the greatest tragedy of this period of
social transition was not the strident clamor of the bad people, but the
appalling silence of the good people. "
Martin Luther King, Jr.
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Regex Newbie Q: Non-Trivial Substitution and Modifying the Matched String

2005-10-10 Thread Andy_Bach
Not sure I'm getting it completely, but using match in a while loop w/ the 
/g modifier lets you process a string one match at a time:
my $string = "Lots of words to be read one at a time.\nthough more than 
one line";
while ( $string =~ /(\w+)/g ) {
   print "Found: $1\n";
   print "Proceessing ...\n";
}   # while /(\w+)/g

While the original string should be left alone (so pos and \G (the marker 
for the last match) don't get confused) you can process/chop up a copy of 
the string as you go. To get really tricky, you can munge the string and 
matches via assignments to pos() - best to look at "Mastering Regular 
Expressions" (O'Reilly/Freidl - or better, buy it!) Chapter 7ish. That's 
not for the fainthearted.

@nums = $data =~ m/\d+/g; # pick apart string, returning a list of numbers

Suppose the list of numbers starts *after* a marker - "" - prime the 
'pos'

$data =~ m//g; # prime the /g start, pos($data) now point to just 
after the ""
@nums = $data =~ m/\d+/g; # pick apart the rest of the string, returning a 
list of numbers

Or:
pos($data) = $i if $i = index($data, ""), $i > 0;   # find the ""
@nums = $data =~ m/\d+/g; # pick apart the rest of the string, returning a 
list of numbers

difference here is pos is just before the "" while in the previous, 
its just after but ...

a

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5932

" History will have to record that the greatest tragedy of this period of
social transition was not the strident clamor of the bad people, but the
appalling silence of the good people. "
Martin Luther King, Jr.
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Counting Matches In A Line

2006-12-05 Thread Andy_Bach
> The match condition is number of ; in 1 line 
Output: 
found 14 ; in 1,232 lines
found 13 ; in 7,456 lines
found 12 ; in 2,321 lines

my %line_counts;
while (<>) {
   my $semis = tr/;/;/;
   if ( $semis ) {
  $line_counts{$semis}++;
   }
   else {
   warn("Bad data - no semis: $_");
   } # if $semis

}# while <>
foreach my $counts ( sort keys %line_counts ) {
   printf("Found %02d ; in %d lines\n", $line_counts{$counts}, $counts);
}

run on itself, we get
# perl /tmp/ln.pl /tmp/ln.pl
Bad data - no semis: while (<>) {
Bad data - no semis:if ( $semis ) {
Bad data - no semis:}
Bad data - no semis:else {
Bad data - no semis:} # if $semis
Bad data - no semis:
Bad data - no semis: }# while <>
Bad data - no semis: foreach my $counts ( sort keys %line_counts ) {
Bad data - no semis: }
Found 03 ; in 1 lines
Found 01 ; in 2 lines
Found 01 ; in 3 lines



a

Andy Bach
Internet: [EMAIL PROTECTED]
VOICE: (608) 261-5738  FAX 264-5932

Have you noticed that people whose parents did not have 
children, also tend not to have children?
  - Robert J. Kolker in sci.math, "Re: Genetics and Math-Ability"
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Counting Matches In A Line

2006-12-06 Thread Andy_Bach
wwang suggested:
>How about using split function?  I mean something like:

my @myCounts = split @YourData, /;/
print $#myCounts;

Er, close - your split is reversed and you want to split the string, not 
an array:
my @myCounts = split /;/, $YourData;

but this points to an interesting thing we just ran into - split, w/o the 
limit field and using an RE to split on, will drop null fields at the end 
of the split so:
my $YourData = 'one;;three;four';
my @myCounts = split /;/, $YourData ;
# '$#array give last index for array
print "cn: ", $#myCounts, "\n";
# add 2 null fields at end
$YourData = 'one;;three;four;;';
@myCounts = split /;/, $YourData ;
print "cn: ", $#myCounts, "\n";
# split using limit param
@myCounts = split /;/, $YourData , 6;
print "cn: ", $#myCounts, "\n";
# array in scalar context gives # of elements
print "cn: ", scalar @myCounts, "\n";

gives:
cn: 3
cn: 3
cn: 5
cn: 6

even thouhg the 2nd one has 5 semis in it.  The last only shows a 'five' 
as

a

Andy Bach
Internet: [EMAIL PROTECTED]
VOICE: (608) 261-5738  FAX 264-5932

Have you noticed that people whose parents did not have 
children, also tend not to have children?
  - Robert J. Kolker in sci.math, "Re: Genetics and Math-Ability"
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: 3 dimensional hash of hashes

2007-02-22 Thread Andy_Bach
Just keep doing it ;->
   $tp3Lookup{$fileTypeKey}{$tpKey})

is your reference to the inner anon hash.  Syntatic sugar pointers make it 
(to me) a little clearer
   $tp3Lookup{$fileTypeKey}->{$tpKey})

but
foreach my $anonKey ( keys %{  $tp3Lookup{$fileTypeKey}->{$tpKey} } )  {

A clarity trick (see "The Randal" article:
http://www.samag.com/documents/s=10108/sam0701h/

or, more complex Cozens on:
http://www.perl.com/pub/a/2006/11/02/all-about-hashes.html 

) is to take a scalar 'ref' to the anon hash along the way:
   my $anon_ref = \%{ $tp3Lookup{$fileTypeKey}->{$tpKey} };

and use that in the now, slightly saner for loop:
   foreach my $anonKey ( keys %{ $anon_ref } ) {

i.e.
print "\nTest iterating over the 3 dimensional hash of hashes...\n";
foreach my $fileTypeKey (keys %tp3Lookup) {
 print "$fileTypeKey => \n";
 foreach my $tpKey (keys %{$tp3Lookup{$fileTypeKey}}) {
   print "\t$tpKey => $tp3Lookup{$fileTypeKey}->{$tpKey}\n";
   print "\tThis is a ",
   ref($tp3Lookup{$fileTypeKey}->{$tpKey})," reference. 
\n";
   my $anon_ref = \%{ $tp3Lookup{$fileTypeKey}->{$tpKey} };
   foreach my $anonKey ( keys %{ $anon_ref } ) {
  print "\t\t$anonKey => $anon_ref->{$anonKey}\n";
   }#  foreach my $anonKey ( keys %{ $anon_ref } )
}   #  foreach my $tpKey (keys 
%{$tp3Lookup{$fileTypeKey}}) 
}   #  foreach my $fileTypeKey (keys %tp3Lookup) 

Andy Bach
Systems Mangler
Internet: [EMAIL PROTECTED]
VOICE: (608) 261-5738  FAX 264-5932

"Procrastination is like putting lots and lots of commas in the sentence 
of your life."
Ze Frank 
http://lifehacker.com/software/procrastination/ze-frank-on-procrastination-235859.php
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: RES: Getting file name from a path

2007-04-13 Thread Andy_Bach
> Often I need to extract the file name from a path.
$current_file = "c:/somedir/test.ps"; 
@split_path = split("/",$current_file); 
$current_file = @split_path[-1]; 
print $current_file; 

You want to use the '$' sigil here, not '@' (you're refering to just one 
element, not the whole array):
$current_file = $split_path[-1];

or
$current_file = pop @split_path;

#!/perl -w
 
$current_file = "c:/somedir/test.ps";
$current_file =~ /.*(\/|\\)(.*)/gi;
 
print $2;
 
Er, you don't want/need the /g (only matches once) or the /i (no letters 
to ignore case on), you don't want to match in the null context so, at 
least:

#!/perl -w
 
my $current_file = "c:/somedir/test.ps";
if ( $current_file =~ /.*(\/|\\)(.*)/ ) {
  print $2;
} 
else {
  warn("whacky file path: $current_file\n");
} 

If you don't test your match, you'll never know if it worked.  There's an 
'interesting' feature of perl this way, if the match fails "$2" retains 
any value it may have had, so you can get a result from a previous match. 
Not a good thing and hard to figure out.

This is relying on the initial "dot star" being greedy enough to eat all 
preceeding slashes and will fail w/
c:myfile.txt

and, on non-DOS
/tmp/this\ filename\ has\ embeded\ spaces

Probably the best way is to let somebody else do it:
   File::Basename(3)
NAME
 fileparse - split a pathname into pieces
 basename - extract just the filename from a path
 dirname - extract just the directory from a path

SYNOPSIS
 use File::Basename;

 ($name,$path,$suffix) = fileparse($fullname,@suffixlist);
 $name = fileparse($fullname,@suffixlist);
 fileparse_set_fstype($os_string);
 $basename = basename($fullname,@suffixlist);
 $dirname = dirname($fullname);

 ($name,$path,$suffix) = 
fileparse("lib/File/Basename.pm",qr{\.pm});
 fileparse_set_fstype("VMS");
 $basename = basename("lib/File/Basename.pm",".pm");
 $dirname = dirname("lib/File/Basename.pm");

a

Andy Bach
Systems Mangler
Internet: [EMAIL PROTECTED]
VOICE: (608) 261-5738  FAX 264-5932

 Punctuality is the virtue of the bored.
--Evelyn Waugh (1903-1966)
___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Newline in CGI script

2007-05-07 Thread Andy_Bach
> The following code is live at http://www.biblescramble.com/Esther/x.cgi.

> I want to put newlines where the tabs are in $insert=" Hazel Work 
states: 
WYrelocate: No";

> I've tried , \n, etc. What do I need?

Not sure what you're after, exactly.  Perl will replace tabs by:
$insert =~ s/\t+/xxx/g;

I"m using "\t+" (one more tab chars) in case you've got tab tab tab and 
don't want 3 new lines - browsers will display new lines w/  so [1]:
$insert =~ s/\t+//g;

if you want to do it in the 'testing' java script function, its a little 
different.
$script2="function tester(){testing=\"";
$script2.=$insert;
$script2.="\"; var head1 = document.getElementById(\"head1\"); 
head1.firstChild.nodeValue=testing; return false;}";

 java script has an RE package, which comes w/ the string object and a 
replace method,
http://www.devguru.com/technologies/ecmascript/QuickRef/string_replace.html

Note, using single quotes (as you've got no perl var being "interpolated) 
will avoid the escapes of all you dbl quotes, so something like:
$script2 = 'function tester(){testing="';
$script2 .= $insert;
$script2 .= '"; 
var test_regex = /\t+/g;
var result = testing.replace(test_regex, "\n");
var head1 = document.getElementById("head1"); 
head1.firstChild.nodeValue = result; 
return false;}';

Untested, YMMV but that's the idea.

a

[1]
as a human readability issue, I always add a \n to "" tags (and  
tags etc, so when you view source, its more legible:
$insert =~ s/\t+/\n/g;


Andy Bach
Systems Mangler
Internet: [EMAIL PROTECTED]
VOICE: (608) 261-5738  FAX 264-5932

"So it goes "
Kurt Vonnegut, Jr. (November 11, 1922 ? April 11, 2007) 
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Perl Report Format Question

2007-05-08 Thread Andy_Bach
> Why don't I Get IP addresses in the IP Address column of the report 
below.  The data is there if I do a print after the split of the value 
in %GAA_Up


foreach $GS_IPAddr (keys(%GAA_Up)) {
   open (UP, ">>$GS_Report") || die "Could not open file 
$GS_Report : $!\n";
  ($GS_Location,$GS_IPAddr,$GS_Time)=split(/\t/,$GAA_Up{$GS_IPAddr 
});
 write(UP);
 close(UP);
 }

I imagine (depending upon you data) you're running into a lexical scoping 
problem.  For one thing its *not* a good idea to use $GS_IPAddr as both 
your loop key and an assigned to var from the split.  Its confusing and 
unnecessary and buys you nothing (but, maybe the cost of one scalar).

However, there's a little magic in how perl handles foreach loop vars as 
far as scoping goes and when you're using it to work w/ a format ... good 
luck. Or here, bad luck.  Adding warnings to your script and the crucial 
part is (line 40 is the UP format line):

Use of uninitialized value in formline at /tmp/form.pl line 40.
Use of uninitialized value in formline at /tmp/form.pl line 40.
Use of uninitialized value in formline at /tmp/form.pl line 40.

Because you don't scope the other split vars, they become global and just 
(luckily) get attached to the format as you wanted them. But $GS_IPAddr is 
scoped as the foreach loop var and so isn't global enough to make the 
grade.  It doesn't exist outside those curlies and so - no IP for you!

Strict/warnings - its not just a good idea, its a Best Practice - which is 
close enough to being a law ;->

a

Andy Bach
Systems Mangler
Internet: [EMAIL PROTECTED]
VOICE: (608) 261-5738  FAX 264-5932

"So it goes "
Kurt Vonnegut, Jr. (November 11, 1922 ? April 11, 2007) 
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: How do I know where my Perl script is running?

2007-05-15 Thread Andy_Bach
2 ways of looking at your quesiton:

FindBin:
NAME
   FindBin - Locate directory of original perl script

SYNOPSIS
use FindBin;
use lib "$FindBin::Bin/../lib";

or

use FindBin qw($Bin);
use lib "$Bin/../lib";

give you where the script itself is.   The module Cwd recommends using 
File::Spec if portability is an issue - as you're asking on win32 ...
NAME
   File::Spec - portably perform operations on file names

SYNOPSIS
   use File::Spec;
...

   curdir
 Returns a string representation of the current directory.

 $curdir = File::Spec->curdir();

a

Andy Bach
Systems Mangler
Internet: [EMAIL PROTECTED]
VOICE: (608) 261-5738  FAX 264-5932

The competent programmer is fully aware of the strictly limited size of
his own skull; therefore he approaches the programming task in full
humility, and among other things he avoids clever tricks like the
plague. -- Edsger Dijkstra
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: What is not initialised here?

2007-05-29 Thread Andy_Bach
$_ = shift @data until /Amount/;   # start of transactions

> and get this warning:

Use of uninitialized value in pattern match (m//) at CMS2AP.pl line 138.

One way to avoid the warning is to check for that first uninit'ed $_
#!perl -w
use strict;
my @data = qw(hi ho hee ha Amount hoo hooh);
print "t: ", $_ = shift @data, "\n" until  ($_ and /Amount/);   # 
start of transactions
# $_ = shift @data until  ($_ and /Amount/);   # start of transactions

But if you've got a bunch of noise in there before 'Amount' you're wasting 
a lot of assigns, I think.  Similarly, you'll run for quite a ways if 
there *isn't* an "Amount" in there ;->

I tried to use the '...' flip-flop:
[the flip-flop '..' operator] can test the right operand and
   become false on the same evaluation it became true (as in awk), but 
it still returns true once.  If you
   don't want it to test the right operand till the next evaluation, 
as in sed, just use three dots ("...")
   instead of two.  In all other regards, "..." behaves just like ".." 
does.

but couldn't get it to help here. I'd think you'd want to do this on the 
front end, though.  Don't put it into @data in the first place
while (<>) {
 next if 1 .. /Amount/;
 chomp;
 push @data, $_;
...

or something.

a

Andy Bach
Systems Mangler
Internet: [EMAIL PROTECTED]
VOICE: (608) 261-5738  FAX 264-5932

The competent programmer is fully aware of the strictly limited size of
his own skull; therefore he approaches the programming task in full
humility, and among other things he avoids clever tricks like the
plague. -- Edsger Dijkstra
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Questions on porting Perl from Unix to Windows

2008-01-24 Thread Andy_Bach
OS is found via the $^O ("carrot capital O") special var:
$ perl -e 'print $^O'
linux

I think winx stuff starts w/ "MS"
C:\>perl -e "print $^O"
MSWin32

but you don't really want to do that. File::Copy
$ perldoc File::Copy
use File::Copy;

   copy("file1","file2") or die "Copy failed: $!";
   copy("Copy.pm",\*STDOUT);
   move("/dev1/fileA","/dev2/fileB");

does it OS-independently.  Using the perl native opendir/readdir commands
perldoc -f readdir
...
opendir(DIR, $some_dir) ââ die "can't opendir $some_dir: $!";
   @dots = grep { /^\./ && -f "$some_dir/$_" } 
readdir(DIR);
   closedir DIR;

again does this w/o needing to know which OS you're on. You may also want 
to look at:
NAME
   File::Spec - portably perform operations on file names

as for the 2nd question/shebang lines - yeah MS doesn't use the shebang, 
it uses file extension. However perl does use the shebang, even on winx so 
you can just have a shebang of
#!perl -w

and get the warnings etc enabled.  We used to just make a matching batch 
file  that called perl on the script.

a

Andy Bach
Systems Mangler
Internet: [EMAIL PROTECTED]
VOICE: (608) 261-5738  FAX 264-5932

"The Microsoft Exchange Information Store service depends on
the Microsoft Exchange Directory service which failed to
start because of the following error:
The operation completed successfully."
(actual MSE error msg)

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Questions on porting Perl from Unix to Windows

2008-01-24 Thread Andy_Bach
It's a windows cmd.exe thing.  Only dbl quotes work for "-e" script 
creation and there are a number of other issues.  In general, one-liners 
in cmd.exe aren't going to be very useful.  You can get a different shell 
(even bash) for winx but 

a

Andy Bach
Systems Mangler
Internet: [EMAIL PROTECTED]
VOICE: (608) 261-5738  FAX 264-5932

"The Microsoft Exchange Information Store service depends on
the Microsoft Exchange Directory service which failed to
start because of the following error:
The operation completed successfully."
(actual MSE error msg)

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: use FileHandle;

2008-04-17 Thread Andy_Bach
> So, joyfully, I wrote the following:

use FileHandle;
 
foreach my $key (0 .. 34) {
 
 $fh = new FileHandle "> VTOL$key";
 $flist{$key} = $fh;
 print $flist{$key} "Header info\n";
 
}

> But it will not compile, it errors with:

C:\>VTOLparser.pl
String found where operator expected at C:\VTOLparser.pl line 9, near "
} "Header info\n""
(Missing operator before  "Header info\n"?)

I saw this somewhere else, there's something about the list context of 
print that keeps it from treating the stored filehandle properly. I recall 
from Perl Best Practices the suggestion to use curlies to make clear that 
the first entry is a filehandle and not something else.:
use FileHandle;

my %flist; 
foreach my $key (0 .. 34) {
 
 my $fh = new FileHandle "> VTOL$key";
 $flist{$key} = $fh;
 print { $flist{$key} } "Header info\n";
 
}

anyway that works.

a
---
Andy Bach
Systems Mangler
Internet: [EMAIL PROTECTED]
Voice: (608) 261-5738 Fax: 264-5932

"When angry, count to four; when very angry, swear."
Mark Twain

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: trouble accessing fully qualified $main::variable contents

2008-07-07 Thread Andy_Bach
> why I am unable to print the value of the main
package's variable (despite that I am using the fully qualified variable
name)?

Name "main::var" used only once: possible typo at C:\perlsrc\TEST.PL line
9.

Use of uninitialized value in concatenation (.) or string at
c:\perlsrc\TEST.PL line 9.


Hmm, this:
use strict;
use warnings;

our $var = "value";

works as expected. 'my' is the lexicalizer, so it appears it *doesn't* put
the var into the main package. Something like lexical vars *can't* be
package scope/FQVNamed, only 'our' vars can. Try 'perldoc our':
   An "our" declares the listed variables to be valid globals
within the enclosing block, file, or "eval".  That is, it has the same
scoping rules
as a "my" declaration, but does not create a local variable.  If more than
one value is listed, the list must be placed in parentheses.  The "our"
declaration has no semantic effect unless "use strict vars" is in effect,
in which case it lets you use the declared global variable without
qualifying it with a package name.  (
But only within the lexical scope of the "our" declaration.  In this it
differs from "use vars", which is package scoped.)

   An "our" declaration declares a global variable that will be
visible across its entire lexical scope, even across package boundaries.
The package in which the variable is entered is determined at the point of
the declaration, not at the point of use.  This means the following
behavior holds:

a

---
Andy Bach
Systems Mangler
Internet: [EMAIL PROTECTED]
Voice: (608) 261-5738 Fax: 264-5932

The name "grep" is from the common "ed" (pre-visual "vi") command:
 g/re/p i.e. g(lobal-search)/r(egular) e(xpression)/p(rint)

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: New to Perl and stuck

2009-03-03 Thread Andy_Bach
> My goal is to automate this so that after specifying a folder, Perl will 
pull the specified lines from each of the 25 txt files in that folder and 
write them to a single output file.


If you don't need file names etc, you can just expand your pointies to use 
a glob
@output = <*.txt>

but that  put it all in there, so:
print ("\nESTIMATION\n");
print @output[18,21..24,28..31,206..208];
print "\nPOPULATION\n";
print @output[220,223..226,229..233,424..426];

would need to be done in ... increments of 427 or whatever the total 
lines/file is.  That seem fraught w/ peril but you know your data best. 
I'd never trust line #s to be accurate, nor would I believe in fixed 
length data files, but only as I've been well burned by both assumptions. 
YMMV

You can use command line globbing (if you've got it):
myapp /this/data/*.txt

and then each file is opened one after the other. perldoc -f eof will help 
if want to handle them one after the other. Otherwise, opendir and readdir 
are the route - perldoc -f readdir gives the better sample code.

a
---
Andy Bach
Systems Mangler
Internet: andy_b...@wiwb.uscourts.gov
Voice: (608) 261-5738; Cell: (608) 658-1890

Entropy just ain't what it used to be___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Issue with Mail::Sender : "what is the << function called?"

2009-03-05 Thread Andy_Bach
> ... but what exactly is the "<<" function called?

It's called a "here doc" - it's a unix/shell convention, allowing 
multiline input:
$ mail root < hi
> 
> mom
> 
> .
> EOM

(old 'mail' ends w/ a '.' on line by itself)

perldoc -q HERE.Doc

Found in /usr/lib/perl5/5.8.0/pod/perlfaq4.pod
   Why don?t my <___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: implied variables (what are they actually?)

2009-03-10 Thread Andy_Bach
while (<>) {#line1
print; #line2
}

> But so that I could add some better control of this program, can anyone 
please tell me what the ACTUAL VARIABLE name is that is being internally 
used in ?<>? (line 1)?
> And what ACTUAL VARIABLE name is being internally used in ?print;? (line 
2).

One and the same - the 'heart' of Perl's special vars: $_ 

while (<>) {

is really
while ( $_ = <> ) {

and (try perldoc -f print) print's default var is $_;

> I would like to modify the above program so that it will run with 
variable names that can be seen, as a learning exercise?

while (my $line = <> )   {

a

---
Andy Bach
Systems Mangler
Internet: andy_b...@wiwb.uscourts.gov
Voice: (608) 261-5738; Cell: (608) 658-1890

Entropy just ain't what it used to be___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: :Socket (client program sends simple text file containing CRLFnewlines)

2009-03-13 Thread Andy_Bach
I did a little googling and found Net::Telnet (built on top of IO::Socket) 
saying:
In the input stream, each sequence of carriage return and line feed (i.e. 
"\015\012" or CR LF) is converted to "\n". In the output stream, each 
occurrence of "\n" is converted to a sequence of CR LF. See binmode() to 
change the behavior. TCP protocols typically use the ASCII sequence, 
carriage return and line feed to designate a newline. 

Some sort of network data standard (one of many ;-) or something like ftp 
in ascii mode?  Perl does automagically make the mystic "\n" all things 
(eol-ish) to all OS (\r\n for DOS, \n for *nix* \r for Mac) but possibly a 
binmode might help? Seeing some of the code or (I guess this is the Win32 
list and you do say IIS) more specific details on OS etc might help.

a
---
Andy Bach
Systems Mangler
Internet: andy_b...@wiwb.uscourts.gov
Voice: (608) 261-5738; Cell: (608) 658-1890

Entropy just ain't what it used to be___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Help with array

2011-09-08 Thread Andy_Bach
> It might also be worth taking a look at slices, described in 'perldoc 
perldata'.

It also *may* be worth rethinking your data model. Often (not always but 
...) needing to insert something into a particular location in an array 
implies a hash might be a better structure.

Just a thought.

a

--
Andy Bach
Systems Mangler
Internet: andy_b...@wiwb.uscourts.gov
Voice: (608) 261-5738, Cell: (608) 658-1890

?One of the most striking differences between a cat and a lie is that a 
cat has only nine lives.? 
Mark Twain, Vice President, American Anti-Imperialist League, and 
erstwhile writer___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Initialize variables in perl

2011-10-17 Thread Andy_Bach
> is there a difference between
my @variable;

Creates a new, lexically scoped array.  All the elements are undef.

my @variable=();

The same, though it assigns an empty list to the array, sort of an explicit
code comment "This array is empty".  Note, w/o the "my" this would empty an
existing array.

undef @variable;

undef-ines (empties) an existing array.  If @variable didn't exist in the
current scope, I'm betting this will give you a new, globally scoped array.
That is:
my @variable;
undef @variable;

is the same as the previous two.

a

--
Andy Bach
Systems Mangler
Internet: andy_b...@wiwb.uscourts.gov
Voice: (608) 261-5738, Cell: (608) 658-1890

"The reward you get for digging holes is a bigger shovel"
Granny Weatherwax

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: How to split up a string with carriage returns into an array

2011-11-04 Thread Andy_Bach
> I  tried 
 
@ans = split (/\r/s, $msg);
@ans = split (/\r/g, $msg);
@ans = split (/\r\n/s, $msg);
@ans = split (/\r\n/g, $msg);

2 things - Perl should handle the \r\n part for you  - "\n" is normally a 
match for whatever your OS's end of line marker is.  You also don't need 
the modifiers on the match - split is like  a \g already, and \s only 
affects the '.' metachar (allowing it to match a newline).  So
@ans = split( /\n/, $msg );

*should* work, usually this kind of problem is due to the newlines not 
really being there.  But, as an idiom, this works to split up a multi-line 
string into an array, one line per element. Note also there *won't* be a 
new line on the end of each element anymore:

my $msg = 
'this is line one
this is line 2
this is line III'
my @ans = split(/\n/, $msg);

foreach my $line ( @ans ) {
   print "line: $line\n";
}

$ perl /tmp/test.pl
line: this is line one
line: this is line 2
line: this is line III
a 
--
Andy Bach
Systems Mangler
Internet: andy_b...@wiwb.uscourts.gov
Voice: (608) 261-5738, Cell: (608) 658-1890

We are what we pretend to be, so we must be careful about what we 
pretend to be.
Kurt Vonnegut___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: How to split up a string with carriage returns into an array

2011-11-04 Thread Andy_Bach
Parsing English is hard ;-> but if you're fairly confident of the 
formatting, you can try and add in a marker and then split on that:
   $msg =~ s/((?:JAN|FEB|...|OCT|NOV|DEC)\s\d+\s\-)/|#|$1/g;
   @ans = grep { /\w/ } split('\|#\|', $msg);


The elipsis there is the rest of the months (note, they *must* be upper 
case though you can mark that adding the "i" modifier to the subst (and, 
in the 'non-capturing parens' by adding between the ? and the : "(?i:JAN 
...") and the grep is to ditch in the empty first match.

a

my $msg =-
'OCT 1 - this is line one
NOV 1 - this is line 2
NOV 2 - this is line III';
my @ans = split(/\n/, $msg);

foreach my $line ( @ans ) {
   print "line: $line\n";
   }

   $msg =~ s/\n/ /g;
   print "Msg: $msg\n";
   $msg =~ s/((?:JAN|FEB|OCT|NOV|DEC)\s\d+\s\-)/|#|$1/g;
   print "Msg: $msg\n";
   @ans = grep { /\w/ } split('\|#\|', $msg);

foreach my $line ( @ans ) {
   print "line2: $line\n";
   }

--
Andy Bach
Systems Mangler
Internet: andy_b...@wiwb.uscourts.gov
Voice: (608) 261-5738, Cell: (608) 658-1890

We are what we pretend to be, so we must be careful about what we 
pretend to be.
Kurt Vonnegut___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: [OT:] RE: PDA language!

2003-06-23 Thread Andy_Bach
My Sharp Zaurus 5600 runs perl.

a

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5030

"We are either doing something, or we are not. 'Talking about' is a 
subset of 'not'."-- Mike Sphar in alt.sysadmin.recovery
___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: echo %CD%

2003-09-25 Thread Andy_Bach
print `cd`;

(those are back tics, not quotes) works.  "cd" by itself, in winx returns 
the current dir.

a

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5030

"We are either doing something, or we are not. 'Talking about' is a 
subset of 'not'."-- Mike Sphar in alt.sysadmin.recovery
___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: What is this used for....

2003-11-07 Thread Andy_Bach
Its rude/bad/silly to post msgs to two lists at once, esp. as this is _javascript_ not perl but it appears to be a wanker's code to get folks to a web site in the Netherlands.  All that:function rd() {   var a = l3[4]+l1[8]+l2[4]+l1[3]+l2[5]+l3[4];   var b = l2[2]+l2[5]+l1[2]+l1[0]+l3[1]+l1[8]+l2[5]+l2[4];  stuff is to spell out "window.load" and it appears somebody misspelled "advice". The rest it to test which browser is in use.aAndy Bach, Sys. ManglerInternet: [EMAIL PROTECTED]VOICE: (608) 261-5738  FAX 264-5030"And do my eyes deceive me, or has Judsys at long last been mentioned by name in an official Kremlin pronouncement?"   unnamed humorist off list (no offense)___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Sorting numbers?

2003-12-02 Thread Andy_Bach
one way:
sort { $a +0  <=> $b + 0 } @msgs

forces them to be treated as numbers, not strings.   Hmm:
@msglist = sort { $a <=> $b } @msgs;

$found = $msglist[0];
for ($i = 0; $i < scalar(@msglist); ++$i) {
print "Checking message $msgs[$i]\n";
last if ($msglist[$i] == $message);
}
$found = $msglist[$i-1] if ($i);

print "Previous: $found\n";

Try:
@msglist = sort { $a <=> $b } @msgs;

$found = $msglist[0];
for my $msgid (@msglist) {
print "Checking message $msgid\n";
last if ($msgid == $message);
$found = $msgid; 
}

print "Previous: $found\n";

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5030

We are REDACTED!!!
___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Seemingly simple???

2003-12-10 Thread Andy_Bach
Perl uses the line ending appropriate for the platform \r\n for dos \n for 
*nix and translates the
print "hey\n"

appropriately. You can do:
$var =~ s/\r//;

to remove the ^M but you probably want to look into setting the var $/ and 
use chomp. 
perldoc -f chomp
   chomp   This safer version of "chop" removes any trailing
 string that corresponds to the current value of $/
 (also known as $INPUT_RECORD_SEPARATOR in the
 "English" module).  It returns the total number of
 characters removed from all its arguments.  It's
 often used to remove the newline from the end of an
 input record when you're worried that the final
 record may be missing its newline.  When in
 paragraph mode ("$/ = """), it removes all trailing
 newlines from the string.  When in slurp mode ("$/ =
 undef") or fixed-length record mode ($/ is a
 reference to an integer or the like, see perlvar)
 chomp() won't remove anything. 

a

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5030

Documentation is the castor oil of programming.  Managers know it must be 
good because the programmers hate it so much.
___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: How to strip out EOF characters

2004-02-03 Thread Andy_Bach
Isn't it a little silly to go on to try and read from whichever file 
handle after the open has failed?  You've got two flags set (errorcheck2 
and error_flag), so at least use  one to skip around the "while <>".

It looks like you're expecting to have a number of possible failures - 
make sure to include useful data like file name that failed and why:
if ($errorcheck2 eq "TRUE")
   {
  print mainlog "ERROR \- data table $table$data_ext missing for 
validation ($fileDir open failed: $!)\n";
  $error_flag = "TRUE";
 }

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5030

"I'm just doing my job, nothing personal, sorry."
___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Regex matching of patterns in variables

2004-03-03 Thread Andy_Bach
Works fine here - 'course the '.' in Wile E. is going to match any char 
so:
Wile E. Coyote
 matches
Wile Ej Coyote
 matches
Wile E. Coyotexxx
 matches

using quotemeta or \Q:
#my $var = "coyote";
my $var = quotemeta "Wile E. Coyote";
while () {
  if ( /$var/oi) {
print $_ . " matches\n";
}   # if $var
}   # while <>
__END__
moose
coyote
Wile E. Coyote
Wile Ej Coyote
Wile E. Coyotexxx

keeps the meta chars from being meta chars in your $var.  the /o on the 
match will make things faster, as long as $var doesn't change and note:
 if ( $_ =~ /$var/oi) {
is the same as:
 if ( /$var/oi) {

though the latter is more "perlish."

a

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5030

"[the heck w/ it] lets go bowling"
___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: searching a file for keywords

2004-04-15 Thread Andy_Bach
You might do better to make an altenation:
$keywords = join("|", @keywords);
if ( $wholefile =~ /^\s*($keywords)\s*$/mo ) {   # do you want 'i' ?

rather than the loop.  Worth a benchmark, but one search for multiple 
matches is probably faster than multiple searches for some sized files. 

You also might do better to process the file and strip out the keyword 
chunks, put them in a hash.
my %keywords;
while ( my ($keyword) = $wholefile =~ /^\s*([A-Z]+)\s*/gm ) {
$keywords{$keyword}++;
}

...
 if ( $keywords{uc($lookup_keyword)} ) {
 # found one

depends, again on how often you're needing to search.  If, say you've got 
50 files and 100 searches, it may save time to read them each once, get 
the keywords and then make 100 hash lookups (you could append the filename 
to the $keywords{$keyword} instead of just a counter) as opposed to 50 
full reads times a 100.  If the files are static, you'll win big by 
putting the index info to a file (you can use Storeable modules to save 
the hash even) and just opening that the next time.

Or, look at htdig (www.htdig.org).

a

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5932

Rule #8 Don't close the latch on an empty DLT drive
___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: getting file's date/time in Net::FTP

2004-04-20 Thread Andy_Bach
perldoc Net::FTP:

 dir ( [ DIR ] )
 Get a directory listing of "DIR", or the current
 directory in long format.

 In an array context, returns a list of lines returned
 from the server. In a scalar context, returns a
 reference to a list.

so, for Solaris anyway:
# get file info
my @file_dir = $ftp->dir($file)
or die "dir failed ", $ftp->message;
# looks like:
#  -rw-r--r--   1 103581  41392 Dec 26  2001 
01-C-684-C-12-18-01.pdf
# or (note time, not year:
#  -rw-r--r--   1 103581  41392 Apr 16  12:03 
01-C-684-C-12-18-01.pdf

foreach my $file ( @file_dir ) {
# split on whitespace for file_name
  my @file_parts = split(/\s+/, $file);
  my $file_name = pop(@file_parts);
  print "file: $file_name\n";
# er, other parts (id, gid, links) are guesses
  my ($perm, $id, $gid, $links, $size, @date) = @file_parts;
# fudge to handle both year and time
# use Date::Manip or something
  print "Date: ", join(" ", @date), "\n";
}

a

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5932

Why do programmers confuse Halloween and Christmas?
Because OCT 31 = DEC 25.
___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: localtime no work

2004-05-26 Thread Andy_Bach
Because your -5 hours off of epoch time - i.e.:my ($secs, $mins, $hours, $mday,  $mon, $year, undef, undef, undef) =localtime($endTime - $startTime);print "Elapsed time was $mon/$mday/$year - $hours:$mins:$secs\n"; gives:Elapsed time was 11/31/69 - 18:0:10That is plus 10 seconds minus (in my case) 6 hours from the epoch (1/1/70) gives us a time of Dec 31, 1969 18:00:10 (month as '11' is uncorrected for the zero based month numbers of localtime() - see perldoc -f localtime) Try:my $diff_time = $endTime - $startTime;printf("Elapsed time was %.02f seconds\n", $diff_time);printf("Elapsed time was %.02f minutes\n", $diff_time/60);printf("Elapsed time was %.04f hours\n", $diff_time/(60*60));printf("Elapsed time was %.04f days\n", $diff_time/(24*60*60)); aAndy Bach, Sys. ManglerInternet: [EMAIL PROTECTED]VOICE: (608) 261-5738  FAX 264-5932Why do programmers confuse Halloween and Christmas?Because OCT 31 = DEC 25.___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: named hash inside of hash

2004-06-08 Thread Andy_Bach
> I don't understand this line:

 $ips{$source_ip}->{$dest_ip}++;

> how can you access and increment something that doesnt exist?

Fancy name: autovivification - perl, w/ your best interests at heart 
(generally) knows what you want to do, so it automagically creates the 
source_ip hash entry (if it doesn't exist), creates the anon-hash (ditto), 
adds a dest_ip key (ibid) and sees the incr. op, so set a value to zero 
and increments it - bingo
 $ips{$source_ip}->{$dest_ip}++;
 if ( $ips{$source_ip}->{$dest_ip} == 1 ) {
 print "First time $source_ip has sent $dest_ip packets!\n";
 }

On the other hand, autoV works for increment, but you should see a warning 
(you are using -w/strict right?) if you try::
 if ( $ips{$source_ip}->{$dest_ip} == 0 ) {
 print "First time $source_ip has sent $dest_ip packets!\n";
 }
 $ips{$source_ip}->{$dest_ip}++;

Here, if it *is* the first time, as dest_ip doesn't exist, you'll see:
Use of uninitialized value in numeric eq (==) at 

In this case:
 unless ( $ips{$source_ip}->{$dest_ip} ) {
 print "First time $source_ip has sent $dest_ip packets!\n";
 }
 $ips{$source_ip}->{$dest_ip}++;

gets around that as there's no compare - basically you're checking for 
truth, existence or non-zero.

Make any sense?

a

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5932

Hardware, n.:
The parts of a computer system that can be kicked.

___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: simple regex question

2004-10-15 Thread Andy_Bach
A further debug/syntax nitpick:
print "foo='$foo'\n";
  $foo =~ /([^.]+).txt/;
  $ans = $1;
  print "ans='$ans'\n";

best as:
my ($foo, $ans);
print "foo='$foo'\n";
  if ( $foo =~ /([^.]+)\.txt/ ) {
 $ans = $1;
  } else {
 $ans="match failed on: $foo";
  } 
 print "ans='$ans'\n";

if you check your "=~" as they happen, you'll know $1 will be what you 
hope for.  A failing match does *not* reset $1, so it can have a value 
after a failed match, the value it got in a previous, successful match.

Note also the backslash before the dot in ".txt" otherwise:
nodot-txt

will match, unless that's what you wanted. 

a

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5932

Contrariwise," continued Tweedledee, "if it was so, it might be, and
if it were so, it would be; but as it isn't, it ain't.  That's logic!"
  -- Lewis Carroll, "Through the Looking Glass"
___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Mason question

2004-10-06 Thread Andy_Bach
One guess:
"My intiial thought is that i need to see more info.  My guess is that the 
mason
interpreter isn't even getting those requests.  I would try setting it up 
to
use an HTML::Mason::CGIHandler object.  There should be an example at
http://www.masonhq.com/

a

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5932

Contrariwise," continued Tweedledee, "if it was so, it might be, and
if it were so, it would be; but as it isn't, it ain't.  That's logic!"
  -- Lewis Carroll, "Through the Looking Glass"
___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: regex help

2004-11-03 Thread Andy_Bach
$Bill wrote:
> ... and \1 is deprecated for $1.

I believe that should read [thanks Greg!] "...and \1 is deprecated on the 
right
side of s///".  On the left side (as in any regexp), \1 and $1 differ. But 
as a holdover from sed, \1 means the same as
$1 on the right side of the subst.

\1 isn't the same as $1, in that
s/(.)\1//;
 
deletes duplicate chars, while;
s/(.)$1//;
 
depends upon the value of $1 - which, in this case'd be set *before* the 
subst got invoked.  On the right hand side, $1 is what we expect (the 
final matched text inside the first set of parens).

\1 (on the LHS) also can/will change during the course of a regex 
evaluation - as the 
parser/matcher works, \1 will take on different values during backtracking 

etc - though, in the end \1 is the same as $1. I've never understood it 
but I belive perl golfer/obfu folks can do strange and terrible things w/ 
the difference.

a

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5932

"If there be time to expose through discussion the falsehood and 
fallacies, to avert the evil
by the process of education, the remedy to be applied is more speech, not 
enforced silence." 
   Justice Louis Brandeis

___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: black box

2004-12-16 Thread Andy_Bach
Just a small point - the shebang on winx does help perl - it will use any 
command line options (e.g
#!perl -Tw

turns on taint and warnings) it finds in the shebang, regardless of the 
path.

a

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5932

Call out Gouranga be happy!!!
Gouranga Gouranga Gouranga 
That which brings the highest happiness!!
___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: count of matches

2004-12-23 Thread Andy_Bach
Try:
#!/usr/bin/perl -w
use strict;
 $_ = "ABabcde12345BAABabcde1234blahblah5BA";
 print "is: ", $_ , "\n";
my $count = () = $_=~ /(AB.*?BA)/g;
 my @match = /(AB.*?BA)/g;
print "match: ", join(", ", @match), "\n";
 print "I matched $count times\n";

The trick is: 
$_ =~ /../
applies the pattern to the string and returns a list of the 2 matches (due 
to the parens inside the RE).  The same thing as:

The empty parens supply the "list context" to the match, and then the 
assignment to $count puts that list into scalar context, akin to:
 my @match = $_ =~ /(AB.*?BA)/g;
my $count = @match;

or:
print "I matched ", scalar @match, " times\n";

Okay, I'm a little shakey on the fact that, the empty parens seem to 
accept the list of return values (anonymous list, akin to an anon. array 
[] ?) and then act like an array (as:
my $count = ('ABabcde12345BA', 'ABabcde1234blahblah5BA');
differs from:
my $count = @match;

in that the former assigns the final element of the list to $count, the 
later assigns the # of elements) but that's the case and thats how the 
matchbox gets opened and counted. 

a

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5932

Call out Gouranga be happy!!!
Gouranga Gouranga Gouranga 
That which brings the highest happiness!!
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Script printing its own source code

2005-05-31 Thread Andy_Bach
> ... called quines ...
And, in perl, one of the most amazing quine programs is Damian Conway's 
SelfGol. It's also the game of Life and a quine-izer for other programs - 
don't even think about looking at it.

http://libarynth.f0.am/cgi-bin/twiki/view/Libarynth/SelfGOL

I had the good fortune to sit in a nice enough bar, drinking beer (not the 
best idea considering) while he char by char worked his way through the 
script (it was an obfusticated perl submission). He includes evil things 
like stray parens/curlies so vi's '%' won't help you de-parse it and, even 
at a merer 1000 chars, managed to slip in useless code just to further 
befuddle.   It was beyond humbling, made you want to give up even typing 
on a computer and go back to banging rocks and sticks together.

a

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5932

self-reference, n. - See self-reference 
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs