[28373] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 9737 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Sep 18 18:05:47 2006

Date: Mon, 18 Sep 2006 15:05:05 -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           Mon, 18 Sep 2006     Volume: 10 Number: 9737

Today's topics:
    Re: arrays and hashes <benmorrow@tiscali.co.uk>
    Re: Can't locate oject methoid Cells via package Ssprea <jgibson@mail.arc.nasa.gov>
    Re: Column extraction in perl <tzz@lifelogs.com>
    Re: Column extraction in perl <tzz@lifelogs.com>
        database connect: protect with exceptions? <filippo2991@virgilio.it>
    Re: database connect: protect with exceptions? <tadmc@augustmail.com>
    Re: Learning perl - for experienced programmers <filippo2991@virgilio.it>
        perl C extension gmlvsk2@gmail.com
    Re: perl C extension anno4000@radom.zrz.tu-berlin.de
    Re: perl C extension gmlvsk2@gmail.com
    Re: perl C extension xhoster@gmail.com
        Read from xml to 2D array <r.aravind@gmail.com>
    Re: Read from xml to 2D array <mritty@gmail.com>
    Re: Read from xml to 2D array <mritty@gmail.com>
    Re: Read from xml to 2D array <tzz@lifelogs.com>
    Re: Read from xml to 2D array <r.aravind@gmail.com>
    Re: Sum the middle column for a given unique volser. <Noatec@gmail.com>
    Re: Sum the middle column for a given unique volser. <tadmc@augustmail.com>
    Re: Sum the middle column for a given unique volser. <mritty@gmail.com>
        Use of uninitialized value in print <pamelapdh@aol.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 18 Sep 2006 12:12:05 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: arrays and hashes
Message-Id: <5hb2u3-boq.ln1@osiris.mauzo.dyndns.org>


Quoth David Squire <David.Squire@no.spam.from.here.au>:
> anno4000@radom.zrz.tu-berlin.de wrote:
> > Paul Lalli <mritty@gmail.com> wrote in comp.lang.perl.misc:
> >> Abigail wrote:
> >>> David Squire (David.Squire@no.spam.from.here.au) wrote on MMMMDCCLXIII
> >>> September MCMXCIII in <URL:news:eeeauv$jr1$1@gemini.csx.cam.ac.uk>:
> >>> ;;
> >>> ;; > What is the difference with ()
> >>> ;;
> >>> ;;  allows you to construct a list
> >>>
> >>> Eh, not really. Context makes lists - the only place where () is significant
> >>> in creating a list is on the left hand side of the x.
> 
> I am happy to be corrected on this, but the usage I was referring to, in 
> the context of the OP's original post, is something like this:
> 
> my @new_array = (1, 2, 3);
> 
> as opposed to, say,
> 
> my $new_array_ref = [1, 2, 3];
> 
> Is my understanding of the usage of (...) in the first instance flawed?

It's not flawed, it's just that it's not the () that create the list.
The comma creates the list; the () simply prevents it from being parsed
as

    (my @new_array = 1), 2, 3;

as = binds tighter than , (in that direction).

Ben

-- 
'Deserve [death]? I daresay he did. Many live that deserve death. And some die
that deserve life. Can you give it to them? Then do not be too eager to deal
out death in judgement. For even the very wise cannot see all ends.'
                                                        benmorrow@tiscali.co.uk


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

Date: Mon, 18 Sep 2006 12:59:11 -0700
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Can't locate oject methoid Cells via package Sspreadsheet::WriteExcel::Worksheet
Message-Id: <180920061259116686%jgibson@mail.arc.nasa.gov>

In article <1158597175.798104.172770@m73g2000cwd.googlegroups.com>, Pam
<pamelapdh@aol.com> wrote:

> jmcnamara@cpan.org wrote:
> > Pam wrote:
> > > I  get
> > > Can't locate boject method Cells via package
> > > Spreadsheet::WriteExcel::Worksheet
> >
> > That is because Cell() isn't a Spreadsheet::WriteExcel method it is a
> > Spreadsheet::ParseExcel method.
> >
> > You are calling the wrong method on the wrong object.
> >
> > John.
> > --
> 
> 
> Yes, I was calling the wrong methond on the wrong object.  I have
> corrected that, I think the confussion because I am trying to write to
> a file as well as read it.  I have corrected that as well, but I am
> still having problems getting the contents of the cell.
> I no longer get the warning about not being able to locate object
> method.  I fixed that part.

To increase the chances of getting help from the people who read this
newsgroup, you should shorten your program to the smallest example that
still shows your problem. You are not having any problems reading and
parsing a CSV file, so leave that part out. Also, please do not start a
new thread with a similar subject, but continue to respond in the old
thread.

[many irrelevant lines snipped]

> 
> #Tring to check for empty cell
> #This looks at Sheet1 in the workbook
> my $Sheet = $Book->Worksheets(1);
> 
> 
>         for(my $row =1 ;
>                  $row <= $total ; $row++) {
> 
> 
> 
> printf "At ($row, $col) the value is %s and the formula is %s\n",
>                        $Sheet->Cells($row,$col)->{'Value'},
>                        $Sheet->Cells($row,$col)->{'Formula'};

There are two methods for Worksheet:

   Cells[$row][$col]
   Cell($row,$col)

You seem to be confusing them.

-- 
Jim Gibson

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com


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

Date: Mon, 18 Sep 2006 16:38:04 -0400
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Column extraction in perl
Message-Id: <g69fyeorjv7.fsf@CN1374059D0130.kendall.corp.akamai.com>

On 15 Sep 2006, someone@example.com wrote:

vkrish7@gmail.com wrote: >
>> I have about 100 text files and each text file looks something like
>> this:-
>>
>> V_or_I   Lo_BIT
>> 5330        1
>> 5380        5
>> 5390        6
>> 5400        9
>> 5410       11
>> 5420       15
>>
>> Now  i need to grab only the first column starting with (5330, skip
>> "V_or_I" line) go till the end of the file(Say till 5420) and write
>> this to another file(say dataappend.txt). Then open another text file(
>> out of 100 text files) do the same( ie. start from second line of the
>> file which will be in number format similar to 5330 and grab the first
>> column and append to dataappend.txt... Can any1 please show me how to
>> do this on perl?
>
>
> perl -lne'/^\s*(\d+)\s/ && print $1' *.txt > dataappend.txt

This assumes numerics.  The OP didn't specify that, and there's no
need to assume it to get the job done.

Ted


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

Date: Mon, 18 Sep 2006 16:42:16 -0400
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Column extraction in perl
Message-Id: <g698xkgrjo7.fsf@CN1374059D0130.kendall.corp.akamai.com>

On 18 Sep 2006, tzz@lifelogs.com wrote:

On 15 Sep 2006, someone@example.com wrote:
>
> vkrish7@gmail.com wrote: >
>>> I have about 100 text files and each text file looks something like
>>> this:-
>>>
>>> V_or_I   Lo_BIT
>>> 5330        1
>>> 5380        5
>>> 5390        6
>>> 5400        9
>>> 5410       11
>>> 5420       15
>>>
>>> Now i need to grab only the first column starting with (5330, skip
>>> "V_or_I" line) go till the end of the file(Say till 5420) and write
>>> this to another file(say dataappend.txt). Then open another text file(
>>> out of 100 text files) do the same( ie. start from second line of the
>>> file which will be in number format similar to 5330 and grab the first
>>> column and append to dataappend.txt... Can any1 please show me how to
>>> do this on perl?
>>
>>
>> perl -lne'/^\s*(\d+)\s/ && print $1' *.txt > dataappend.txt
>
> This assumes numerics.  The OP didn't specify that, 

Sorry.  I thought I had read the OP's post carefully but I didn't,
missing the "number format" part.  Never mind.

> and there's no need to assume it to get the job done.

Ted


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

Date: 18 Sep 2006 13:25:03 -0700
From: "filippo" <filippo2991@virgilio.it>
Subject: database connect: protect with exceptions?
Message-Id: <1158611103.376687.143820@e3g2000cwe.googlegroups.com>

Hello,

hereafter is the command I use to connect my database;

my $dbhCampeggio = DBI->connect(
    "DBI:PgPP:dbname=$DATABASE_NAME;host=$DATABASE_SERVER;port=5432",
    "postgres",
    "postgres",
     { RaiseError => 1}
     ) or die ( "Connessione al DATABASE non riuscita: $DBI::errstr");

$DATABASE_SERVER is passed from command line. I want my program to ask
interactively the user to write the name,  If the user forget to pass
the database server at command line, and the program ask the user
whenever she doesn't write a valid database server.

How can I do?

Thanks
Filippo



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

Date: Mon, 18 Sep 2006 17:03:55 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: database connect: protect with exceptions?
Message-Id: <slrnegu5ub.27m.tadmc@magna.augustmail.com>

filippo <filippo2991@virgilio.it> wrote:

> $DATABASE_SERVER is passed from command line. I want my program to ask
> interactively the user to write the name,  If the user forget to pass
> the database server at command line,


   my $DATABASE_SERVER = shift @ARGV;
   unless ( defined $DATABASE_SERVER ) {
      print STDOUT 'enter the database server name: ';
      $DATABASE_SERVER = <STDIN>;
      chomp $DATABASE_SERVER;
   }


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: 18 Sep 2006 13:42:49 -0700
From: "davide" <filippo2991@virgilio.it>
Subject: Re: Learning perl - for experienced programmers
Message-Id: <1158612169.485876.204180@m73g2000cwd.googlegroups.com>


cartercc@gmail.com ha scritto:

> In my job, I use ColdFusion, Perl, and Java. Each language has its good
> uses and bad uses. Perl is very good for one off scripts of less than
> 25 - 50 lines. In fact, it's very, very good for that. If you're
> dealing with an application of 100 lines or more that will stick around
(snip)

this isn't my experience, at all. I am not a professional programmer,
just enthusiast about languages. I coded a commercial software, a 30k+
rows perl  application (Tk GUI, not web something). It's a campsite
management application built over postgresql, working on a small
network (10 win32 and 6 linuxes + a database server). I paid attention
to use good programming rules for code reuse and maintainability. It's
perfect! It's very easy for me to open emacs and change  something
after more that one year.

For the records: I'm going to port it to Python+WxPython. I'd like to
learn that language and this is a good chance for me to redesign some
part having fun! :-)

Best regards,

Davide



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

Date: 18 Sep 2006 11:34:12 -0700
From: gmlvsk2@gmail.com
Subject: perl C extension
Message-Id: <1158604452.277226.138630@k70g2000cwa.googlegroups.com>

Simple code that creates new hash and put {'foo'=>5} to it
This works and prints 5, but does not release variable sv, so there is
memory leak
some function
  HV *hv;
  SV *sv = newSViv(5);
  hv = newHV();
  hv_store( hv, "foo", 3, sv, 0 );
  return sv_2mortal( newRV((SV*)hv) );

If I change line 2 to
SV *sv = sv_2mortal( newSViv(5) );
I get undef instead of 5 during print, but the variable is released.

What do I do wrong here, how can mark variable for relese but still be
able to use it from hash

__________________________________________________
Perl to test
use Try;
use Data::Dumper;

$a = Try::foo();
#print $a->{'foo'}, "\n";
print Dumper( $a );



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

Date: 18 Sep 2006 19:41:04 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: perl C extension
Message-Id: <4n8b2gF97u8rU1@news.dfncis.de>

 <gmlvsk2@gmail.com> wrote in comp.lang.perl.misc:
> Simple code that creates new hash and put {'foo'=>5} to it
> This works and prints 5, but does not release variable sv, so there is
> memory leak
> some function
>   HV *hv;
>   SV *sv = newSViv(5);
>   hv = newHV();
>   hv_store( hv, "foo", 3, sv, 0 );
>   return sv_2mortal( newRV((SV*)hv) );
> 
> If I change line 2 to
> SV *sv = sv_2mortal( newSViv(5) );
> I get undef instead of 5 during print, but the variable is released.
> 
> What do I do wrong here, how can mark variable for relese but still be
> able to use it from hash
> 
> __________________________________________________
> Perl to test
> use Try;
> use Data::Dumper;
> 
> $a = Try::foo();
> #print $a->{'foo'}, "\n";
> print Dumper( $a );

Since your code isn't XS code, I suppose you were using Inline to
compile it.

As far as I can see you don't need sv_2mortal() at all in this example.
It is certainly wrong for the scalar you store in the hash.  Since you
are not returning the value on the stack (only a hash that contains it)
the default refcounts are fine.

As for the return value, either Inline or XS auto-mortalizes it, I
believe.  If you do it yourself that amounts to one too many.

The one thing that might want an sv_2mortal() is the hash itself.
It comes with a refcount of 1 which is increased to 2 by taking
the reference later.  After the function returns, the return value
is the only reference to the hash, so the count is one high.  You
 can also compensate by using newRV_noinc() to take the ref.

The following snippet shows the three refcounts involved, that of the
hashref, that of the hash, and that of the scalar 5 the hash contains.
They are all 1 as they should be.

    use Devel::Peek;
    Dump mk_hash();
    exit;

    use Inline C => <<'EOC';

    SV* mk_hash() {
        HV *hv;
        SV *sv = newSViv(5);
        hv = newHV();
        hv_store( hv, "foo", 3, sv, 0 );
        return newRV_noinc((SV*)hv);
    }
    EOC

Anno


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

Date: 18 Sep 2006 12:54:04 -0700
From: gmlvsk2@gmail.com
Subject: Re: perl C extension
Message-Id: <1158609244.667650.4700@b28g2000cwb.googlegroups.com>

Thanks,
If this helps I use SWIG and use this typemap to return result from
function
Does it make any difference?

%module Try
%include "typemaps.i"
%{
%}

%typemap(out) char* {
  HV *hv;
  SV *sv = sv_2mortal(newSViv(5));
  hv = newHV();
  hv_store( hv, "foo", 3, sv, 0 );
  $result = sv_2mortal( newRV((SV*)hv) );
  printf( "%d\n", SvREFCNT(sv) );
  argvi++;
}

char* foo();



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

Date: 18 Sep 2006 20:11:48 GMT
From: xhoster@gmail.com
Subject: Re: perl C extension
Message-Id: <20060918161306.504$rk@newsreader.com>

gmlvsk2@gmail.com wrote:
> Simple code that creates new hash and put {'foo'=>5} to it
> This works and prints 5, but does not release variable sv, so there is
> memory leak

I think the actual problem is that it does not release the hv.  You need
to mortalize hv rather than (or maybe in addition to) mortalizing newRV(hv)

> some function
>   HV *hv;
>   SV *sv = newSViv(5);
>   hv = newHV();
>   hv_store( hv, "foo", 3, sv, 0 );
>   return sv_2mortal( newRV((SV*)hv) );
>
> If I change line 2 to
> SV *sv = sv_2mortal( newSViv(5) );
> I get undef instead of 5 during print, but the variable is released.

Sometimes freeing the wrong thing can eliminate a memory leak in a
different variable, for reasons I don't understand.

Xho

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


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

Date: 18 Sep 2006 12:29:30 -0700
From: "Aravind" <r.aravind@gmail.com>
Subject: Read from xml to 2D array
Message-Id: <1158607770.307513.57560@m73g2000cwd.googlegroups.com>

I have an xml file that has tags such as say:
<BOOK>
  <TITLE>t1</TITLE>
  <AUTHOR>a1</AUTHOR>
  <ISBN>111</ISBN>
</BOOK>
<BOOK>
  <TITLE>t2</TITLE>
  <AUTHOR>a2</AUTHOR>
  <ISBN>222</ISBN>
</BOOK>
 ...
I want to read the data into a 2D array eg: array[0][0] = t1,
array[0][1]=a1, ...

What is the simplest way to achieve this??

I was thinking of this (which works) but if xml file has a lot of sub
tags then its cumbersome:
#!/usr/local/bin/perl -w
use diagnostics;
use Data::Dumper;
open FIN,'test.txt' or die "Cannot open file. $!";
$index = 0;
while(<FIN>)
{
    chomp;
    print "\n Now: '$_'";
    if(m{<TITLE>(.+)</TITLE>}) {$array[$index][0] = $1;}
    if(m{<AUTHOR>(.+)</AUTHOR>}) {$array[$index][1] = $1;}
    if(m{<ISBN>(.+)</ISBN>}) {$array[$index][2] = $1;}
    if(m{</BOOK>}) {$index++;}
}
print Dumper(\@array);
close FIN;



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

Date: 18 Sep 2006 12:56:02 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Read from xml to 2D array
Message-Id: <1158609362.799496.15640@b28g2000cwb.googlegroups.com>

Aravind wrote:
> I have an xml file that has tags such as say:
            ^^^
> <BOOK>
>   <TITLE>t1</TITLE>
>   <AUTHOR>a1</AUTHOR>
>   <ISBN>111</ISBN>
> </BOOK>
> <BOOK>
>   <TITLE>t2</TITLE>
>   <AUTHOR>a2</AUTHOR>
>   <ISBN>222</ISBN>
> </BOOK>
> ...
> I want to read the data into a 2D array eg: array[0][0] = t1,
> array[0][1]=a1, ...
>
> What is the simplest way to achieve this??
              ^^^^^^
This comes so close to being a self-answered question, it's scary
http://search.cpan.org/~grantm/XML-Simple-2.14/lib/XML/Simple.pm

Paul Lalli



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

Date: 18 Sep 2006 13:06:05 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Read from xml to 2D array
Message-Id: <1158609965.092780.297460@m73g2000cwd.googlegroups.com>

Paul Lalli wrote:
> Aravind wrote:
> > I have an xml file that has tags such as say:
>             ^^^
> > <BOOK>
> >   <TITLE>t1</TITLE>
> >   <AUTHOR>a1</AUTHOR>
> >   <ISBN>111</ISBN>
> > </BOOK>
> > <BOOK>
> >   <TITLE>t2</TITLE>
> >   <AUTHOR>a2</AUTHOR>
> >   <ISBN>222</ISBN>
> > </BOOK>
> > ...
> > I want to read the data into a 2D array eg: array[0][0] = t1,
> > array[0][1]=a1, ...
> >
> > What is the simplest way to achieve this??
>               ^^^^^^
> This comes so close to being a self-answered question, it's scary
> http://search.cpan.org/~grantm/XML-Simple-2.14/lib/XML/Simple.pm

For example:
$ perl -MXML::Simple -MData::Dumper -e'
my $in = XMLin(<<EOF);
<ALL>
<BOOK>
  <TITLE>t1</TITLE>
  <AUTHOR>a1</AUTHOR>
  <ISBN>111</ISBN>
</BOOK>
<BOOK>
  <TITLE>t2</TITLE>
  <AUTHOR>a2</AUTHOR>
  <ISBN>222</ISBN>
</BOOK>
</ALL>
EOF
my @a = map { [ @{$_}{qw/TITLE AUTHOR ISBN/} ] }  @{$in->{BOOK}};

print Dumper($in, \@a);
'
$VAR1 = {
          'BOOK' => [
                      {
                        'TITLE' => 't1',
                        'ISBN' => '111',
                        'AUTHOR' => 'a1'
                      },
                      {
                        'TITLE' => 't2',
                        'ISBN' => '222',
                        'AUTHOR' => 'a2'
                      }
                    ]
        };
$VAR2 = [
          [
            't1',
            'a1',
            '111'
          ],
          [
            't2',
            'a2',
            '222'
          ]
        ];


Paul Lalli



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

Date: Mon, 18 Sep 2006 16:27:33 -0400
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Read from xml to 2D array
Message-Id: <g69mz8wrkcq.fsf@CN1374059D0130.kendall.corp.akamai.com>

On 18 Sep 2006, r.aravind@gmail.com wrote:

> I have an xml file that has tags such as say:
> <BOOK>
> <TITLE>t1</TITLE>
> <AUTHOR>a1</AUTHOR>
> <ISBN>111</ISBN>
> </BOOK>
> <BOOK>
> <TITLE>t2</TITLE>
> <AUTHOR>a2</AUTHOR>
> <ISBN>222</ISBN>
> </BOOK>
> ...
> I want to read the data into a 2D array eg: array[0][0] = t1,
> array[0][1]=a1, ...
>
> What is the simplest way to achieve this??

Use XML::Simple.  To get the data into the array, use something like
this.  Note the enclosing <qqq> tags to make this a valid (enough for
parsing) XML document.

If you understand what $data contains (via the first Dumper() call),
the @array population process will make sense.

#!/usr/bin/perl

use warnings;
use strict;
use XML::Simple;
use Data::Dumper;

my $data = XMLin(\*DATA);
print Dumper $data;

# put the title, ISBN, and author into @array as an array reference,
# so @array is a two-dimensional list.

my @array = map { [ $_->{TITLE}, $_->{ISBN}, $_->{AUTHOR} ] } @{$data->{BOOK}};

print Dumper \@array;

__DATA__
<qqq>
<BOOK>
<TITLE>t1</TITLE>
<AUTHOR>a1</AUTHOR>
<ISBN>111</ISBN>
</BOOK>
<BOOK>
<TITLE>t2</TITLE>
<AUTHOR>a2</AUTHOR>
<ISBN>222</ISBN>
</BOOK>
</qqq>


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

Date: 18 Sep 2006 14:39:02 -0700
From: "Aravind" <r.aravind@gmail.com>
Subject: Re: Read from xml to 2D array
Message-Id: <1158615542.579521.159480@d34g2000cwd.googlegroups.com>

Thanks a lot Paul and Ted.
I didnt know about XML::Simple before. It was very helpful.

--Aravind.



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

Date: 18 Sep 2006 13:00:30 -0700
From: "Noatec" <Noatec@gmail.com>
Subject: Re: Sum the middle column for a given unique volser.
Message-Id: <1158609630.668640.42380@b28g2000cwb.googlegroups.com>

Tad McClellan wrote:
> Noatec <Noatec@gmail.com> wrote:
>
>
> [ snip 150 quoted lines. Please learn how to compose a proper followup. ]
>
>
> > HOLY CRACKERS !!!!!!!
> > You really made me angry !!!!
> > I thought about what you said; took some time to cool off and I'm fine
> > now.
>
>
> You really should see the Posting Guidelines that are posted
> here frequently.
>
>
> > Honestly, I'll bet you're probably not the prick you come off to be in
> > this medium.
>
>
> Do you want people to help you or to ignore all of your future posts?
>
> Act accordingly.
>
>
> > I kinda like the tough love thing.
>
>
> Then you will learn a whole lot about Perl programming here.
>
>
> --
>     Tad McClellan                          SGML consulting
>     tadmc@augustmail.com                   Perl programming
>     Fort Worth, Texas

Hello everyone,
First of all thanks for all your help and patients.
I applogize for the rough start.
I took some extra time to review the perldocs and my reference book on
hashes in detail. ( I can hear the cheers from here.)

This is what I came up with.
#!/usr/bin/perl
use strict;
use warnings;

my $thresh = $ARGV[0];
my $filename = 'mlist';
my $sum = 0;
my $line='';
my $reads='';
my $volser='';
my $tmp_vol = '';

# open the file
open my $MLIST, '<', $filename or die "Unable to open $filename: $!\n";

# load the hash
my %hash=();
while (my $line = <$MLIST>) {
 my ($volser, $reads, $date) = split(/,/,$line);
        if ($tmp_vol eq ''){
        $tmp_vol=$volser;
}
 $hash{$volser}{reads}+=$reads;
 $hash{$volser}{date}=$date;
        if ($tmp_vol != $volser) {
        if ($hash{$tmp_vol}{reads} <= $thresh) {
print "$tmp_vol $hash{$tmp_vol}{reads} $date";
}
$tmp_vol=$volser;
}
}

#close the file
close($MLIST);

I added the threshold since I only care about tapes with less than x
reads.
The next step is to add some date logic to calculate whether three
years has past since the last write date.
I'm very interested in your critique of this.
Is there a way to do it better, faster or shorter?



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

Date: Mon, 18 Sep 2006 15:25:48 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Sum the middle column for a given unique volser.
Message-Id: <slrnegu06c.22n.tadmc@magna.augustmail.com>

Noatec <Noatec@gmail.com> wrote:
> Tad McClellan wrote:

>> You really should see the Posting Guidelines that are posted
>> here frequently.


Pleeaaase learn the proper way to compose a followup.

Do not quote an entire article.

Do not quote .sigs.

Interleave your comments following the quoted text that
you are commenting on.

Thank you.


> First of all thanks for all your help and patients.


Some of us are patients, but we don't generally admit it in public.

Many of us have patience.

:-)


>         if ($tmp_vol eq ''){
>         $tmp_vol=$volser;


Whitespace is not a scarce resource, feel free to use as much of it
as you like in order to make your code easier to read and understand.

One good place to put spaces is around operators.

You can replace that if() and its block with:

   $tmp_vol = $volser unless length $tmp_vol;


>         if ($tmp_vol != $volser) {
>         if ($hash{$tmp_vol}{reads} <= $thresh) {
> print "$tmp_vol $hash{$tmp_vol}{reads} $date";
> }
> $tmp_vol=$volser;
> }
> }


Something horrid has happened to your code formatting.

Surely it isn't really formatted like this, is it?

If so, then learn and adopt a sensible coding style. It will help
both you and everybody else who has to look at your code. See:

   perldoc perlstyle


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: 18 Sep 2006 14:01:14 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Sum the middle column for a given unique volser.
Message-Id: <1158613274.201917.96880@i42g2000cwa.googlegroups.com>

Noatec wrote:
> First of all thanks for all your help and patients.
> I applogize for the rough start.
> I took some extra time to review the perldocs and my reference book on
> hashes in detail. ( I can hear the cheers from here.)
>
> This is what I came up with.
> #!/usr/bin/perl
> use strict;
> use warnings;
>

GOOD!!


> my $thresh = $ARGV[0];
> my $filename = 'mlist';
> my $sum = 0;
> my $line='';
> my $reads='';
> my $volser='';
> my $tmp_vol = '';

Do not do this.
For one, do not declare your variables until they are needed.  That
will create them in the smallest scope possible, helping use strict; to
tell you when you've done things wrong, and also preventing the need to
scan your entire source file to find all the times the variable might
have changed.
For two, do not pre-initialize variables to defined values.  Perl has a
default value.  It's called 'undef'.  And when you use a variable
before you meant to, 9 times out of 10, Perl will be able to warn you
about it.  By initializing those variables before you have any reason
to, Perl assumes you know what you're doing and doesn't warn you.

> # open the file
> open my $MLIST, '<', $filename or die "Unable to open $filename: $!\n";

Good!!

>
> # load the hash
> my %hash=();

Again, no need for the initialization.  `my %hash;` will do fine.
Also, consider finding better names for your variables.  The % already
tells you that it's a hash.  Name it something tells you what the
variable contains.

> while (my $line = <$MLIST>) {

Are you sure you want a newline on the end of that line?  I don't know,
maybe you do.  But if not,
chomp $line;

>  my ($volser, $reads, $date) = split(/,/,$line);
>         if ($tmp_vol eq ''){
>         $tmp_vol=$volser;

$tmp_vol = $volser unless defined $tmp_vol;

> }

Please fix your indentation.  I've cleaned it up below, because I can't
parse it otherwise.

>  $hash{$volser}{reads}+=$reads;
>  $hash{$volser}{date}=$date;
>  if ($tmp_vol != $volser) {
>      if ($hash{$tmp_vol}{reads} <= $thresh) {
>          print "$tmp_vol $hash{$tmp_vol}{reads} $date";
>      }
>      $tmp_vol=$volser;
>   }
> }
>
> #close the file
> close($MLIST);
>
> I added the threshold since I only care about tapes with less than x
> reads.
> The next step is to add some date logic to calculate whether three
> years has past since the last write date.
> I'm very interested in your critique of this.
> Is there a way to do it better, faster or shorter?

Other than the comments I've already made, looks decent to me.  What
are $volser and $tmp_vol.  As in, are they strings or integers or
other?  Depending on the data, it might be possible to use arrays
(which are sorted by nature) and eliminate the whole "checking to see
if $x is not the same as the last $x" bit.  

Paul Lalli



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

Date: 18 Sep 2006 11:42:03 -0700
From: "Pam" <pamelapdh@aol.com>
Subject: Use of uninitialized value in print
Message-Id: <1158604922.971505.186270@k70g2000cwa.googlegroups.com>

Hello
I ran into a problem that I have been working on, the warning  use of
unitialized value at print
I have tried everything I know.  I have initialized my variables but I
don't get the contents of the value.  I have looked at perldoc but
can't seem to resolve  the issue of not getting my
value contents.  It compiles through to the end.  I'm just begining to
get more extensive into Perl.  Trying to meet a dealine.  I had to take
a step back and look at my logic since this is my first stab at
Win32::OLE.   I need another pair oif eyes because I am not seeing the
obvious

use Spreadsheet::ParseExcel;
use Spreadsheet::WriteExcel;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
use warnings;

$filename =3D"CCB.txt";


open(FILE,">$filename") || die("Cannot Open File $filename : $!" );
print FILE $query_result->content;
print "File open ";


close (FILE);


#This is the holding variable for date because the requirements want
the date in filename


$datestamp =3D strftime("%Y%m%d",localtime) ;


# Open the Comma Separated Variable file
open (CSVFILE, $filename) or die "$filename: $!";


# Create a new Excel workbook
my $workbook  =3D
Spreadsheet::WriteExcel->new("3GSoftwareCCB_MeetingAgenda$datestamp.xls");

my $worksheet =3D $workbook->add_worksheet();


# get already active Excel application or open new
my $Excel =3D Win32::OLE->GetActiveObject('Excel.Application')
    || Win32::OLE->new('Excel.Application', 'Quit');


# Create a new CSV parsing object
my $csv =3D Text::CSV_XS->new;


# Row and column are zero indexed
my $row =3D 0;


while (<CSVFILE>) {
    if ($csv->parse($_)) {
        my @Fld =3D $csv->fields;


        my $col =3D 0;
        foreach my $token (@Fld) {
            $worksheet->write($row, $col, $token);
            $col++;
        }
        $row++;
       if ($row > 1){
           $count =3D $count + 1;


         $total =3D $count;


        }


    }
    else {
        my $err =3D $csv->error_input;
        print "Text::CSV_XS parse() failed on argument: ", $err, "\n";
    }



}


      print "Adding sheet1\n";

   print "Now will format .xls file\n";


#Holding Variables for mailing list
$Name1 =3D '3gccb...@motorola.com';


# Add a Format
  $format =3D $workbook->add_format();
#must set wrap for CCB comments and Description
  $format->set_text_wrap();
  $format->set_bold();
  $format->set_bg_color('51');
  $format->set_border();
  $format->set_bottom();
  $format->set_top();
  $format->set_left();
  $format->set_right();


# The general syntax is write($row, $col, $token, $format)


# Write some formatted text


      $col =3D 0;
      $row =3D 0;


       $worksheet->write(0, $col, "Identifier", $format,);
       $worksheet->write(0, 1, "Team Comments", $format,);
       $worksheet->write(0, 2, "Description", $format);
       $worksheet->write(0, 3, "Status", $format);
       $worksheet->write(0, 4, "Severity", $format);
       $worksheet->write(0, 5, "Priority", $format);
       $worksheet->write(0, 6, "CCBComments_encl", $format);
       $worksheet->write(0, 7, "Primary-feature-team", $format);
       $worksheet->write(0, 8, "Sub-feature-team", $format);
       $worksheet->write(0, 9, "Project", $format);
       $worksheet->write(0, 10,"Product", $format);
       $worksheet->write(0, 11,"Products-targeted", $format);
       $worksheet->write(0, 12,"Products-targed_del", $format);
       $worksheet->write(0, 13,"Products-targetd_add", $format);
       $worksheet->write(0, 14,"Program", $format);


my $Book =3D $Excel->Workbooks->Open("D:/Profiles/w8143c/My


Documents/Spreadsheet-WriteExcel-2.17/3GSoftwareCCB_MeetingAgenda$datestamp=
=AD.xls");



#$row =3D 1;
$col =3D 11;


print "Is this book geeting seen", $Book, "\n";


#Tring to check for empty cell
#This looks at Sheet1 in the workbook
my $Sheet =3D $Book->Worksheets(1);


        for(my $row =3D1 ;
                 $row <=3D $total ; $row++) {


printf "At ($row, $col) the value is %s and the formula is %s\n",
                       $Sheet->Cells($row,$col)->{'Value'},
                       $Sheet->Cells($row,$col)->{'Formula'};


                       print $row, "\n";


        }


  After this I get warning talking about use of uninitialized value and

it does not print value(which I was hoping to get contents of the cell
but print statement is empty


print $total, "\n";


while ($row <=3D $total)
{
         $worksheet->write($row, $col, "3G_Platform", $format2);


          $row=3D $row + 1;



}


#Here I am writing to the file but I need to check if the row is empty

If I use something like  if ($Sheet->Cells($row,$col)->{'Value'} " ")
it blows up on me.  If I put a string in it complains about it is not
numeric=20

Need your help, I am spinning my wheels here

Thanks,=20
Pamela



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

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


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