[28380] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 9744 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Sep 20 14:05:47 2006

Date: Wed, 20 Sep 2006 11:05:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Wed, 20 Sep 2006     Volume: 10 Number: 9744

Today's topics:
    Re: array of hashes with a little more... <jgibson@mail.arc.nasa.gov>
    Re: array of hashes with a little more... xhoster@gmail.com
    Re: array of hashes with a little more... <rvtol+news@isolution.nl>
    Re: Coderef to object methods? anno4000@radom.zrz.tu-berlin.de
        explaining how memory works with tie()ed hashs <botfood@yahoo.com>
        Help with plotting <pradeep.bg@gmail.com>
    Re: How do I find the Nth index of array that is (whate <tzz@lifelogs.com>
    Re: How do I find the Nth index of array that is (whate <benmorrow@tiscali.co.uk>
        inserting lines <noreply@hotmail.com>
    Re: inserting lines <David.Squire@no.spam.from.here.au>
    Re: inserting lines as4109@wayne.edu
    Re: inserting lines <David.Squire@no.spam.from.here.au>
    Re: inserting lines <jgibson@mail.arc.nasa.gov>
    Re: inserting lines <uri@stemsystems.com>
    Re: inserting lines xhoster@gmail.com
    Re: inserting lines <David.Squire@no.spam.from.here.au>
    Re: inserting lines <someone@example.com>
    Re: Net::Telnet, script exits after connection failure <Janin.Netosoite@gmail.com>
        Parallel::ForkManager not working as intended on Window <schaitan@gmail.com>
    Re: Parallel::ForkManager not working as intended on Wi xhoster@gmail.com
        Parse CSV file mmittiga17@gmail.com
    Re: Parse CSV file <David.Squire@no.spam.from.here.au>
    Re: Parse CSV file <xicheng@gmail.com>
    Re: Parse CSV file <mritty@gmail.com>
    Re: Server/client over a network <deadpickle@gmail.com>
        Trying to match keys of hash with DBI fetchrow_hashref <bryan@worldspice.net>
    Re: Trying to match keys of hash with DBI fetchrow_hash <mritty@gmail.com>
    Re: Trying to match keys of hash with DBI fetchrow_hash <bryan@worldspice.net>
    Re: Trying to match keys of hash with DBI fetchrow_hash <mritty@gmail.com>
    Re: Trying to match keys of hash with DBI fetchrow_hash <bryan@worldspice.net>
    Re: Windows 2000/XP: Owner of a File <rvtol+news@isolution.nl>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 20 Sep 2006 09:15:53 -0700
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: array of hashes with a little more...
Message-Id: <200920060915530847%jgibson@mail.arc.nasa.gov>

In article <1158717875.079376.322330@h48g2000cwc.googlegroups.com>,
<usenet@DavidFilmer.com> wrote:

> mikeybe wrote:
> 

[problem description of dewey decimal ranges snipped]

> 
> How about this:
> 
> #!/usr/bin/perl
>    use strict; use warnings;
>    use Data::Dumper;
> 
>    my @deweys=qw{000-099 100-199 200-299 300-399 400-499 500-599};

There is redundant information in this array. I would just store the
starting values and use the appropriate comparison operators and
succesive elements in the array to define a range:

   my @deweys = qw{ 000 100 200 300 400 500 };


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

Date: 20 Sep 2006 16:14:44 GMT
From: xhoster@gmail.com
Subject: Re: array of hashes with a little more...
Message-Id: <20060920121610.652$L0@newsreader.com>

anno4000@radom.zrz.tu-berlin.de wrote:
>  <usenet@DavidFilmer.com> wrote in comp.lang.perl.misc:
> > mikeybe wrote:
> >
> > > I only have to edit the deweystring array. I need to
> > > create the hash keys from a variable name (@deweystring), I need to
> > > dynamically analyze each line to determine which hash key it belongs
> > > in...so if a record had a dewey number of 654 it would be determined
> > > that it belongs in the 600-700 hash key array

Why not just make them 0xx, 1xx, 2xx, 3xx, etc?  Especially as the end
point is confusing, it should be 600-699, no?

$dewey =~ /^(\d)\d\d\./ or die "bad dewey $dewey";
my $bin="$1xx";

> At this point the module Tie::RangeHash ("Hashes with 'low,high' ranges
> as keys") comes to mind.  I have never used it, but from the description
> it fits the situation exactly.

I've had bad experiences with that module.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: Wed, 20 Sep 2006 18:28:18 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: array of hashes with a little more...
Message-Id: <ees233.10g.1@news.isolution.nl>

Jim Gibson schreef:
> usenet:

>>    my @deweys=qw{000-099 100-199 200-299 300-399 400-499 500-599};
>
> There is redundant information in this array. I would just store the
> starting values and use the appropriate comparison operators and
> succesive elements in the array to define a range:
>
>    my @deweys = qw{ 000 100 200 300 400 500 };

    my @deweys = 0..5 ;
;)

-- 
Affijn, Ruud

"Gewoon is een tijger."




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

Date: 20 Sep 2006 13:06:19 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Coderef to object methods?
Message-Id: <4ncsmbF9pfngU1@news.dfncis.de>

 <xhoster@gmail.com> wrote in comp.lang.perl.misc:
> "robb@acm.org" <robb@acm.org> wrote:
> > xhos...@gmail.com wrote:

> You can use an ordinary string with the name of the method,
> or the UNIVERSAL::can method, or just just take a reference to
> fully qualified sub.

A nice list.

The alternatives differ in when (or if) method lookup through classes
takes place.

> perl -wle 'package foo; sub bar {print "asfdadsf"}; package main; \
>            $bar="bar";foo->$bar();'

Here method lookup happens on every call, just like in "foo->bar()".

> perl -wle 'package foo; sub bar {print "asfdadsf"}; package main; \
>            $bar=foo->can("bar");foo->$bar();'

Here ->can does a method lookup.  The result is frozen into the
coderef $bar.  There is no method lookup at call time.

> perl -wle 'package foo; sub bar {print "asfdadsf"}; package main; \
>            $bar=\&foo::bar;foo->$bar();'

No method lookup happens at all.  $bar becomes a coderef to the sub
bar() in package "foo", no two ways.

Anno


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

Date: 20 Sep 2006 08:04:18 -0700
From: "botfood" <botfood@yahoo.com>
Subject: explaining how memory works with tie()ed hashs
Message-Id: <1158764658.604864.297510@i42g2000cwa.googlegroups.com>

I would like to know more about how perl uses memory when managing a
hash that is tie()ed to a file on disk. Using DB_File and variable
length records....

I have an application where the DB file gotten quite big, not giant,
but around 20k records and is a file about 11MB in size. My
understanding of DB_FIle is that while there is no programtic limit on
the number of records, there may be memory-driven limits depending what
one does with the hash. Looping thru the keys is still fast and so the
number of records doesnt seem to be a problem.

In this particular case, each record is not that big, except for one
specific type of 'book-keeping' record that is used to keep track of
what records are considered 'complete' by this particular application.
With a couple reports, I need to whip thru all complete records
searching for various things.... And in another spot I know that the
code looks for matches within this big record that contains around 20k
'words' consisting of about 20 digits.

what I am wondering is whether it is likely that the simple number of
records eats up large amounts of memory just by being tie()ed, or if it
is more likely that this one particular internal index record is
causing me problems when it gets pulled into memory to do things like
m// or s// on its contents to find or edit a 'word' which is simply a
list of the keys having a specific status.

The next part of the question is.... if it sounds like a large internal
indexing record is likely to be a problem, what would some recommended
techniques be to break that out? should I create a separate DB file to
use as an index?  I am really wondering how best to 'fake' large
database capabilities to manage keeping track of status without eating
tons of memory.

TIA,
d



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

Date: 20 Sep 2006 09:54:01 -0700
From: "Deepu" <pradeep.bg@gmail.com>
Subject: Help with plotting
Message-Id: <1158771241.010243.65760@m7g2000cwm.googlegroups.com>

Hi All,

I am trying to generate a bar chart after creating a table out of the
information in a file.

I have file which contains so many other details and have a line in the
file which start with:

#RESULT: 30 => YES: 10 NO: 10 UNKNOWN: 10

I have several files with each file have a line with RESULT.

The code to store the number of YES, NO and UNKNOWN:

#!/usr/bin/perl

@dirList= qw(FILE1 FILE2 FILE3 FILE4 FILE5);

foreach $dir (@dirList) {
   open (PH, "$dir") || die "Can not open:$dir";

   while (<PH>) {
      if (/#RESULT: (\d+) => YES: (\d+) NO: (\d+) UNKNOWN: (\d+)/) {
         push (@yesArray, $2);
         push (@noArray, $3);
         push (@unknownArray, $4);
      }
   }

close (PH);
}

Now i am trying to display it in a table format:

              YES        NO       UNKNOWN
FILE1       10           8               14
FILE2        6            7               20
FILE3        18          10             10
FILE4        20          12             10
FILE5        10          10             10

After printing this out how to create abar chart out of it in perl with
X axis showing FILE number and has 3 bars for each file.

Thanks for the help.



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

Date: Wed, 20 Sep 2006 11:27:21 -0400
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: How do I find the Nth index of array that is (whatever)
Message-Id: <g69hcz2k17q.fsf@CN1374059D0130.kendall.corp.akamai.com>

On 20 Sep 2006, usenet@davidfilmer.com wrote:

> Suppose I have an array like this:
>
> my @array = ('foo', '', 'bar', '', '', '', 'baz', '');
>
> and I want to find the index value of the third undefined element.

This uses an xor, then adds up the value at $offset until the sum
reaches 3.  It can probably be improved and shortened.

Ted

#!/usr/bin/perl

use warnings;
use strict;
use Data::Dumper;

my @array = ('foo', '', 'bar', '', '', '', 'baz', '');
my @barray = map { $_ xor 1 } @array;

my $offset = 0;
my $sum = 0;

foreach (@barray)
{
 $sum += $_;
 if ($sum >=3)
 {
  print "Found it, offset is $offset\n";
  last;
 }
 $offset++;
}


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

Date: Wed, 20 Sep 2006 16:05:14 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: How do I find the Nth index of array that is (whatever)
Message-Id: <au18u3-drr.ln1@osiris.mauzo.dyndns.org>


Quoth usenet@DavidFilmer.com:
> Suppose I have an array like this:
> 
>    my @array = ('foo', '', 'bar', '', '', '', 'baz', '');
> 
> and I want to find the index value of the third undefined element.  I
> can do something really ugly like this:
> 
>    my $search_for = 3; #find index of third undefined element
> 
>    my $undefined = 0;
>    foreach my $index(0..$#array) {
>       $undefined++ unless $array[$index];
>       if ($undefined == $search_for) {
>          print "Index of undefined element #$search_for is $index\n";
>          next;
>       }
>    }
> 
> but I HATE THAT CODE.  It's ugly. It's inelegant.  It's an offense to
> human dignity.  It increases global warming.  Is there a better (and
> more environmentally safe) technique?

use List::MoreUtils qw/firstidx/;

my @array = (foo => '', bar => '', '', '', baz => '');
my $search_for = 3;

local ($\, $,) = ("\n", ' ');
my $n = 0;
print "Index of false element #$search_for is",
    firstidx { !$_ && ++$n == $search_for }
    @array;

Ben

P.S. #$search_for really threw me for a second... I don't think there's
anything to be done about that, though. A highlighting editor would of
course have helped :).

-- 
It will be seen that the Erwhonians are a meek and long-suffering people,
easily led by the nose, and quick to offer up common sense at the shrine of
logic, when a philosopher convinces them that their institutions are not based 
on the strictest morality. [Samuel Butler, paraphrased] benmorrow@tiscali.co.uk


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

Date: Wed, 20 Sep 2006 15:29:47 GMT
From: "PG" <noreply@hotmail.com>
Subject: inserting lines
Message-Id: <L7dQg.38696$_k2.697116@news2.nokia.com>

I want to know how to add a line , say "foobar" , after a line which says,
for example "string1", in a inout file.

how can I do this ?

(I'm pretty new to Perl)




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

Date: Wed, 20 Sep 2006 16:37:12 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: inserting lines
Message-Id: <eern78$840$1@gemini.csx.cam.ac.uk>

PG wrote:
> I want to know how to add a line , say "foobar" , after a line which says,
> for example "string1", in a inout file.
> 
> how can I do this ?

What have you tried so far?

What is an "inout" file?


DS


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

Date: 20 Sep 2006 09:22:59 -0700
From: as4109@wayne.edu
Subject: Re: inserting lines
Message-Id: <1158769379.872508.155110@i42g2000cwa.googlegroups.com>


PG ha escrito:

> I want to know how to add a line , say "foobar" , after a line which says,
> for example "string1", in a inout file.
>
> how can I do this ?
>
> (I'm pretty new to Perl)

perl -pe 'if (/string1/) { chomp; s/$/foobar\n/ }'

The above,  of course,  is the same as the following script:

#!/usr/bin/perl

while (<>) {
  if (/string1/) { chomp; s/$/foobar\n/ };
  print
}



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

Date: Wed, 20 Sep 2006 17:31:52 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: inserting lines
Message-Id: <eerqdp$ejb$1@gemini.csx.cam.ac.uk>

as4109@wayne.edu wrote:
> PG ha escrito:
> 
>> I want to know how to add a line , say "foobar" , after a line which says,
>> for example "string1", in a inout file.
>>
>> how can I do this ?
>>
>> (I'm pretty new to Perl)
> 
> perl -pe 'if (/string1/) { chomp; s/$/foobar\n/ }'
> 

 ... but this does not solve the OP's problem, which does not mention
substitution, but rather an additional line.

Still waiting to here what the OP has tried...


DS


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

Date: Wed, 20 Sep 2006 09:34:30 -0700
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: inserting lines
Message-Id: <200920060934307869%jgibson@mail.arc.nasa.gov>

In article <L7dQg.38696$_k2.697116@news2.nokia.com>, PG
<noreply@hotmail.com> wrote:

> I want to know how to add a line , say "foobar" , after a line which says,
> for example "string1", in a inout file.
> 
> how can I do this ?
> 
> (I'm pretty new to Perl)

This is a "Frequently Asked Question". Unfortunately, the FAQ for Perl
only has one recommendation: using the Tie::File module. If you are new
to Perl, learning about the "tie" concept may confuse you, as may using
modules, although you will definitely benefit from learning how to use
modules.

The old-fashioned way is to:

1. Open the old file for reading (perldoc -f open).
2. Open a new file for writing.
3. Read the old file a line-at-a-time, looking for "string1" and
writing out each line to the new file (perldoc -f print).
4. If "string1" is found, write out the line "foobar" (after you copy
the string1 line to the new file.
5. When the old file has been completely read and copied, close both
files.
6. Either within your program or via the operating system after the
program has quit, rename the old file to the new file, after optionally
renaming the old file as a backup (perldoc -f rename).

You will learn a lot of Perl just doing this. It can also be done as a
one-liner using Perl's facilities (see 'perldoc perlrun' for the useful
options).


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

Date: Wed, 20 Sep 2006 12:44:57 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: inserting lines
Message-Id: <x7zmcujxme.fsf@mail.sysarch.com>

>>>>> "JG" == Jim Gibson <jgibson@mail.arc.nasa.gov> writes:

  JG> The old-fashioned way is to:

  JG> 1. Open the old file for reading (perldoc -f open).
  JG> 2. Open a new file for writing.
  JG> 3. Read the old file a line-at-a-time, looking for "string1" and
  JG> writing out each line to the new file (perldoc -f print).
  JG> 4. If "string1" is found, write out the line "foobar" (after you copy
  JG> the string1 line to the new file.
  JG> 5. When the old file has been completely read and copied, close both
  JG> files.
  JG> 6. Either within your program or via the operating system after the
  JG> program has quit, rename the old file to the new file, after optionally
  JG> renaming the old file as a backup (perldoc -f rename).

the new fashioned way to do this is with file::slurp and 1 s///g
call. in the near future (i keep promising this) there will be edit_file()
and edit_file_lines() subs added to that module that will do this in one
call.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: 20 Sep 2006 16:45:18 GMT
From: xhoster@gmail.com
Subject: Re: inserting lines
Message-Id: <20060920124644.860$1H@newsreader.com>

David Squire <David.Squire@no.spam.from.here.au> wrote:
> as4109@wayne.edu wrote:
> > PG ha escrito:
> >
> >> I want to know how to add a line , say "foobar" , after a line which
> >> says, for example "string1", in a inout file.
> >>
> >> how can I do this ?
> >>
> >> (I'm pretty new to Perl)
> >
> > perl -pe 'if (/string1/) { chomp; s/$/foobar\n/ }'
> >
>
> ... but this does not solve the OP's problem, which does not mention
> substitution, but rather an additional line.

How is substituting an empty string with a line not the same
thing as inserting a line?  (although he is missing a newline, he
should take out the chomp and move \n to the beginning of the
replacement.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: Wed, 20 Sep 2006 18:02:30 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: inserting lines
Message-Id: <eers77$i7m$1@gemini.csx.cam.ac.uk>

xhoster@gmail.com wrote:
> David Squire <David.Squire@no.spam.from.here.au> wrote:
>> as4109@wayne.edu wrote:
>>> PG ha escrito:
>>>
>>>> I want to know how to add a line , say "foobar" , after a line which
>>>> says, for example "string1", in a inout file.
>>>>
>>>> how can I do this ?
>>>>
>>>> (I'm pretty new to Perl)
>>> perl -pe 'if (/string1/) { chomp; s/$/foobar\n/ }'
>>>
>> ... but this does not solve the OP's problem, which does not mention
>> substitution, but rather an additional line.
> 
> How is substituting an empty string with a line not the same
> thing as inserting a line?  (although he is missing a newline, he
> should take out the chomp and move \n to the beginning of the
> replacement.
>

Ah. I misread that. Sorry.


DS


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

Date: Wed, 20 Sep 2006 17:16:48 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: inserting lines
Message-Id: <4IeQg.30576$cz3.11735@edtnps82>

PG wrote:
> I want to know how to add a line , say "foobar" , after a line which says,
> for example "string1", in a inout file.
> 
> how can I do this ?

perl -i -pe'$_ .= "foobar\n" if /string1/' inout_file



John
-- 
use Perl;
program
fulfillment


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

Date: 20 Sep 2006 08:58:34 -0700
From: "--Jani--" <Janin.Netosoite@gmail.com>
Subject: Re: Net::Telnet, script exits after connection failure
Message-Id: <1158767914.796876.20850@i3g2000cwc.googlegroups.com>

Hi!

Here's my code and configfile syntax is 192.168.0.1,username,password:

foreach (@confile) {
        if (/^\#(.*)/) {
        }
        elsif (/^\d(.*)/) {
                ($swip, $swuser, $swpass) = split(/,/);
                chomp ($swip, $swuser, $swpass);
	        use Net::Telnet;
		$cmdString = "switchshow";
	        $brocade = new Net::Telnet (Host => "$swip", Timeout => 10);
                $brocade->dump_log($tracefile);
		$brocade->login(Name => $swuser, Password => $swpass);
		$brocade->cmd($cmdString);
		$brocade->close;
        }
}



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

Date: 20 Sep 2006 06:26:08 -0700
From: "chaitu" <schaitan@gmail.com>
Subject: Parallel::ForkManager not working as intended on Windows Server 2003
Message-Id: <1158758768.201599.133810@k70g2000cwa.googlegroups.com>

Hi folks,

I've created a perl module MakeCVSStructure in which I've coded 2
functions called CheckoutAllSubDirs and CheckoutSubDir respectively.
This module and its functions are intended to help me maintain my CVS
working copies.

In MakeCVSStructure.pm:
--------------------------------------

use Parallel::ForkManager;

 ...bla bla...some more functions go here...

sub CheckoutSubDir
{
  my $self = shift;
  my $sub_dir_name = shift;
  chdir $self->{TOPDIR};
  $ENV{CVSROOT}=<The value goes here...it's confidential...>;
  system("cvs -q checkout -r $self->{TAG} -P $sub_dir_name");
  print "\nCVS Checkout of module $sub_dir_name successful!\n" if $? ==
0;
}

sub CheckoutAllSubDirs
{
  # The subdirs need to be checked out in a parallel fashion!
  # Code goes here...
  my $self = shift;
  my $sub_dir_list = $self->{SUBDIRLIST};
  my $no_of_subdirs = @$sub_dir_list;
  print "\nNo. of subdirectories = $no_of_subdirs\n";

  my $pm = new Parallel::ForkManager();

  foreach my $subdir (@$sub_dir_list)
  {
     print "\nSpawning $subdir now...\n";
     $pm->start and next;

     print "\nInside child process for $subdir now...\n";
     $self->CheckoutSubDir($subdir);
     $pm->finish;

  }
  $pm->wait_all_children;
}

When I create an object of this class in my .pl file and run the .pl
file as follows, it does NOT start parallel checkouts of the CVS
modules, but goes in a serial fashion.....

Code of my .pl :
------------------------

use MakeCVSStructure;

my $sub_dir_list =
["admin","build","host","server","thirdparty","tools"];
my ($drive, $topdirname, $tag) = @ARGV;

my $MyCVSObj = MakeCVSStructure->new();
$MyCVSObj->SetTopDir("${drive}:\\${topdirname}");
$MyCVSObj->SetTag("$tag");
$MyCVSObj->SetSubDirList($sub_dir_list);
$MyCVSObj->GetDetails;
$MyCVSObj->MakeTopDir;
$MyCVSObj->CheckoutAllSubDirs;

Could the gurus please tell me why the checkouts aren't happening in a
parallel fashion as I expected them to?

Thnx a million for the help...



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

Date: 20 Sep 2006 16:26:57 GMT
From: xhoster@gmail.com
Subject: Re: Parallel::ForkManager not working as intended on Windows Server 2003
Message-Id: <20060920122823.871$tV@newsreader.com>

"chaitu" <schaitan@gmail.com> wrote:
> Hi folks,
>
> I've created a perl module MakeCVSStructure in which I've coded 2
> functions called CheckoutAllSubDirs and CheckoutSubDir respectively.
> This module and its functions are intended to help me maintain my CVS
> working copies.


>   my $pm = new Parallel::ForkManager();
                                      ^^

What do you think this accomplishes?


Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: 20 Sep 2006 08:30:17 -0700
From: mmittiga17@gmail.com
Subject: Parse CSV file
Message-Id: <1158766217.547100.136560@i42g2000cwa.googlegroups.com>

Need help folks.

I have a csv file that has some fields containing "," as part of the
field and not a delimiter.  These fields are double quoted.

What I need to do is read each line, if the value in field 22 = X dont
print
$line.  How can I get the value of each line's field 22, when split
does not work on csv files that have quoted fields?

example:  "Potsdam, NY"

This one is driving me crazy.

I have only gotten about this far:

use Text::ParseWords;

  while (@ARGV) {
     $file=shift (@ARGV);
     open (IN,"$file") unless ($file =~ /\.Z/);
        while (defined($line = <IN>)) {
           @fields =quotewords(',', 0, $line);
           print "@fields[22]\n";

        }
 }
 I have also tried the regex for csv files from Mastering Regex.

 One thought was use a hash...

 Any help or suggestions would be appreciated.

 Thanks



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

Date: Wed, 20 Sep 2006 16:39:35 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: Parse CSV file
Message-Id: <eernbn$840$2@gemini.csx.cam.ac.uk>

mmittiga17@gmail.com wrote:
> Need help folks.
> 
> I have a csv file that has some fields containing "," as part of the
> field and not a delimiter.  These fields are double quoted.
> 
> What I need to do is read each line, if the value in field 22 = X dont
> print
> $line.  How can I get the value of each line's field 22, when split
> does not work on csv files that have quoted fields?
> 
> example:  "Potsdam, NY"
> 
> This one is driving me crazy.
> 
> I have only gotten about this far:
> 
> use Text::ParseWords;
> 

Use the right module for the job. Go to CPAN and search for CSV... or
even "CSV simple" :)


DS


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

Date: 20 Sep 2006 08:51:30 -0700
From: "Xicheng Jia" <xicheng@gmail.com>
Subject: Re: Parse CSV file
Message-Id: <1158767490.070125.281610@i42g2000cwa.googlegroups.com>

mmittiga17@gmail.com wrote:
> Need help folks.
>
> I have a csv file that has some fields containing "," as part of the
> field and not a delimiter.  These fields are double quoted.
>
> What I need to do is read each line, if the value in field 22 = X dont
> print
> $line.  How can I get the value of each line's field 22, when split
> does not work on csv files that have quoted fields?
>
> example:  "Potsdam, NY"
>
> This one is driving me crazy.
>
> I have only gotten about this far:
>
> use Text::ParseWords;
>
>   while (@ARGV) {
>      $file=shift (@ARGV);
>      open (IN,"$file") unless ($file =~ /\.Z/);
>         while (defined($line = <IN>)) {
>            @fields =quotewords(',', 0, $line);

how about(assumed no escaped double-quotes within a field):

              @fields = ($line =~ /"[^"]*"|[^",]+/g);
              $fields[21] =~ tr/"//d;
              print "$fields[21]\n";

Xicheng



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

Date: 20 Sep 2006 08:51:49 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Parse CSV file
Message-Id: <1158767509.649796.103510@m73g2000cwd.googlegroups.com>

mmittiga17@gmail.com wrote:
> Need help folks.
>
> I have a csv file that has some fields containing "," as part of the
> field and not a delimiter.  These fields are double quoted.

What was wrong with the answer I gave you in perl.beginners?

http://groups.google.com/group/perl.beginners/browse_frm/thread/55062e9570c1d596/#

Paul Lalli



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

Date: 20 Sep 2006 07:09:38 -0700
From: "deadpickle" <deadpickle@gmail.com>
Subject: Re: Server/client over a network
Message-Id: <1158761378.488683.270180@i42g2000cwa.googlegroups.com>

S=E9bastien Cottalorda wrote:
> deadpickle a =E9crit :
> > what I want to do is have a server on one machine and a client on the
> > other. I want the client to connect to the server through a tcp network
> > (over the internet). I have tried this and I constantly get the no
> > socket error.
> Hi,
>
> On which side : client or server ?

I believe I get it on the server side if I dont assign the value
'localhost' to LocalHost in IO::SOCKET::INET. And I get it on the
client side whenever I connect to an ip address.

> If it's on the client side, you cannot manage to reach the server socket
> through the router.
> Try this:
> #> telnet router 7890
> that will try open a telnet session on the router port 7890
> If your router forward this port, your request will go to the server on
> which you want to pick the file.

tried it in terminal and got:
Connecting to router...Could not open a connection to the host, on port
7890: Connect failed
So I guess it did not forward anything.

> Just enter FIL|namefile
> Normaly you'll see something

Not sure what this is. Is this a line in the script?

> NOTE: your client socket need to be open with the *router* and not with
> the server (port forwarding case).
> If you mean routing instead of port forwarding, then the client needs to
> point to the *server* adress.

 I mean port forwarding since it is easy to do. how can I open a port
with the "router"?



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

Date: 20 Sep 2006 08:08:32 -0700
From: "samasama" <bryan@worldspice.net>
Subject: Trying to match keys of hash with DBI fetchrow_hashref
Message-Id: <1158764912.153275.111140@m73g2000cwd.googlegroups.com>

 Hi, noob here...
I'm trying to read a row containing usernames and see if those
usernames are in an already created hash...

------- Code -------

my %lusers;

sysopen( LIST0, "list.txt", O_RDONLY );
#|| die "Can't open original list!";

while (<LIST0>) {
   my @fields = split(/\s{2}/, $_);
   $lusers{$fields[0]} = $fields[1];
   }


 my $dbh = open_db('ro') || die "Could not open db: $!\n";
  my $sql = qq{ SELECT username
                FROM dialup
                };

    my $sth0 = $dbh->prepare($sql) || die "DBH Preparing: ",
$dbh->errstr;

    $sth0->execute;

    while ( my $row = $sth0->fetchrow_hashref ) {
    foreach my $keys (keys %lusers) {
    if ($row->{'username'} ne $keys) {
    print "$keys not in db\n";
        }
     }
last;
 }

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

I know I'm going wrong on my if statement there, but I'm not sure what
I should be comparing exactly...

 Any help is vastly appreciated.

Thanks

--
samasama



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

Date: 20 Sep 2006 08:33:19 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Trying to match keys of hash with DBI fetchrow_hashref
Message-Id: <1158766399.522738.157150@i42g2000cwa.googlegroups.com>

samasama wrote:

> I'm trying to read a row containing usernames and see if those
> usernames are in an already created hash...
>
> while ( my $row = $sth0->fetchrow_hashref ) {
>   foreach my $keys (keys %lusers) {
>     if ($row->{'username'} ne $keys) {
>       print "$keys not in db\n";
>     }
>   }
>   last;
>  }
>

> I know I'm going wrong on my if statement there, but I'm not sure what
> I should be comparing exactly...
>

Your structure says to loop through all the keys of the existing hash,
and compare $row->{username}  to each one of them.  If ANY of them
don't match, you print out the error message.  Obviously, at most one
should match.  You should be using the exists function, not looping
through the keys.

while (my $row = $sth0->fetchrow_hashref) {
   if (!exists $lusers{ $row->{username} } ) {
      print "$row->{username} not in existing hash\n";
   }
}

Paul Lalli



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

Date: 20 Sep 2006 08:39:16 -0700
From: "samasama" <bryan@worldspice.net>
Subject: Re: Trying to match keys of hash with DBI fetchrow_hashref
Message-Id: <1158766755.900416.90110@h48g2000cwc.googlegroups.com>


> Your structure says to loop through all the keys of the existing hash,
> and compare $row->{username}  to each one of them.  If ANY of them
> don't match, you print out the error message.  Obviously, at most one
> should match.  You should be using the exists function, not looping
> through the keys.
>
> while (my $row = $sth0->fetchrow_hashref) {
>    if (!exists $lusers{ $row->{username} } ) {
>       print "$row->{username} not in existing hash\n";
>    }
> }
>
> Paul Lalli

 errr, I explained myself wrong... I need to check whether the key
(username) in the hash exists in the database, if not do something with
that username.



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

Date: 20 Sep 2006 08:50:48 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Trying to match keys of hash with DBI fetchrow_hashref
Message-Id: <1158767448.547579.325700@m7g2000cwm.googlegroups.com>

samasama wrote:
> > Your structure says to loop through all the keys of the existing hash,
> > and compare $row->{username}  to each one of them.  If ANY of them
> > don't match, you print out the error message.  Obviously, at most one
> > should match.  You should be using the exists function, not looping
> > through the keys.
> >
> > while (my $row = $sth0->fetchrow_hashref) {
> >    if (!exists $lusers{ $row->{username} } ) {
> >       print "$row->{username} not in existing hash\n";
> >    }
> > }

>
>  errr, I explained myself wrong... I need to check whether the key
> (username) in the hash exists in the database, if not do something with
> that username.

Then I'd say you're going backwards...

my $sql = "SELECT 1 FROM dialup WHERE username = ?";
my $sth = $dbh->prepare($sql);
for my $username (keys %lusers) {
   $sth->execute($username);
   my ($found) = $sth->fetchrow_array();
   print "$username not found in DB\n" unless $found;
}

If for some reason you really wanted to fetch them all and then
check...
my $sql = "SELECT username FROM dialup";
my $sth = $dbh->prepare($sql);
$sth->execute();
my %db_users
while (my ($username) = $sth->fetchrow_array() ) {
   $db_users{$username} = 1;
}
for my $username (keys %lusers) {
   print "$username not found in DB" unless exists
$db_users{$username};
}

Paul Lalli



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

Date: 20 Sep 2006 08:56:01 -0700
From: "samasama" <bryan@worldspice.net>
Subject: Re: Trying to match keys of hash with DBI fetchrow_hashref
Message-Id: <1158767761.467701.34360@m7g2000cwm.googlegroups.com>


>
> Then I'd say you're going backwards...
>
> my $sql = "SELECT 1 FROM dialup WHERE username = ?";
> my $sth = $dbh->prepare($sql);
> for my $username (keys %lusers) {
>    $sth->execute($username);
>    my ($found) = $sth->fetchrow_array();
>    print "$username not found in DB\n" unless $found;
> }
>

Wow, yeah, that's a lot simpler (and efficient). I'll have to stare at
it a bit...

 Thanks so much Paul!

--
Bryan Paxton



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

Date: Wed, 20 Sep 2006 15:02:20 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Windows 2000/XP: Owner of a File
Message-Id: <eerl88.1k4.1@news.isolution.nl>

Aqua schreef:

> In Windows 2000/XP how do I find an owner of a file which is present
> in a network drive.
>
> G:\sample\file\myfile.txt
>
> Domain: testdomain


If CPAN has nothing, look into:
http://www.sysinternals.com/Utilities/ShareEnum.html
http://www.sysinternals.com/Utilities/AccessEnum.html

-- 
Affijn, Ruud

"Gewoon is een tijger."




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

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.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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 9744
***************************************


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