[30057] in Perl-Users-Digest
Perl-Users Digest, Issue: 1300 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Feb 23 00:09:47 2008
Date: Fri, 22 Feb 2008 21:09:11 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 22 Feb 2008 Volume: 11 Number: 1300
Today's topics:
Re: BEGIN not safe after errors--compilation aborted <tadmc@seesig.invalid>
Re: glob a directory then sort by timestamp <tony_curtis32@yahoo.com>
Re: glob a directory then sort by timestamp <glennj@ncf.ca>
Re: glob a directory then sort by timestamp <someone@example.com>
Re: I don't understand the Net::Telnet error <ced@blv-sam-01.ca.boeing.com>
Re: I don't understand the Net::Telnet error <cdalten@gmail.com>
Re: I don't understand the Net::Telnet error <ced@blv-sam-01.ca.boeing.com>
Re: Interactive apps with Perl <stoupa@practisoft.cz>
Re: Long line in perl script <stoupa@practisoft.cz>
Re: Long line in perl script <jurgenex@hotmail.com>
Re: Long line in perl script <someone@example.com>
Re: setting %ENV in a module <pgodfrin@gmail.com>
Re: Speeding my script <phynkel@gmail.com>
Re: Split a string in perl <ced@blv-sam-01.ca.boeing.com>
Spreadsheet::ParseExcel - How to get certain Cells <not@home.net>
Re: Spreadsheet::ParseExcel - How to get certain Cells <glex_no-spam@qwest-spam-no.invalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 23 Feb 2008 03:43:55 GMT
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: BEGIN not safe after errors--compilation aborted
Message-Id: <slrnfruphp.8m8.tadmc@tadmc30.sbcglobal.net>
daz9643 <darrenbird@dipinternational.co.uk> wrote:
> syntax error at googleperlcode.cgi line 9, near "sub
^^^^^^
^^^^^^
> This is the actual google code,
I do not believe you.
> #!/usr/bin/perl
> use CGI::Carp qw(fatalsToBrowser);
> ul class="simpleblue"
> sub google_append_color {
> my @color_array = split(/,/, $_[0]);
> return $color_array[$_[1] % @color_array];
> }
There is no line 9 in your 7 line program...
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Fri, 22 Feb 2008 16:04:02 -0500
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: glob a directory then sort by timestamp
Message-Id: <fpndc3$ltf$1@knot.queensu.ca>
jhellma1@gmail.com wrote:
> All,
>
> I am trying to glob a directory, storing the filenames into an array.
> The trick is that I then want to sort that array by the files'
> timestamps. Is there an easy way to do this?
The glob() is going to do its own sort, so that would be wasteful.
Better to opendir(), grep() a readdir() for the required items, closedir()
and then sort those by stat()ing for {modified,changed,accessed} time.
Don't forget to see "perldoc -f stat" about the use of "_" to minimize
file system hits.
hth
t
------------------------------
Date: 22 Feb 2008 21:37:02 GMT
From: Glenn Jackman <glennj@ncf.ca>
Subject: Re: glob a directory then sort by timestamp
Message-Id: <slrnfrug40.sru.glennj@smeagol.ncf.ca>
At 2008-02-22 04:04PM, "Tony Curtis" wrote:
> jhellma1@gmail.com wrote:
> > All,
> >
> > I am trying to glob a directory, storing the filenames into an array.
> > The trick is that I then want to sort that array by the files'
> > timestamps. Is there an easy way to do this?
>
> The glob() is going to do its own sort, so that would be wasteful.
>
> Better to opendir(), grep() a readdir() for the required items, closedir()
> and then sort those by stat()ing for {modified,changed,accessed} time.
>
> Don't forget to see "perldoc -f stat" about the use of "_" to minimize
> file system hits.
And read http://www.perlmonks.org/?node_id=128722 to learn how to speed
up your sorting.
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
------------------------------
Date: Fri, 22 Feb 2008 23:28:36 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: glob a directory then sort by timestamp
Message-Id: <EUIvj.39979$w57.18852@edtnps90>
jhellma1@gmail.com wrote:
> All,
>
> I am trying to glob a directory, storing the filenames into an array.
> The trick is that I then want to sort that array by the files'
> timestamps. Is there an easy way to do this?
>
> TIA!
For example (UNTESTED):
my $dir = 'something';
opendir my $DH, $dir or die "Cannot opendir '$dir' $!";
my @sorted_files =
map $_->[1],
sort { $a->[0] <=> $b->[0] }
map -f "$dir/$_" ? [ ( stat _ )[9], $_ ] : (),
readdir $DH;
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
------------------------------
Date: Fri, 22 Feb 2008 15:40:19 -0800 (PST)
From: "comp.llang.perl.moderated" <ced@blv-sam-01.ca.boeing.com>
Subject: Re: I don't understand the Net::Telnet error
Message-Id: <b38d4a9d-58aa-43a5-818e-91bef4b8be48@n58g2000hsf.googlegroups.com>
On Feb 21, 6:28 pm, grocery_stocker <cdal...@gmail.com> wrote:
> When I run the following on Linux Fedora Core 6
>
> #!/usr/bin/perl
> use warnings;
> use strict;
> use diagnostics;
>
> {
> my ($pty, $ssh, @lines);
> my $host = "arbornet.org";
> my $user = "cd";
> my $password = "testme";
> my $prompt = "/changeme:~> $/";
> ## Start ssh program.
> $pty = &spawn("ssh", "-l", $user, $host); # spawn() defined below
> ## Create a Net::Telnet object to perform I/O on ssh's tty.
> use Net::Telnet;
> $ssh = new Net::Telnet (-fhopen => $pty,
> -prompt => $prompt,
> -telnetmode => 0,
> -cmd_remove_mode => 1);
>
> ## Login to remote host.
> #$ssh->open($host);
> #$ssh->login($user, $pass);
> $ssh->waitfor(-match => '/password: ?$/i', -errmode => "return")
> or die "problem connecting to host: ", $ssh->lastline;
> $ssh->print($password);
> $ssh->waitfor(-match => $ssh->prompt, -errmode => "return")
> or die "login failed: ", $ssh->lastline;
>
> ## Send command, get and print its output.
> @lines = $ssh->cmd("who");
> print @lines;
>
> exit;
>
> } # end main program
>
> sub spawn {
> my(@cmd) = @_;
> my($pid, $pty, $tty, $tty_fd);
>
> ## Create a new pseudo terminal.
> use IO::Pty ();
> $pty = new IO::Pty or die $!;
>
> ## Execute the program in another process.
> unless ($pid = fork) { # child process
> die "problem spawning program: $!\n" unless defined $pid;
>
> ## Disassociate process from existing controlling terminal.
> use POSIX ();
> POSIX::setsid
> or die "setsid failed: $!";
>
> ## Associate process with a new controlling
> terminal.
> $tty = $pty->slave;
> $tty_fd = $tty->fileno;
> close $pty;
>
> ## Make stdio use the new controlling terminal.
> open STDIN, "<&$tty_fd" or die $!;
> open STDOUT, ">&$tty_fd" or die $!;
> open STDERR, ">&STDOUT" or die $!;
> close $tty;
>
> ## Execute requested program.
> exec @cmd
> or die "problem executing $cmd[0]\n";
> } # end child process
> $pty;
>
> } # end sub spawn
>
> I get the following error message......
> [cdalten@localhost ~]$ ./bot2.pl
> Uncaught exception from user code:
> problem connecting to host: Welcome to FreeBSD!
> at ./bot2.pl line 24
> [cdalten@localhost ~]$
>
> The "Welcome to FreeBSD!" is part of the message on the remote server.
> However, netstat shows that I'm conneced.
>
> tcp 0 0 66.81.68.80:53169
> 69.39.89.95:22 ESTABLISHED
>
> What is causing this error message and more to the point. How come the
> "who" command doesn't get executed.
Looks like there was a generic error message:
...waitfor(-match => '/password: ?$/i',
-errmode => "return")
or die "problem connecting to host: "
You can add errmsg() for additional detail:
or die "problem connecting to host: ",
$ssh->errmsg;
If the message is a 'pattern match timeout' for
instance, you may want to set Dump_Log and/or
Input_Log to see what's being sent from the
server.
--
Charles DeRykus
------------------------------
Date: Fri, 22 Feb 2008 17:12:21 -0800 (PST)
From: grocery_stocker <cdalten@gmail.com>
Subject: Re: I don't understand the Net::Telnet error
Message-Id: <f28c71f8-6687-47de-8ccf-cf52afb6554c@s12g2000prg.googlegroups.com>
On Feb 22, 3:40 pm, "comp.llang.perl.moderated" <c...@blv-
sam-01.ca.boeing.com> wrote:
> On Feb 21, 6:28 pm, grocery_stocker <cdal...@gmail.com> wrote:
>
>
>
> > When I run the following on Linux Fedora Core 6
>
> > #!/usr/bin/perl
> > use warnings;
> > use strict;
> > use diagnostics;
>
> > {
> > my ($pty, $ssh, @lines);
> > my $host = "arbornet.org";
> > my $user = "cd";
> > my $password = "testme";
> > my $prompt = "/changeme:~> $/";
> > ## Start ssh program.
> > $pty = &spawn("ssh", "-l", $user, $host); # spawn() defined below
> > ## Create a Net::Telnet object to perform I/O on ssh's tty.
> > use Net::Telnet;
> > $ssh = new Net::Telnet (-fhopen => $pty,
> > -prompt => $prompt,
> > -telnetmode => 0,
> > -cmd_remove_mode => 1);
>
> > ## Login to remote host.
> > #$ssh->open($host);
> > #$ssh->login($user, $pass);
> > $ssh->waitfor(-match => '/password: ?$/i', -errmode => "return")
> > or die "problem connecting to host: ", $ssh->lastline;
> > $ssh->print($password);
> > $ssh->waitfor(-match => $ssh->prompt, -errmode => "return")
> > or die "login failed: ", $ssh->lastline;
>
> > ## Send command, get and print its output.
> > @lines = $ssh->cmd("who");
> > print @lines;
>
> > exit;
>
> > } # end main program
>
> > sub spawn {
> > my(@cmd) = @_;
> > my($pid, $pty, $tty, $tty_fd);
>
> > ## Create a new pseudo terminal.
> > use IO::Pty ();
> > $pty = new IO::Pty or die $!;
>
> > ## Execute the program in another process.
> > unless ($pid = fork) { # child process
> > die "problem spawning program: $!\n" unless defined $pid;
>
> > ## Disassociate process from existing controlling terminal.
> > use POSIX ();
> > POSIX::setsid
> > or die "setsid failed: $!";
>
> > ## Associate process with a new controlling
> > terminal.
> > $tty = $pty->slave;
> > $tty_fd = $tty->fileno;
> > close $pty;
>
> > ## Make stdio use the new controlling terminal.
> > open STDIN, "<&$tty_fd" or die $!;
> > open STDOUT, ">&$tty_fd" or die $!;
> > open STDERR, ">&STDOUT" or die $!;
> > close $tty;
>
> > ## Execute requested program.
> > exec @cmd
> > or die "problem executing $cmd[0]\n";
> > } # end child process
> > $pty;
>
> > } # end sub spawn
>
> > I get the following error message......
> > [cdalten@localhost ~]$ ./bot2.pl
> > Uncaught exception from user code:
> > problem connecting to host: Welcome to FreeBSD!
> > at ./bot2.pl line 24
> > [cdalten@localhost ~]$
>
> > The "Welcome to FreeBSD!" is part of the message on the remote server.
> > However, netstat shows that I'm conneced.
>
> > tcp 0 0 66.81.68.80:53169
> > 69.39.89.95:22 ESTABLISHED
>
> > What is causing this error message and more to the point. How come the
> > "who" command doesn't get executed.
>
> Looks like there was a generic error message:
>
> ...waitfor(-match => '/password: ?$/i',
> -errmode => "return")
> or die "problem connecting to host: "
>
> You can add errmsg() for additional detail:
>
> or die "problem connecting to host: ",
> $ssh->errmsg;
>
> If the message is a 'pattern match timeout' for
> instance, you may want to set Dump_Log and/or
> Input_Log to see what's being sent from the
> server.
>
I added errmsg() and when I ran the code, got the following
[cdalten@localhost ~]$ ./bot2.pl
Uncaught exception from user code:
problem connecting to host: pattern match timed-out at ./
bot2.pl line 24.
at ./bot2.pl line 24
What package does Dump_Log and Input_log belong to?
------------------------------
Date: Fri, 22 Feb 2008 19:28:55 -0800 (PST)
From: "comp.llang.perl.moderated" <ced@blv-sam-01.ca.boeing.com>
Subject: Re: I don't understand the Net::Telnet error
Message-Id: <7a97d563-c9d6-4828-b503-4c3745f21bfc@s13g2000prd.googlegroups.com>
On Feb 22, 5:12 pm, grocery_stocker <cdal...@gmail.com> wrote:
> On Feb 22, 3:40 pm, "comp.llang.perl.moderated" <c...@blv-
>....
> [cdalten@localhost ~]$ ./bot2.pl
> Uncaught exception from user code:
> problem connecting to host: pattern match timed-out at ./
> bot2.pl line 24.
> at ./bot2.pl line 24
>
> What package does Dump_Log and Input_log belong to?
perldoc Net::Telnet (see METHODS):
$obj = new Net::Telnet (
...
[Dump_Log => $filename,]
...
[Input_log => $file,]
--
Charles DeRykus
------------------------------
Date: Sat, 23 Feb 2008 02:16:57 +0100
From: "Petr Vileta" <stoupa@practisoft.cz>
Subject: Re: Interactive apps with Perl
Message-Id: <fpnsdf$2t52$2@ns.felk.cvut.cz>
sunilkjin@gmail.com wrote:
> Folks,
> I am trying to build an interactive test application. I
> would like to generate interactive commands to an existing
> server(ftpd)
> so commands like ftp 192.68.20.1
> ace>login: xxxx
> ace >password : yyyy
> I should be able to mimic human intervention. Is this possible in
> perl.
> I do not want to add delay's . I prefer synching with the server. I
> know this is possible in Expect. Is there any other scripting language
> this is possible in say Tcl/tk/Perl/Python/Ruby? What language do you
> suggest for such application? this feature in addition with strong
> parsing capabilities is what I am looking for.
> Thanks in advance
> Sunil
Perl/Tk could be a good candidate. (comp.lang.perl.tk)
--
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your
mail from another non-spammer site please.)
Please reply to <petr AT practisoft DOT cz>
------------------------------
Date: Sat, 23 Feb 2008 02:13:57 +0100
From: "Petr Vileta" <stoupa@practisoft.cz>
Subject: Re: Long line in perl script
Message-Id: <fpnsdf$2t52$1@ns.felk.cvut.cz>
Subra wrote:
> Hi ,
>
> I have very long line in perl script. I need to break it into
> multiple lines. Some thing like below.
>
> Present Code :-
>
> if ($settings->{$service_name}-
>> {'pin_home_dir'} || $settings->{$service_type}->{'pin_home_dir'}) {
>
> New code should be:
> if ($settings-
>> {$service_name}->{'pin_home_dir'} ||
>
> $settings->{$service_type}->{'pin_home_dir'}) {
>
>
> Is there any special character like in C (\) which I should put at the
> end of the broken line ????
>
You can wrap line at any space in your long line. Example:
if ($settings->{$service_name}->{'pin_home_dir'}
|| $settings->{$service_type}->{'pin_home_dir'}) {
--
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your
mail from another non-spammer site please.)
Please reply to <petr AT practisoft DOT cz>
------------------------------
Date: Sat, 23 Feb 2008 02:12:15 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Long line in perl script
Message-Id: <a10vr3tt1og0rop1fb473mo3oan2m4aqmo@4ax.com>
"Petr Vileta" <stoupa@practisoft.cz> wrote:
>Subra wrote:
>You can wrap line at any space in your long line. Example:
That is true. And even more than that: you can even place a line break
everywhere where you _could_ place a space, because Perl is a free format
language and all white spaces are treated the same.
jue
------------------------------
Date: Sat, 23 Feb 2008 03:42:40 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Long line in perl script
Message-Id: <QCMvj.50131$C61.26467@edtnps89>
Petr Vileta wrote:
>
> Subra wrote:
>>
>> Is there any special character like in C (\) which I should put at the
>> end of the broken line ????
>>
> You can wrap line at any space in your long line. Example:
>
> if ($settings->{$service_name}->{'pin_home_dir'} ||
> $settings->{$service_type}->{'pin_home_dir'}) {
It doesn't even have to be at a space:
$ perl -MData::Dumper -le'$w->{x}->{y}->{z} = "hello"; print Dumper $w'
$VAR1 = {
'x' => {
'y' => {
'z' => 'hello'
}
}
};
$ perl -MData::Dumper -le'
$w
->
{x}
->
{y}
->
{z}
=
"hello";
print Dumper $w'
$VAR1 = {
'x' => {
'y' => {
'z' => 'hello'
}
}
};
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
------------------------------
Date: Fri, 22 Feb 2008 17:41:35 -0800 (PST)
From: pgodfrin <pgodfrin@gmail.com>
Subject: Re: setting %ENV in a module
Message-Id: <6a67bd0d-a6ef-4043-87eb-9b8d64b7e85f@28g2000hsw.googlegroups.com>
On Feb 22, 2:21 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth Jim Gibson <jimsgib...@gmail.com>:
>
>
>
> > In article
> > <b4ae604d-7dca-484c-bfbe-bb6ab2fd0...@e23g2000prf.googlegroups.com>,
> > pgodfrin <pgodf...@gmail.com> wrote:
>
> > > Hi - I knew there was a better way! I will try this out. I realized as
> > > I was posting my code that it is indeed silly to dump the file into
> > > memory. I've changed that. Why is the three argument version of open
> > > better though?
>
> > It is more explicit and more flexible. See 'perldoc -f open' for all
> > the things you can do with the 3-argument version of open. For simple
> > programs, it doesn't really matter. But for more complicated programs,
> > the explicitness is helpful and consistency is good for
> > maintainability.
>
> > One slightly facetious difference: with 2-argument open you can't open
> > a file named '<stupid_file_name.txt'.
>
> Much more importantly, if someone passes you a filename of
> 'rm -rf /; echo |' you won't delete all your files by mistake.
>
> Ben
Howdy - good advice all around. Now my code is really efficient, but
it still doesn't work <grin>
#!/usr/bin/perl
use Env;
open $rvars,"<","/home/pgodfrin/perl/et/etfile" ;
foreach(<$rvars>)
{
if (/^export\s+(\w+)=(.+)/)
{
$ENV{$1}=$2;
}
}
$ENV{TMPTREE}='myliteral';
foreach $key (sort(keys %ENV)) {
print $key, '=', $ENV{$key}, "\n";
}
results:
TMPTEST=/tmp
TMPTOO=$TMPTEST
TMPTREE=myliteral
I also tried what Joost suggested - nope...
Anyone test this?
pg
------------------------------
Date: Fri, 22 Feb 2008 16:32:31 -0800 (PST)
From: Petyr David <phynkel@gmail.com>
Subject: Re: Speeding my script
Message-Id: <94a217a6-9fd8-4e43-b415-774173a43912@n77g2000hse.googlegroups.com>
On Feb 22, 2:48 pm, "J. Gleixner" <glex_no-s...@qwest-spam-no.invalid>
wrote:
> Petyr David wrote:
> > have a web page calling PERL script that searches for patterns in 20,
> > 000 files + and returns link to files and lines found matching
> > pattern. I use a call to `find` and `egrep`
>
> > Q: Script works - but is straining under the load - files are in the
> > Gbs.
> > How to speed process? How simple to employ threads or slitting
> > off
> > new processes?
>
> > I know i should RTFM (LOL) and I will, but just looking for some
> > quick guidance/suggestions
>
> No need to LOL at your laziness.
>
> Using find/grep on thousands of files and Gb of data is a poor
> choice. Try looking at various indexing tools: htdig, glimpse,
> Swish-e, etc.
Agreed, but it was my first project in PERL. It started out as a very,
very simple file searcher
and then a bunch of people asked if anyone knew of file search
software that could be implementd quickly.
I meekly raised my hand. Since then a lot of options have been added
and I do believe
that I either take this to the next step, using one of the indexing
tools mentioned, or I
leave it "as is". I have plenty of other things to do. It's just that
I like programming.
My other responsibilities pay me plenty, but are boring and are almost
clerical in nature
TX to all for the help
------------------------------
Date: Fri, 22 Feb 2008 13:37:27 -0800 (PST)
From: "comp.llang.perl.moderated" <ced@blv-sam-01.ca.boeing.com>
Subject: Re: Split a string in perl
Message-Id: <f6633b50-6f61-4750-894c-ad8754f23eb3@h11g2000prf.googlegroups.com>
On Feb 22, 12:34 pm, John <chunj...@gmail.com> wrote:
> On Feb 22, 11:56 am, "comp.llang.perl.moderated" <c...@blv-
>
>
>
> sam-01.ca.boeing.com> wrote:
> > On Feb 22, 11:01 am, John <chunj...@gmail.com> wrote:
>
> > > There must be something that I have ignored in PERL.
>
> > > To split a string with delimiter "|", sometimes, I need to give "\",
> > > sometimes it works for "\\" only.
>
> > > Here are the details.
>
> > > The String is " 0000|Null".
>
> > > And the ways I have tried to do split are:
> > > 1. my @columns = split('\|', $String);
> > > 2. my ( $proj, $desc) = split('\|', $String);
> > > 3. my ( $proj, $desc) = split('\\|', $String);
>
> > > Case 1 and Case 3 are good. But Case 2 failed, both $proj and $desc
> > > receive an empty string .
>
> > > What was wrong with case 2 , I thought if Case 1 works, Case2 should
> > > be working also, Right ? I am using perl 5.6.1 on linux box.
>
> > case 2 works as is. Is it possible you meant:
> > split( "\|", $String ) ...?
>
> > In that case, double-quotish interpolation causes
> > your split regex to be just '|':
>
> > # perl -MO=Deparse,-p -e 'split "\|"," 0000|Null"'
> > split(/|/, ' 0000|Null', 0);
> > -e syntax OK
>
> > Then, you have the alternation meta character
> > alone with empty alternatives on either side
> > which'll match the 1st 2 characters encountered:
>
> > $proj=' ' $desc='0'
>
> > Is it possible you had 2 leading blanks in
> > string which "looked" like 2 empty strings for
> > $proj and $desc...
>
> > --
> > Charles DeRykus- Hide quoted text -
>
> > - Show quoted text -
>
> I do not have any leading blanks in this String.
>
Hm, the original post does however:
> "The String is " 0000|Null".
--
Charles DeRykus
------------------------------
Date: Fri, 22 Feb 2008 23:22:31 +0100
From: Tom Brown <not@home.net>
Subject: Spreadsheet::ParseExcel - How to get certain Cells
Message-Id: <2008022223223116807-not@homenet>
Hello,
how may I get certain cells (rows and columns) from an Excel file using
the module Spreadsheet::ParseExcel???
Let's asume an Excel-sheet contains the following values:
[ A ][ B ][ C ]
[1] Cell_A1 Cell_B1 Cell_C1
[2] Cell_A2 Cell_B2 Cell_C2
[3] Cell_A3 Cell_B3 Cell_C3
[4] Cell_A4 Cell_B4 Cell_C4
[5] Cell_A5 Cell_B5 Cell_C5
[6] Cell_A6 Cell_B6 Cell_C6
Let's asume further B2 - B5 are the cells I would like to parse. I
tried it out with the following code, but there are few mistakes.
[-------------------------CODE-------------------------]
use strict;
use Spreadsheet::ParseExcel;
my $Excel_file = new Spreadsheet::ParseExcel;
my $oBook = $Excel_file->Parse('example.xls');
my $iRow=1;
my $iColumn=0;
my ($oWkS, $oWkC);
#for(my $iSheet=0; $iSheet < $oBook->{SheetCount} ; $iSheet++) {
for(my $iSheet = $oBook->{Worksheet}[0]) {
$oWkS = $oBook->{Worksheet}[$iSheet];
print "--------- SHEET: ", $oWkS->{Name}, "\n";
for($iRow ;
defined $oWkS->{MaxRow} && $iRow <= $oWkS->{MaxRow} ;
$iRow++) {
for($iColumn;
defined $oWkS->{MaxCol} && $iColumn <=
$oWkS->{MaxCol} ; $iColumn++) {
$oWkC = $oWkS->{Cells}[$iRow][$iColumn];
# print "( $iRow , $iColumn ) =>", $oWkC->Value, "\n"
if($oWkC);
print $oWkC->Value, "\n" if($oWkC);
}
}
}
[-------------------------CODE-------------------------]
My mistakes I know about already:
1. I do not know how to specify the range to be parsed.
2. The value I am getting from the script above is the whole row.
But I am looking for the columns B2 - B5 which should give me
the following values:
Cell_B2
Cell_B3
Cell_B4
Cell_B5
As I am in general quite new to Perl and to the module in particular,
I got stuck at this point.
Therefore, I appreciate any help.
Thank you in advance.
Tom
------------------------------
Date: Fri, 22 Feb 2008 17:36:49 -0600
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Spreadsheet::ParseExcel - How to get certain Cells
Message-Id: <47bf5c91$0$515$815e3792@news.qwest.net>
Tom Brown wrote:
> Hello,
>
> how may I get certain cells (rows and columns) from an Excel file using
> the module Spreadsheet::ParseExcel???
>
> Let's asume an Excel-sheet contains the following values:
>
> [ A ][ B ][ C ]
> [1] Cell_A1 Cell_B1 Cell_C1
> [2] Cell_A2 Cell_B2 Cell_C2
> [3] Cell_A3 Cell_B3 Cell_C3
> [4] Cell_A4 Cell_B4 Cell_C4
> [5] Cell_A5 Cell_B5 Cell_C5
> [6] Cell_A6 Cell_B6 Cell_C6
>
> Let's asume further B2 - B5 are the cells I would like to parse. I tried
> it out with the following code, but there are few mistakes.
>
> [-------------------------CODE-------------------------]
> use strict;
> use Spreadsheet::ParseExcel;
> my $Excel_file = new Spreadsheet::ParseExcel;
>
> my $oBook = $Excel_file->Parse('example.xls');
> my $iRow=1;
> my $iColumn=0;
> my ($oWkS, $oWkC);
>
> #for(my $iSheet=0; $iSheet < $oBook->{SheetCount} ; $iSheet++) {
> for(my $iSheet = $oBook->{Worksheet}[0]) {
Don't need a for() there because you're only going through one
Worksheet, the first one, [0];
my $iSheet = $oBook->{Worksheet}[0];
> $oWkS = $oBook->{Worksheet}[$iSheet];
> print "--------- SHEET: ", $oWkS->{Name}, "\n";
Since you know what row.. and column.. it might be easier to understand as:
> for($iRow ;
> defined $oWkS->{MaxRow} && $iRow <= $oWkS->{MaxRow} ;
> $iRow++) {
for my $iRow ( 2 .. 5 ) {
> for($iColumn;
> defined $oWkS->{MaxCol} && $iColumn <=
> $oWkS->{MaxCol} ; $iColumn++) {
Since you want a specific column, you don't need another 'for' loop.
You could set this outside of your first loop.
my $iColumn = 2;
> $oWkC = $oWkS->{Cells}[$iRow][$iColumn];
> # print "( $iRow , $iColumn ) =>", $oWkC->Value, "\n"
> if($oWkC);
> print $oWkC->Value, "\n" if($oWkC);
> }
> }
> }
> [-------------------------CODE-------------------------]
>
> My mistakes I know about already:
>
> 1. I do not know how to specify the range to be parsed.
> 2. The value I am getting from the script above is the whole row.
Because of the third 'for' loop. Had you uncommented the print above
you should have seen the values for iRow and iColumn and seen
that it was going through every cell in every row and column.
> But I am looking for the columns B2 - B5 which should give me
> the following values:
>
> Cell_B2
> Cell_B3
> Cell_B4
> Cell_B5
>
> As I am in general quite new to Perl and to the module in particular,
> I got stuck at this point.
Most (All?) languages have 'for' loops, so this shouldn't be a new topic.
Maybe reviewing:
perldoc perlinto
perldoc perlsyn
might help you get reaquainted. :-)
I also must say you did a good job stating your issue, showing
code, and sample data. I wish more people would do the same.
------------------------------
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 V11 Issue 1300
***************************************