[19078] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 1273 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jul 11 03:16:51 2001

Date: Wed, 11 Jul 2001 00:16:32 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <994835791-v10-i1273@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Wed, 11 Jul 2001     Volume: 10 Number: 1273

Today's topics:
        complex perl problem <bart@nijlen.com>
    Re: complex perl problem <jeff@vpservices.com>
    Re: complex perl problem (Logan Shaw)
    Re: complex perl problem <mdufault@dynamicservers.com>
    Re: complex perl problem <todd@designsouth.net>
    Re: complex perl problem <krahnj@acm.org>
    Re: complex perl problem <krahnj@acm.org>
    Re: complex perl problem <EvR@compuserve.com>
    Re: complex perl problem <bart@nijlen.com>
    Re: complex perl problem (Craig Berry)
    Re: complex perl problem <krahnj@acm.org>
        Confusing Cookie Conundrum! (Smiley)
    Re: Confusing Cookie Conundrum! <bill.kemp@wire2.com>
    Re: Confusing Cookie Conundrum! (Smiley)
    Re: Confussed about s/^([a-z0-9]+)/$1/ behaviour (F. Xavier Noria)
    Re: csv file conversion and speed.. <krahnj@acm.org>
    Re: csv file conversion and speed.. (Yves Orton)
        Data structures question. (Garry)
    Re: Data structures question. <mjcarman@home.com>
    Re: DBI/MySQL: finding ID of an added record <fty@mediapulse.com>
    Re: DBI/MySQL: finding ID of an added record <michael_w@pacific.net.au>
    Re: double key for hashtable <goldbb2@earthlink.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Mon, 09 Jul 2001 21:06:11 GMT
From: "Bart Van der Donck" <bart@nijlen.com>
Subject: complex perl problem
Message-Id: <71p27.5087$z7.1278297@afrodite.telenet-ops.be>

Hello,

The number of strings in an array is not known.
However it is known that the number of strings in the array is a multiple of
4.
How can I sort this array so that I get the following order:

output: 1 2 5 6 3 4 7 8
supposed that @b was
@b =("1","2","3","4","5","6","7","8");

or

output: 1 2 7 8 3 4 9 10 5 6 11 12
supposed that @b was
@b =("1","2","3","4","5","6","7","8","9","10","11","12");

or

output: 1 2 9 10 3 4 11 12 5 6 13 14 7 8 15 16
supposed that @b was
@b
=("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16");

I managed to get it work only when the exact number of strings in the array
is known (so the digits in "splice" can be filled out).

Thanks for help or suggestions
Bart  vdd




------------------------------

Date: Mon, 09 Jul 2001 15:19:49 -0700
From: Jeff Zucker <jeff@vpservices.com>
Subject: Re: complex perl problem
Message-Id: <3B4A2E05.D58E4093@vpservices.com>

Bart Van der Donck wrote:
> 
> The number of strings in an array is not known.
> However it is known that the number of strings in the array is a multiple of
> 4.
> How can I sort this array so that I get the following order:
> 
> output: 1 2 5 6 3 4 7 8
> supposed that @b was
> @b =("1","2","3","4","5","6","7","8");
> 
> or
> 
> output: 1 2 7 8 3 4 9 10 5 6 11 12
> supposed that @b was
> @b =("1","2","3","4","5","6","7","8","9","10","11","12");
> 
> or
> 
> output: 1 2 9 10 3 4 11 12 5 6 13 14 7 8 15 16
> supposed that @b was
> @b
> =("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16");

Well, you left a number of things unspecified, but guessing at some of
those, this produces the desired output:

#!/usr/local/bin/perl -w
use strict;
fiddle( qw(1 2 3 4 5 6 7 8) );
fiddle( qw(1 2 3 4 5 6 7 8 9 10 11 12) );
fiddle( qw(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16) );
fiddle( qw(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) );
# etc.

sub fiddle {
    my @ary = @_;
    my $org_length = scalar @ary;
    my $offset = (($org_length/4)-1)*2;
    my @removes = splice(@ary,2,$offset);
    my $new;
    for (0..3) {
       push @$new, shift @ary;
    }
    for (0..($offset-1)/2) {
       push @$new, shift @removes;
       push @$new, shift @removes;
       push @$new, shift @ary;
       push @$new, shift @ary;
    }
    printf "%3s : %s\n", $org_length, join( ',',@$new);
}
__END__

-- 
Jeff Zucker



------------------------------

Date: 9 Jul 2001 17:28:14 -0500
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: complex perl problem
Message-Id: <9idb5u$jr6$1@charity.cs.utexas.edu>

In article <71p27.5087$z7.1278297@afrodite.telenet-ops.be>,
Bart Van der Donck <bart@nijlen.com> wrote:
>The number of strings in an array is not known.

That's not possible.  The number of strings in "@foo" is always
"scalar @foo".

>However it is known that the number of strings in the array is a multiple of
>4.
>How can I sort this array so that I get the following order:
>
>output: 1 2 5 6 3 4 7 8
>supposed that @b was
>@b =("1","2","3","4","5","6","7","8");
>
>or
>
>output: 1 2 7 8 3 4 9 10 5 6 11 12
>supposed that @b was
>@b =("1","2","3","4","5","6","7","8","9","10","11","12");

Assuming you're referring to a list and not an array (in which case the
length might not be known in advance), your task is impossible, because
the third item in the list is always element number $n/2 + 1, where $n
is the number of elements in the list.  So, without knowing the length
of the list, there is no way to know what to put in the third
position.

Or to be more accurate, I don't believe you can complete the described
task in any way that wouldn't also allow you to easily compute the
length of the list if you didn't know it.

Maybe you're asking for a solution that doesn't do any arithmetic with
the length of the list.  If so, I'm pretty sure it's possible to do it
recursively, although you still have to pop items off the end of the
list (which requires knowing where the end is) and test whether the
list is empty (which requires knowing something about the length).

  - Logan
-- 
my  your   his  her   our   their   _its_
I'm you're he's she's we're they're _it's_


------------------------------

Date: Mon, 09 Jul 2001 23:18:34 GMT
From: "Mark W. Dufault" <mdufault@dynamicservers.com>
Subject: Re: complex perl problem
Message-Id: <eZq27.149517$v5.11393747@news1.rdc1.ct.home.com>

I believe you can use $#b to get the index of the last element in the array
I'm not sure what your trying to do but that's my take...

"Bart Van der Donck" <bart@nijlen.com> wrote in message
news:71p27.5087$z7.1278297@afrodite.telenet-ops.be...
> Hello,
>
> The number of strings in an array is not known.
> However it is known that the number of strings in the array is a multiple
of
> 4.
> How can I sort this array so that I get the following order:
>
> output: 1 2 5 6 3 4 7 8
> supposed that @b was
> @b =("1","2","3","4","5","6","7","8");
>
> or
>
> output: 1 2 7 8 3 4 9 10 5 6 11 12
> supposed that @b was
> @b =("1","2","3","4","5","6","7","8","9","10","11","12");
>
> or
>
> output: 1 2 9 10 3 4 11 12 5 6 13 14 7 8 15 16
> supposed that @b was
> @b
> =("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16");
>
> I managed to get it work only when the exact number of strings in the
array
> is known (so the digits in "splice" can be filled out).
>
> Thanks for help or suggestions
> Bart  vdd
>
>




------------------------------

Date: Mon, 09 Jul 2001 23:41:12 GMT
From: "Todd Smith" <todd@designsouth.net>
Subject: Re: complex perl problem
Message-Id: <sir27.23790$B5.5132281@news1.rdc1.tn.home.com>

> The number of strings in an array is not known.
> However it is known that the number of strings in the array is a multiple
of
> 4.
> How can I sort this array so that I get the following order:
>
> output: 1 2 5 6 3 4 7 8
> supposed that @b was
> @b =("1","2","3","4","5","6","7","8");
>

Here's my answer:

#!/usr/bin/perl

@a = $ARGV[0]..$ARGV[1];

$half_index = (@a+1)/2-1;

@one = @a[0..$half_index];
@two = @a[$half_index+1..$#a];

while (@two) {
        push @return, (shift @one, shift @one), (shift @two, shift @two);
}

print join ', ', @return;

[admin admin]$ ./help 1 12
1, 2, 7, 8, 3, 4, 9, 10, 5, 6, 11, 12
[admin admin]$





------------------------------

Date: Mon, 09 Jul 2001 23:43:23 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: complex perl problem
Message-Id: <3B4A41E4.1EF3A65@acm.org>

Bart Van der Donck wrote:
> 
> The number of strings in an array is not known.
> However it is known that the number of strings in the array is a multiple of
> 4.
> How can I sort this array so that I get the following order:
> 
> output: 1 2 5 6 3 4 7 8
> supposed that @b was
> @b =("1","2","3","4","5","6","7","8");
> 
> or
> 
> output: 1 2 7 8 3 4 9 10 5 6 11 12
> supposed that @b was
> @b =("1","2","3","4","5","6","7","8","9","10","11","12");
> 
> or
> 
> output: 1 2 9 10 3 4 11 12 5 6 13 14 7 8 15 16
> supposed that @b was
> @b
> =("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16");
> 
> I managed to get it work only when the exact number of strings in the array
> is known (so the digits in "splice" can be filled out).



my @b = 1 .. 16;

print "@b\n@{[permute( @b )]}\n";

sub permute {

    return "Input error" if @_ % 4;

    my @x;
    my $half = @_ / 2;

    for ( my $x = 0, my $y = $half; $x < $half; $x += 2, $y += 2 ) {
        push @x, @_[ $x .. $x + 1 ], @_[ $y .. $y + 1 ];
        }

    return @x;
    }



John
-- 
use Perl;
program
fulfillment


------------------------------

Date: Mon, 09 Jul 2001 23:52:40 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: complex perl problem
Message-Id: <3B4A4412.22A7E871@acm.org>

Todd Smith wrote:
> 
> Here's my answer:
> 
> #!/usr/bin/perl
> 
> @a = $ARGV[0]..$ARGV[1];
> 
> $half_index = (@a+1)/2-1;
                   ^^   ^^

Why, why, oh my $DEITY why are you adding one on one side and
subtracting it on the other side?



John
-- 
use Perl;
program
fulfillment


------------------------------

Date: Mon, 9 Jul 2001 19:36:52 -0600
From: "Richard A. Evans" <EvR@compuserve.com>
Subject: Re: complex perl problem
Message-Id: <9idm4f$sin$1@suaar1ab.prod.compuserve.com>

I believe this code snippet does the trick:

    for ( $i=0; $i <= $#b/2; $i+=2 ) {
      print "$b[$i] $b[1+$i] $b[1+$i+$#b/2] $b[2+$i+$#b/2] ";
    }


A small test program:

  foreach $end ( 4, 8, 12, 16, 20, 24 ) {
    @b = ( 1 .. $end );
    print "\n  input:  ", join " ", @b;

    print "\n output:  ";
    for ( $i=0; $i <= $#b/2; $i+=2 ) {
      print "$b[$i] $b[1+$i] $b[1+$i+$#b/2] $b[2+$i+$#b/2] ";
    }
    print "\n";
  }


Output of this program:

C:\test>perl test.pl

  input:  1 2 3 4
 output:  1 2 3 4

  input:  1 2 3 4 5 6 7 8
 output:  1 2 5 6 3 4 7 8

  input:  1 2 3 4 5 6 7 8 9 10 11 12
 output:  1 2 7 8 3 4 9 10 5 6 11 12

  input:  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
 output:  1 2 9 10 3 4 11 12 5 6 13 14 7 8 15 16

  input:  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 output:  1 2 11 12 3 4 13 14 5 6 15 16 7 8 17 18 9 10 19 20

  input:  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 output:  1 2 13 14 3 4 15 16 5 6 17 18 7 8 19 20 9 10 21 22 11 12 23 24


Kind regards,


Rick Evans




------------------------------

Date: Tue, 10 Jul 2001 08:40:50 GMT
From: "Bart Van der Donck" <bart@nijlen.com>
Subject: Re: complex perl problem
Message-Id: <mcz27.5397$z7.1363660@afrodite.telenet-ops.be>

Thanks to all.
I got it working now exactly as it should.

"Richard A. Evans" <EvR@compuserve.com> schreef in bericht
news:9idm4f$sin$1@suaar1ab.prod.compuserve.com...
> I believe this code snippet does the trick:
>
>     for ( $i=0; $i <= $#b/2; $i+=2 ) {
>       print "$b[$i] $b[1+$i] $b[1+$i+$#b/2] $b[2+$i+$#b/2] ";
>     }
>
>
> A small test program:
>
>   foreach $end ( 4, 8, 12, 16, 20, 24 ) {
>     @b = ( 1 .. $end );
>     print "\n  input:  ", join " ", @b;
>
>     print "\n output:  ";
>     for ( $i=0; $i <= $#b/2; $i+=2 ) {
>       print "$b[$i] $b[1+$i] $b[1+$i+$#b/2] $b[2+$i+$#b/2] ";
>     }
>     print "\n";
>   }
>
>
> Output of this program:
>
> C:\test>perl test.pl
>
>   input:  1 2 3 4
>  output:  1 2 3 4
>
>   input:  1 2 3 4 5 6 7 8
>  output:  1 2 5 6 3 4 7 8
>
>   input:  1 2 3 4 5 6 7 8 9 10 11 12
>  output:  1 2 7 8 3 4 9 10 5 6 11 12
>
>   input:  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
>  output:  1 2 9 10 3 4 11 12 5 6 13 14 7 8 15 16
>
>   input:  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
>  output:  1 2 11 12 3 4 13 14 5 6 15 16 7 8 17 18 9 10 19 20
>
>   input:  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
>  output:  1 2 13 14 3 4 15 16 5 6 17 18 7 8 19 20 9 10 21 22 11 12 23 24
>
>
> Kind regards,
>
>
> Rick Evans
>
>




------------------------------

Date: Tue, 10 Jul 2001 16:51:02 -0000
From: cberry@cinenet.net (Craig Berry)
Subject: Re: complex perl problem
Message-Id: <tkmcjmatie36c2@corp.supernews.com>

Bart Van der Donck (bart@nijlen.com) wrote:
: The number of strings in an array is not known.
: However it is known that the number of strings in the array is a multiple of
: 4. How can I sort this array so that I get the following order:
: 
: output: 1 2 5 6 3 4 7 8
: supposed that @b was
: @b =("1","2","3","4","5","6","7","8");

From this and other examples, I assume the idea is to take pairs from the
bottom and top halves of the sequence alternately.

#!/usr/bin/perl -w
# pairwise - generate a sequence per clpm request
# Craig Berry (20010710)

use strict;

sub pairwise($)
{
  my $size = shift;
  die "Size must be multiple of 4" if $size % 4;
  my $half = $size / 2;
  my @seq;
  for (my $i = 1; $i < $half; $i += 2) {
    push @seq, $i, $i + 1, $half + $i, $half + $i + 1;
  }
  return @seq;
}

print join(' ', pairwise $_), "\n" while <>;

-- 
   |   Craig Berry - http://www.cinenet.net/~cberry/
 --*--  "Brute force done fast enough looks slick."
   |             - William Purves


------------------------------

Date: Tue, 10 Jul 2001 19:20:10 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: complex perl problem
Message-Id: <3B4B55B3.99D54A47@acm.org>

> John Imrie wrote:
> 
> >>>>>>>>>>>>>>>>>> Original Message <<<<<<<<<<<<<<<<<<
> 
> On 10/07/01, 00:52:40, "John W. Krahn" <krahnj@acm.org> wrote regarding
> Re: complex perl problem:
> 
> > Todd Smith wrote:
> > >
> > > Here's my answer:
> > >
> > > #!/usr/bin/perl
> > >
> > > @a = $ARGV[0]..$ARGV[1];
> > >
> > > $half_index = (@a+1)/2-1;
> >                    ^^   ^^
> 
> > Why, why, oh my $DEITY why are you adding one on one side and
> > subtracting it on the other side?
> 
> This is
>                @a + 1
> $half_index = --------  -1
>                   2
> 
> which is equivalent to  @a + 1 - 2
>                         ----------
>                             2
> 
> or  @a -1
>    -------
>       2
> 
> which in perl gives
> 
> $half_index = (@a - 1)/2;

That's lovely but it still doesn't explain why.

From Todd's example:

> $half_index = (@a+1)/2-1;
> 
> @one = @a[0..$half_index];
> @two = @a[$half_index+1..$#a];

$half_index when used in this context will be an integer so why not
just:

$half_index = @a/2-1;

as it achieves the same result. Or why not do it simply as:

$half_index = @a / 2;

@one = @a[ 0 .. $half_index - 1 ];
@two = @a[ $half_index .. $#a ];




John
-- 
use Perl;
program
fulfillment


------------------------------

Date: Mon, 09 Jul 2001 18:49:44 GMT
From: gurm@intrasof.com (Smiley)
Subject: Confusing Cookie Conundrum!
Message-Id: <3b49f574.145682820@news1.on.sympatico.ca>

Okay, here's the situation:

I have 2 Perl CGI scripts, each is set up to create a cookie by the
name of 'category'.  Both scripts work well when I test them by
themselves, but there's one problem that confuses me to no end.

When I run Script A, it sets the value of the cookie 'category'
appropriately.  Then when I run Script B after running Script A, it
seems that Script B is unable to alter the value of the cookie
'category', which to my disapointment remains set to the last value
set by Script A.

Hope I haven't lost you here :)

Anyway, I know that Script B works well when the cookie has not
previously been set by Script A.  I'm at a loss to explain why one
script would be unable to over-write a cookie set by the other.  Can
anybody give me a reason why this might be happening, and perhaps a
possible solution?  


------------------------------

Date: Mon, 9 Jul 2001 16:56:18 +0100
From: "W K" <bill.kemp@wire2.com>
Subject: Re: Confusing Cookie Conundrum!
Message-Id: <swk27.1702$h45.11654@news.uk.colt.net>


Smiley wrote in message <3b49f574.145682820@news1.on.sympatico.ca>...
>Okay, here's the situation:
>
>I have 2 Perl CGI scripts, each is set up to create a cookie by the
>name of 'category'.  Both scripts work well when I test them by
>themselves, but there's one problem that confuses me to no end.
>
>When I run Script A, it sets the value of the cookie 'category'
>appropriately.  Then when I run Script B after running Script A, it
>seems that Script B is unable to alter the value of the cookie
>'category', which to my disapointment remains set to the last value
>set by Script A.
>
>Anyway, I know that Script B works well when the cookie has not
>previously been set by Script A.  I'm at a loss to explain why one
>script would be unable to over-write a cookie set by the other.  Can
>anybody give me a reason why this might be happening, and perhaps a
>possible solution?

Minor spelling mistakes in the names?
Not setting the same path in both cookies?

I had this, but I forget what the problem was. Seem to think it was the
path.




------------------------------

Date: Mon, 09 Jul 2001 19:16:42 GMT
From: gurm@intrasof.com (Smiley)
Subject: Re: Confusing Cookie Conundrum!
Message-Id: <3b4a02a0.149055650@news1.on.sympatico.ca>

Thanks a lot Bill!

The path was the problem.  You'd think that my second script would
have just overwritten the previous cookie and specified the new path
instead, funny how that works.  

Thanks for the info, I hadn't realized that the path mattered in such
a way.

On Mon, 9 Jul 2001 16:56:18 +0100, "W K" <bill.kemp@wire2.com> wrote:

>
>Minor spelling mistakes in the names?
>Not setting the same path in both cookies?
>
>I had this, but I forget what the problem was. Seem to think it was the
>path.
>
>



------------------------------

Date: 11 Jul 2001 07:05:58 GMT
From: fxn@retemail.es (F. Xavier Noria)
Subject: Re: Confussed about s/^([a-z0-9]+)/$1/ behaviour
Message-Id: <9igtsm$3e08m2@news1s.iddeo2.es>

On Fri, 06 Jul 2001 09:03:03 +0100, John Tutchings <ccx138@coventry.ac.uk> wrote:

: This is probably simple but I can't see it why it does not work.
: 
: If I do perl -pe "s/^([a-z0-9]+):/$1:pig/ unless(/^ccx/)" test.dat
: the result is
:     ccx002:27248
:     ccx003:27249
:     :pig27333
:     :pig27350

<snip>

: bash$ cat test.dat

Since you are using double quotes in Bash, that $1 is interpolated by
the shell before perl gets called. The program sees probably something
like this

   perl -pe "s/^([a-z0-9]+):/:pig/ unless(/^ccx/)"

because $1 is likely to be empty as a shell variable.

-- fxn


------------------------------

Date: Mon, 09 Jul 2001 22:37:36 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: csv file conversion and speed..
Message-Id: <3B4A3231.CF31440@acm.org>

Wayne Marrison wrote:
> 
> Dear all,
> 
> I have a file (well, many acutally) that contain in excess of 100k lines in
> each.
> 
> The file is standard CSV format (quoted strings, bare numbers & dates
> (dd/mm/yy)) that is fairly easy to parse using the examples to be found in
> the FAQ.
> 
> The problem is ... that using a combination of sed/awk on the same set of
> files rather than perl, takes approx 2./3'ds of the time.
> 
> I have to parse the lines out of the file using a unique field seperator (Im
> currently using ^] as a field delimiter) to retain the commas within the
> quoted fields.
> 
> The way the data is presented, its easy to change "," & ", to the unique
> seperator, and then read in the line, modifiying the old field seperator (,)
> between the dates & numbers (as they now appear to be one field, so I can
> split it down using the perl split function) to the unique field seperator
> and write them back to disk.
> 
> Later on, another script can read the converted file using the unique field
> splitter as a delimiter.
> 
> (forgive the poor description.. heres an example)...
> 
> Raw
> "A","123, somewhere street","anywhere",123.80,12/6/99,400.78,341.09,31/5/00
> 
> After Initial Conversion
> A^]123,somewhere street^]anywhere^]123.80,12/6/99,400.78,341.09,31/5/00
> 
> The numbers at the end appear to be 1 complete field .. i.e.
> 
> 123.80,12/6/99,400.78,341.09,31/5/00
> 
> I can then convert the , to ^] and write it back to the file.....
> 
> The only problem here, as I stated above is that it takes much longer to do
> this in Perl than the equivalent sed/awk.  And as the perl used to do the
> parsing is straight out of the FAQ, Im at a bit of a loss on how to speed it
> up.
> 
> I have written a routine that uses sysread/write & tr to perform most of the
> operations, but I would still have to run through the file line by line
> afterwards and do the final conversion of the commas.
> 
> Any help here would be appreciated...

Since you didn't provide any code it's kind of hard to tell what you can
do to improve it. Of course you could always write it in C if you want
real speed.  :-)




John
-- 
use Perl;
program
fulfillment


------------------------------

Date: 10 Jul 2001 08:47:43 -0700
From: demerphq@hotmail.com (Yves Orton)
Subject: Re: csv file conversion and speed..
Message-Id: <74f348f7.0107100747.35028aa@posting.google.com>

"Wayne Marrison" <wayne.marrison@consignia.com> wrote in message news:<994687021.266653@igateway.postoffice.co.uk>...
> I have a file (well, many acutally) that contain in excess of 100k lines in
> each.
> The file is standard CSV format (quoted strings, bare numbers & dates
> (dd/mm/yy)) that is fairly easy to parse using the examples to be found in
> the FAQ.

Are you sure?  Be aware that the example from Friedl's book (and the
FAQ) will NOT handle excel CSV files correctly (try putting a " symbol
in an excel cell, saving as csv and parsing with the sample code), nor
will it handle CSV data that may contain embedded newlines.

> 
> The problem is ... that using a combination of sed/awk on the same set of
> files rather than perl, takes approx 2./3'ds of the time.

Weird.  What are you doing wrong?

> I have to parse the lines out of the file using a unique field seperator (Im
> currently using ^] as a field delimiter) to retain the commas within the
> quoted fields.
>
> The way the data is presented, its easy to change "," & ", to the unique
> seperator, and then read in the line, modifiying the old field seperator (,)
> between the dates & numbers (as they now appear to be one field, so I can
> split it down using the perl split function) to the unique field seperator
> and write them back to disk.

Strange. I dont understand why you dont just

my @parts=SplitCSV($_); #Friedls code...
print OUT join($seperator,@parts).$endofrecord;

> <SNIP> 

> The only problem here, as I stated above is that it takes much longer to do
> this in Perl than the equivalent sed/awk.  And as the perl used to do the
> parsing is straight out of the FAQ, Im at a bit of a loss on how to speed it
> up.

My guess, without seeing the code is that you are doing unnecessary
work. Maybe too many passes over the data?

Send your code.

Yves


------------------------------

Date: 10 Jul 2001 07:29:25 -0700
From: garry_short@hotmail.com (Garry)
Subject: Data structures question.
Message-Id: <bdcefd33.0107100629.7e970dcf@posting.google.com>

Hello.

I'm trying to work out one of two things, assuming either is possible.

The first option is a 2d array, 9 columns wide by 10k+ rows deep. Is
it possible to sort the array by the nth row? If so, could anyone give
me some pointers on how to do it?

Better still, can I sort that array by multiple columns, as per Excel?
If so, does anyone what's the maximum number of columns I can sort by?
Again, any pointers on how to do it would be appreciated!

I could do this easily using an Excel macro, but I'd rather not! :-)

--

Assuming that's not possible, has anyone got any suggestions on
implementing the following data structure :

array of (unique SRC_IP, 
  array of (unique SRC_PORT, 
    array of (unique DEST_IP, 
      array of (unique DEST_PORT, 
        array of (unique PROTOCOL, 
          array of (unique DATE, 
            array of (unique TIME, 
              array of (TYPE, CODE)
            )
          )
        )
      )
    )
  )
)

I've been thinking about this for a couple of days, on and off, and
still haven't really got my head around it (I only started playing
with Perl at the start of the year; before that, I've not done
anything except Excel VBA since uni, 8yrs ago).

Any suggestions, thoughts, ideas, kicks up the behind, etc, would be
welcome!

TIA,

Garry


------------------------------

Date: Tue, 10 Jul 2001 11:09:23 -0500
From: Michael Carman <mjcarman@home.com>
Subject: Re: Data structures question.
Message-Id: <3B4B28B3.61950B7C@home.com>

Garry wrote:
> 
> I'm trying to work out one of two things, assuming either is 
> possible.

In Perl, damn near anything is possible.
 
> The first option is a 2d array, 9 columns wide by 10k+ rows deep.
> Is it possible to sort the array by the nth row? If so, could
> anyone give me some pointers on how to do it?

Yes, see the perlfaq4 entry "How do I sort an array by (anything)?"
 
> Better still, can I sort that array by multiple columns

Of course.

> If so, does anyone what's the maximum number of columns I can
> sort by?

None that I'm aware of. I've often sorted things by half a dozen or more
fields.

> Again, any pointers on how to do it would be appreciated!

The answers you seek are in the FAQ.
 
> Assuming that's not possible, has anyone got any suggestions on
> implementing the following data structure :
> 
> array of (unique SRC_IP,
>   array of (unique SRC_PORT,
>     array of (unique DEST_IP,
>       array of (unique DEST_PORT,
>         array of (unique PROTOCOL,
>           array of (unique DATE,
>             array of (unique TIME,
>               array of (TYPE, CODE)
>             )
>           )
>         )
>       )
>     )
>   )
> )

Hrm. I'm not sure I understand. That pseudocode looks like you want a
eight-dimensional array! (Each SRC_IP would have multiple SRC_PORTs,
each SRC_PORT multiple DEST_IPs, etc...) I don't think you really want
that. :)

I'd suggest using a LoH (list of hashes). Something like this:

#!/usr/bin/perl -w
use strict;

my @connection;

while (<DATA>) {
    next if /^#/;
    chomp;
    my ($src_ip, $src_port, $dest_ip, $dest_port) = split;

    my %link = (
        SRC_IP    => $src_ip,
        SRC_PORT  => $src_port,
        DEST_IP   => $dest_ip,
        DEST_PORT => $dest_port,
    );
    push @connection, \%link;
}

foreach my $n (0 .. $#connection) {
    my $link = $connection[$n];
    print "$n:\n";
    foreach my $k (sort keys %$link) {
        print "\t$k - $link->{$k}\n";
    }
}
    
__DATA__
# src_ip  src_port dest_ip  dest_port
1.1.1.1   80       2.1.1.1  80
1.1.1.2   10       2.1.1.2  34
1.1.1.3   40       2.1.1.3  12

-mjc


------------------------------

Date: Mon, 09 Jul 2001 19:33:25 GMT
From: "Jay Flaherty" <fty@mediapulse.com>
Subject: Re: DBI/MySQL: finding ID of an added record
Message-Id: <9Gn27.34879$2O6.2533984@news2.aus1.giganews.com>


"Michael Budash" <mbudash@sonic.net> wrote in message
news:mbudash-90207E.18173705072001@news.sonic.net...
> In article <20010705191745.759$A3@newsreader.com>, ctcgag@hotmail.com
> wrote:
>
> > "Stuart Moore" <stumo@bigfoot.com> wrote:
> > Try running the query "select last_insert_id()"
>
> you can also use:
>
> $lastid = $sth->{insertid};

it is $sth->{'mysql_insertid'};
or alternatively via the database handle, i.e. $dbh->{'mysql_insertid'};

jay




------------------------------

Date: Tue, 10 Jul 2001 13:21:56 +1000
From: Michael Walmsley <michael_w@pacific.net.au>
Subject: Re: DBI/MySQL: finding ID of an added record
Message-Id: <3B4A74D4.4090103@pacific.net.au>

Yep these are ture - and fully documented on the DBD::MySQL pod 
documentation.

Getting the Last inserted ID is MySQL specific and not available in the 
vanilla DBI interface.  this is often the case with DB specific 
functionality.

If you check the perl documentaiton on the MySQL database Driver there 
are a number of MySQL specific features you can access.

The more of these features you use (of course) will limit the actual 
portability of your code to other DBMS's - but that's  design decision 
for you to consider. :-)



Jay Flaherty wrote:

> "Michael Budash" <mbudash@sonic.net> wrote in message
> news:mbudash-90207E.18173705072001@news.sonic.net...
> 
>>In article <20010705191745.759$A3@newsreader.com>, ctcgag@hotmail.com
>>wrote:
>>
>>
>>>"Stuart Moore" <stumo@bigfoot.com> wrote:
>>>Try running the query "select last_insert_id()"
>>>
>>you can also use:
>>
>>$lastid = $sth->{insertid};
>>
> 
> it is $sth->{'mysql_insertid'};
> or alternatively via the database handle, i.e. $dbh->{'mysql_insertid'};
> 
> jay
> 
> 
> 



------------------------------

Date: Tue, 10 Jul 2001 23:45:49 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: double key for hashtable
Message-Id: <3B4BCBED.F901341@earthlink.net>

Torsten Drees wrote:
> 
> Hi people,
> 
> i have an interesting problem. i wrote two records in a hash table
> with the same key. Is it possible to get back, both records.
> If not, is it possible to define a subkey?

You can, though it's not exactly pretty.

use DB_File;
$DB_TREE->{flags} |= R_DUP; # allow duplicate keys.

my $d = tie my %hash, "DB_File", undef, O_CREAT|O_RDWR, undef, $DB_TREE;
# create an in-memory db_file.

$hash{key} = "value1";
$hash{key} = "value2";
# store two values for the one key.

@values = $d->get_dup("key");
# puts value1 and value2 in @values, in arbitrary order.
# This is needed because $hash{key} will only return one of the values.

# Also, since each() uses an ordinary FETCH for the value, you can't
# use while( my ($key,$value) = each %hash ), you have to do instead:
my $status, $key, $value;
for(	$status = $d->seq( $key, $value, R_FIRST ); !$status;
	$status = $d->seq( $key, $value, R_NEXT ) ) {
	print "key = $key, value = $value\n";
}

# I don't know if delete will get rid of one or both values, but either
# way, you will occasionally want to delete just one value:
$d->sel_dup( "key", "value1" );

# I could go on, but you'd be better off reading the DB_File docs.

-- 
The longer a man is wrong, the surer he is that he's right.


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 1273
***************************************


home help back first fref pref prev next nref lref last post