complex split (when delimiter appears in one of the fields)

2009-06-12 Thread Michael Alipio

Hi,

I have a string that looks like this:

my $string = 1, 3, 0. 0. 0. 0, 22, Zak',adfk $! mac., ;


Basically, there are seven fields. after the bird, everything up to the last 
comma is the 5th field. 6th field is blank.

Now my problem is splitting it and extracting the 5th field.

If I will do a (split/,/,$string)[-2]) then i will only get adfk $! mac.
I need to get the entire Zak' , adfk $! mac.




  

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: complex split (when delimiter appears in one of the fields)

2009-06-12 Thread Raymond Wan


Hi Michael,


Michael Alipio wrote:

I have a string that looks like this:

my $string = 1, 3, 0. 0. 0. 0, 22, Zak',adfk $! mac., ;


Basically, there are seven fields. after the bird, everything up to the last 
comma is the 5th field. 6th field is blank.

Now my problem is splitting it and extracting the 5th field.

If I will do a (split/,/,$string)[-2]) then i will only get adfk $! mac.
I need to get the entire Zak' , adfk $! mac.



I doubt this is the best way, but I would use both split and a regular 
expression.  The first split, I would specify there are only five fields 
so that the 5th, 6th, and 7th fields ends up in the remainder of split 
(split with three arguments).  Then I would use a regular expression on 
these three fields and pull out the last two.


I suppose a human looking at the line would do something similar (remove 
the fields known not to have commas first and what's left is the 5th). 
If two fields can have commas...then I'm not sure what to do.  :-)


Ray



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




AW: complex split (when delimiter appears in one of the fields)

2009-06-12 Thread Thomas Bätzler
Michael Alipio daem0n...@yahoo.com asked:
 I have a string that looks like this:
 
 my $string = 1, 3, 0. 0. 0. 0, 22, Zak',adfk $! mac., ;
 
 
 Basically, there are seven fields. after the bird, everything up to the
 last comma is the 5th field. 6th field is blank.

 Now my problem is splitting it and extracting the 5th field.
 
 If I will do a (split/,/,$string)[-2]) then i will only get adfk $!
 mac.
 I need to get the entire Zak' , adfk $! mac.

Obviously you can't expect split to guess which comma is part of a field and 
which is a record separator.

Without more information I'd say that you've chosen the wrong separator to 
split on - instead just a comma, it should be a comma and whitespace. That in 
turn means that there are only 

#!/usr/bin/perl -w

use strict;

my $string = q(1, 3, 0. 0. 0. 0, 22, Zak',adfk $! mac., );

my $i = 0;
foreach ( split /,\s+/, $string ){
  print $i: $_\n;
  $i++;
}

__END__

HTH,
Thomas

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: complex split (when delimiter appears in one of the fields)

2009-06-12 Thread John W. Krahn

Michael Alipio wrote:

Hi,


Hello,


I have a string that looks like this:

my $string = 1, 3, 0. 0. 0. 0, 22, Zak',adfk $! mac., ;


Basically, there are seven fields. after the bird, everything up to the
last comma is the 5th field. 6th field is blank.

Now my problem is splitting it and extracting the 5th field.

If I will do a (split/,/,$string)[-2]) then i will only get adfk $! mac.
I need to get the entire Zak' , adfk $! mac.


( split /(?!'),/, $string )[ -2 ]



John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




pass int* to perl

2009-06-12 Thread Patrick Dupre

Hello,

I need to call perl from c.
To pass an integer, I use to make a newSViv.
How do I do to pass a int* ?
Should I just cast the int* to int ans passed as an SV ?
in perl I need to get the value by $$var.

Thank.

--
---
==
 Patrick DUPRÉ  |   |
 Department of Chemistry|   |Phone: (44)-(0)-1904-434384
 The University of York |   |Fax:   (44)-(0)-1904-432516
 Heslington |   |
 York YO10 5DD  United Kingdom  |   |email: pd...@york.ac.uk
==
-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


How to read RegEx match in to a variable?

2009-06-12 Thread Sam Munzani

Team,

I am a totally newbee to perl scripting. I have learned enough to 
understand somebody's simple scripts and written some basic ones. Below 
is what I am trying to achieve.


I am writing a wrapper script to trigger when a syslog message arrives 
to syslog-ng. It fires up my parser.pl script by passing whole message 
as an argument. The message looks like this.


Jun  2 22:34:30 172.24.100.1 Jun 02 2009 22:34:33 home-pix : 
%PIX-6-302013: Built inbound TCP connection 3933236 for 
outside:172.23.1.1/50229 (172.23.1.1/50229) to 
inside:172.24.100.25/1 (172.24.100.25/1) (smunzani)


What I am trying to do is use RegEx to extract information from this. I 
want to capture IP address to a variable, hostname to another variable 
and anything after PIX-6-302013: as message variable. I was able to hack 
through creating the RegEx to match my requirements. However I don't 
know how to pass RegEx matches strings to value of another variable.


Any hints, examples?

Thanks,
Sam

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: How to read RegEx match in to a variable?

2009-06-12 Thread John W. Krahn

Sam Munzani wrote:

Team,

I am a totally newbee to perl scripting. I have learned enough to 
understand somebody's simple scripts and written some basic ones. Below 
is what I am trying to achieve.


I am writing a wrapper script to trigger when a syslog message arrives 
to syslog-ng. It fires up my parser.pl script by passing whole message 
as an argument. The message looks like this.


Jun  2 22:34:30 172.24.100.1 Jun 02 2009 22:34:33 home-pix : 
%PIX-6-302013: Built inbound TCP connection 3933236 for 
outside:172.23.1.1/50229 (172.23.1.1/50229) to 
inside:172.24.100.25/1 (172.24.100.25/1) (smunzani)


What I am trying to do is use RegEx to extract information from this. I 
want to capture IP address to a variable, hostname to another variable 
and anything after PIX-6-302013: as message variable. I was able to hack 
through creating the RegEx to match my requirements. However I don't 
know how to pass RegEx matches strings to value of another variable.


A regular expression *is* a string, it operates on strings and it 
returns strings.  For example:


( $string1 ) = $string2 =~ /$string3/;

What do you mean by RegEx matches strings?



John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




YAPC::NA

2009-06-12 Thread Uri Guttman

hi all,

it is a late announcement here but you should all know that YAPC::NA is
in pittsburgh at CMU. it runs from june 22-25, 2009. it is the only pure
perl conference in NA and it is a blast and a great place to meet other
perl hackers. there will be talks for all skill levels and 2 extra days
of low cost training available. the conference fee is very low and
expenses are low if you stay in the dorms. find out more and register at
yapc10.org.

uri


-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
- Free Perl Training --- http://perlhunter.com/college.html -
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




YAPC::NA

2009-06-12 Thread Uri Guttman

hi all,

it is a late announcement here but you should all know that YAPC::NA is
in pittsburgh at CMU. it runs from june 22-25, 2009. it is the only pure
perl conference in NA and it is a blast and a great place to meet other
perl hackers. there will be talks for all skill levels and 2 extra days
of low cost training available. the conference fee is very low and
expenses are low if you stay in the dorms. find out more and register at
yapc10.org.

uri


-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
- Free Perl Training --- http://perlhunter.com/college.html -
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Please, I need help!!!

2009-06-12 Thread Phillip

Hallo @ all,

i am new in this domain(perlscript) and i have a question.i have a 
array,i sort it,i get the last element of the array but i want to get 
the next element after this one.how can i do this?


for example:

$arr1=(6,3,8,1) ---my last element is 1 and mark that is the last 
element

now i sort them  $arr1=(1,3,6,8)
my last element is 1 and now i want to go to the next element in the 
list,how can i do this?
if i do something like this:$next=$last+1; ---in this case $next will 
be 2 and this is not my third element in my list,i want the 3.

BR,
Phil

here is my code:

@arr[1]=(3,7,13,1,19,5,9);
@array=split(/,/,@arr[1]);
$i=0;
while(@array[$i])
{
$i=$i+1;
}
printAnzahl:$i\n;
@sorty=sort(Nummernsort @array);
sub Nummernsort{
if($a$b){
return -1;
}elsif($a==$b){
return 0;
}else{
return 1;
}
}

print sort:@sorty\n;
$last=$array[$i-1];
print last:$last\n;
$next=?
printnextt=$next\n;

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Please, I need help!!!

2009-06-12 Thread Jim Gibson
On 6/11/09 Thu  Jun 11, 2009  11:00 AM, Phillip fibbe...@gmx.net
scribbled:

 Hallo @ all,
 
 i am new in this domain(perlscript) and i have a question.i have a
 array,i sort it,i get the last element of the array but i want to get
 the next element after this one.how can i do this?

There is a language called Perlscript that is based on Perl but not the
same. Are you asking about that language or about Perl itself? I will assume
the latter, as this is a Perl list, and as I cannot answer any questions
about Perlscript.

 
 for example:
 
 $arr1=(6,3,8,1) ---my last element is 1 and mark that is the last
 element
 now i sort them  $arr1=(1,3,6,8)
 my last element is 1 and now i want to go to the next element in the
 list,how can i do this?
 if i do something like this:$next=$last+1; ---in this case $next will
 be 2 and this is not my third element in my list,i want the 3.

Yes, you cannot freely intermix array indices and values.

 here is my code:
 
 @arr[1]=(3,7,13,1,19,5,9);

@arr[1] is not the syntax for an array element. It is an array slice with
one element, so might give you equivalent results in some applications, but
you should be using proper syntax. You also don't need and array here, as
you have only one string.

You should be using 'use strict;' and 'use warnings;'

my $text = '3,7,13,1,19,5,9';

 @array=split(/,/,@arr[1]);

You can use Perl's qw() operator to form literal arrays:

my @array = qw( 3 7 13 1 19 5 9 );

 $i=0;
 while(@array[$i])
 {
  $i=$i+1;
 }

Perl has a built-in syntax for the highest index of an array: $#array:

my $i = $#array;

 printAnzahl:$i\n;
 @sorty=sort(Nummernsort @array);
 sub Nummernsort{
  if($a$b){
  return -1;
  }elsif($a==$b){
  return 0;
  }else{
  return 1;
  }
 }

Perl has a built-in sort function, which defaults to alphabetical order. For
numerical order, you must supply an explicit comparison function, in which
you can use Perl's tri-state comparison operator:

my @sorty = sort { $a = $b } @array;

 
 print sort:@sorty\n;
 $last=$array[$i-1];
 print last:$last\n;
 $next=?
 printnextt=$next\n;

You can iterate over the sorted array and find the first element larger than
$last, the last element of the original array. That assumes that 1) you do
not have duplicate elements, and 2) the last element of the original array
is never the largest element in that array. If those assumptions are not
valid, some more logic will be required.

Here is a sample program:

#!/usr/local/bin/perl
use strict;
use warnings;

my @array = qw( 3 7 13 1 19 5 9 );
my $last = $array[$#array];
my @sorted = sort { $a = $b } @array;

my $next;
for ( @sorted ) {
if( $_  $last ) {
$next = $_;
last;
}
}

if( defined $next ) {
print The element after $last is $next\n;
}else{
print $last is the largest element in the array\n;
}



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Please, I need help!!!

2009-06-12 Thread Jenda Krynicky
From: Jim Gibson jimsgib...@gmail.com
 On 6/11/09 Thu  Jun 11, 2009  11:00 AM, Phillip fibbe...@gmx.net
 scribbled:
 
  Hallo @ all,
  
  i am new in this domain(perlscript) and i have a question.i have a
  array,i sort it,i get the last element of the array but i want to get
  the next element after this one.how can i do this?
 
 There is a language called Perlscript that is based on Perl but not the
 same. Are you asking about that language or about Perl itself? 

Are you sure? Any pointers?

As far as I know ActiveState has/had something called PerlScript, but 
it's not a different langauge. It's just a scripting engine for 
Windows Scripting Host, Internet Explorer and MS IIS/ASP. Just a DLL 
that allows you to use Perl in those environments. Not a different 
language.

Jenda
= je...@krynicky.cz === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Request for code feedback

2009-06-12 Thread Steve Bertrand
Hi all,

I'm still in the process of updating/replacing some accounting
applications, and thought I'd ask for a peer review.

I understand that everyone has a life et-al, but if there is a chance
that anyone could take a look at this particular piece of code I wrote
and provide some in-context feedback as to how I could make it
better/more efficient etc, it will help me a great deal going forward.

I've already got a few ideas to 'smarten-up' my two modules which are
used in the program, but my objective here is feedback on my coding
style in general.

If anyone has the time to review and report, I would much appreciate it.

Code is here:

http://ipv6canada.com/extra_billing.txt

Steve


smime.p7s
Description: S/MIME Cryptographic Signature


Re: pass int* to perl

2009-06-12 Thread sisyphus
On Fri, Jun 12, 2009 at 10:49 PM, Patrick Dupre pd...@york.ac.uk wrote:


 To pass an integer, I use to make a newSViv.
 How do I do to pass a int* ?

 Perhaps not exactly what you're after ... hope it helps:

##
use warnings;
use Inline C = Config =
BUILD_NOISY = 1;

use Inline C = 'EOC';

void foo(SV * x) {
 int i = SvIV(SvRV(x));
 printf(%d\n, i);
}

SV * bar() {
 int i, *iptr;
 SV *obj_ref, *obj;
 iptr = i;
 i = 1234567;
 obj_ref = newSV(0);
 obj = newSVrv(obj_ref, NULL);
 sv_setiv(obj, *iptr);
 SvREADONLY_on(obj); /* optional */
 return obj_ref;
}

EOC

$x = 173;
foo(\$x);

$ref = bar();
print $$ref, \n;

##

Outputs:
17
1234567


Cheers,
Rob


Copying a named hash to an anonymous hash

2009-06-12 Thread Chap Harrison
I've created a complex, nested  data structure that is, at its  
outermost level, a hash:  %qa.  It collects QA statistics for a school.


Now I want to be able to process multiple schools in one run, so I've  
created another hash, %sch_qa, whose key is School Name and whose  
value is a ref to *a copy of* %qa.


Assuming $sch_name is the name of the school, I wrote this:

if ( ! exists $sch_qa{$sch_name} )  {
%{$sch_qa{$sch_name}} = %qa;
}

That seemed to execute okay.  At the end, however, when I dumped  
%sch_qa it looked pretty strange.  Actually, the first entry looks  
somewhat normal.  It's the last three that are weird: they all seem to  
recursively refer to this entire hash.


Am I duplicating the original %qa hash correctly?

Thanks for any help!
Chap

$VAR1 = {
  '173' = {
 'M_Admins' = {
 'SchoolID' = {
 'notnull' = []
   },
 'first_name' = {
   'notnull' = []
 },
 'last_name' = {
  'notnull' = [],
  'alpha' = []
}
   },
 'M_Discipline' = {
 'Student_Number' = {

'notnull' = []

 },
 'Consequence' = {
'histo'  
= [

 {
   '011 
' = 4,
   '002 
' = 83,
   '' = 
 60,
   '081 
' = 1,
   'XRD 
' = 235,
   'XTO 
' = 60,
   '022 
' = 12,
   '004 
' = 16,

 }
   ]
  },
 'Last_Name' = {
  'notnull'  
= []

},
 'SchoolID' = {
 'constant'  
= [

 10114
   ]
   },
 'First_Name' = {
   'notnull'  
= []

 },
 'Category' = {
 'histo' = [
  {
'' = 
 13802

  }
]
   }
   },
 'M_Historical_Grades' = {
'Storecode' = {
  
'histo' = [

  {
'S1 
' = 7090,
'YR 
' = 12548,
'S2 
' = 3783,
'2007 
-2' = 7,
'SS 
' = 126

  }
 

Re: Please, I need help!!!

2009-06-12 Thread John W. Krahn

Phillip wrote:

Hallo @ all,


Hello,

i am new in this domain(perlscript) and i have a question.i have a 
array,i sort it,i get the last element of the array but i want to get 
the next element after this one.how can i do this?


for example:

$arr1=(6,3,8,1) ---my last element is 1 and mark that is the last 
element

now i sort them  $arr1=(1,3,6,8)
my last element is 1 and now i want to go to the next element in the 
list,how can i do this?
if i do something like this:$next=$last+1; ---in this case $next will 
be 2 and this is not my third element in my list,i want the 3.

BR,
Phil

here is my code:

@arr[1]=(3,7,13,1,19,5,9);
@array=split(/,/,@arr[1]);


That should be:

my @array = ( 3, 7, 13, 1, 19, 5, 9 );


$i=0;
while(@array[$i])
{
$i=$i+1;
}
printAnzahl:$i\n;


print Anzahl:, scalar @array, \n;


@sorty=sort(Nummernsort @array);
sub Nummernsort{
if($a$b){
return -1;
}elsif($a==$b){
return 0;
}else{
return 1;
}
}


my @sorty = sort Nummernsort @array;
sub Nummernsort { $a = $b }


print sort:@sorty\n;
$last=$array[$i-1];


my $last = $array[ -1 ];


print last:$last\n;
$next=?


Do you want the next-to-last?

my $next = $array[ -2 ];


printnextt=$next\n;



John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




RE: Request for code feedback

2009-06-12 Thread David Christensen
Steve Bertrand wrote:
 If anyone has the time to review and report, I would much appreciate
it.
 http://ipv6canada.com/extra_billing.txt

1.  I don't see a header comment block.

2.  I don't see comment section boundaries.

3.  I don't see author information.

4.  I don't see a copyright notice.

5.  I don't see any POD.

6.  I don't see command line options/ processing.

7.  I don't see a usage message.

8.  I prefer 4 column indentation.

9.  I prefer limiting my code to 72 columns.

10. I don't see a version control system tag; I use CVS and put $Id$
near the top of all my files.  Version control is a key differentiator
between hacked code and professional systems software product.  If your
company is using this script to bill people, your company and customers
expect the latter.

11. Use h2xs to create a module and move your code into it.  (Another
key differentiator.)

12. Do you have automated regression tests?  (Another key
differentiator.)

13. You're using a lot of 'my' variables to fetch fields from data
structures.  I do this and/or suffer through with the full syntax, but
have always wanted a cleaner solution.  Perhaps a full set of accessors?
AUTOLOAD?  Alias?


Here's a fairly current example of my coding style which demonstrates
all of the above points:

http://www.holgerdanske.com/node/460


HTH,

David


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/