you are correct, you do not need " " around $.
thank you!
I was using strict and warnings, but must of had another error.
I am unfamiliar with the variable $.? I tried playing with it, but was
unsuccessful. I could not find it in cookbook nor in programming perl.
linect=$.
linect=1;
while ( <FILEHANDLE>) {
if ( $linect > 9 ) {
do whatever ...
}
$linect++;
}
Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams
Jeff 'japhy'
Pinyan
<[Email Removed] To
rg> [Email Removed]
cc
02/15/2005 10:38 'Perl Beginners '
AM <[Email Removed]>
Subject
RE: Start reading from a specific
line
On Feb 15, [Email Removed] said:
QUOTE |
my $linect="$."; while ( <FILEHANDLE>) { if ( $linect > 9 ) { do whatever ... } $linect++; }
need to use double-quotes around variable $.
|
No you don't.
my $linecount = $.;
works just fine.
And is there a reason you don't want to use $.? That is, why create
another variable which does the same thing?
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
[Email Removed] wrote:
QUOTE |
you are correct, you do not need " " around $. thank you! I was using strict and warnings, but must of had another error. I am unfamiliar with the variable $.? I tried playing with it, but was unsuccessful. I could not find it in cookbook nor in programming perl.
It contains the current line number read of the current open file that you are reading. You do not need to initialize it, just use as if ( $. > 10 ) { # then do somehting}else { # do something else} |
Wags ;)
QUOTE |
linect=$. linect=1; while ( <FILEHANDLE>) { if ( $linect > 9 ) { do whatever ... } $linect++; }
Derek B. Smith OhioHealth IT UNIX / TSM / EDM Teams
Jeff 'japhy' Pinyan <[Email Removed] To rg> [Email Removed]
cc 02/15/2005 10:38 'Perl Beginners ' AM <[Email Removed]
Subject RE: Start reading from a specific line
On Feb 15, [Email Removed] said:
my $linect="$."; while ( <FILEHANDLE>) { if ( $linect > 9 ) { do whatever ... } $linect++; }
need to use double-quotes around variable $.
No you don't.
my $linecount = $.;
works just fine.
And is there a reason you don't want to use $.? That is, why create another variable which does the same thing?
-- Jeff "japhy" Pinyan % How can we ever be the sold short or RPI Acacia Brother #734 % the cheated, we who for every service http://japhy.perlmonk.org/ % have long ago been overpaid? http://www.perlmonks.org/ % -- Meister Eckhart
-- To unsubscribe, e-mail: [Email Removed] For additional commands, e-mail: [Email Removed] <http://learn.perl.org/> <http://learn.perl.org/first-response
|
*******************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
*******************************************************
Jenda Krynicky
Feb 15 2005, 04:14 PM
From: [Email Removed]
QUOTE |
you are correct, you do not need " " around $. thank you! I was using strict and warnings, but must of had another error. I am unfamiliar with the variable $.? I tried playing with it, but was unsuccessful. I could not find it in cookbook nor in programming perl.
|
Try the manpages. It's in perlvar.
c:> perldoc perlvar
Jenda
===== [Email Removed] ===
http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
Could not find this variable in perlvar under 5.8.0 for sun solaris
can you explain it?
Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams
"Jenda Krynicky"
<[Email Removed]
z> To
'Perl Beginners '
02/15/2005 11:14 <[Email Removed]>
AM cc
Subject
RE: Start reading from a specific
line
From: [Email Removed]
QUOTE |
you are correct, you do not need " " around $. thank you! I was using strict and warnings, but must of had another error. I am unfamiliar with the variable $.? I tried playing with it, but was unsuccessful. I could not find it in cookbook nor in programming perl.
|
Try the manpages. It's in perlvar.
c:> perldoc perlvar
Jenda
===== [Email Removed] ===
http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Jenda Krynicky
Feb 15 2005, 05:26 PM
From: [Email Removed]
QUOTE |
Could not find this variable in perlvar under 5.8.0 for sun solaris can you explain it?
Derek B. Smith
|
No. It has always been there since I can remember. (Perl 5.001 I
think, definitely Perl 5.003)
Jenda
===== [Email Removed] ===
http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
Graeme St. Clair
Feb 15 2005, 07:56 PM
Well, it is, you know. In my 3rd Edn, July 2000 printing, it's indexed
about half way down the left column on p1011, where it is also described as
$INPUT_LINE_NUMBER. Admittedly, the first reference (to p103) is off, as
some refs are in this edn. But it's only just over onto p104. And p665
(correct this time!) gives you the goods.
Rgds, GStC.
-----Original Message-----
From: [Email Removed] [mailto:[Email Removed]]
Sent: Tuesday, February 15, 2005 11:01 AM
To: 'Perl Beginners '
Subject: RE: Start reading from a specific line
you are correct, you do not need " " around $.
thank you!
I was using strict and warnings, but must of had another error.
I am unfamiliar with the variable $.? I tried playing with it, but was
unsuccessful. I could not find it in cookbook nor in programming perl.
Yes there was some mis-communication as I thought he was referring to
variable $.? as opposed to $.
Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams
"Graeme St.
Clair"
<Graeme.St.Clair@ To
hds.com> "'Perl Beginners '"
<[Email Removed]>
02/15/2005 02:56 cc
PM
Subject
RE: Start reading from a specific
line
Well, it is, you know. In my 3rd Edn, July 2000 printing, it's indexed
about half way down the left column on p1011, where it is also described as
$INPUT_LINE_NUMBER. Admittedly, the first reference (to p103) is off, as
some refs are in this edn. But it's only just over onto p104. And p665
(correct this time!) gives you the goods.
Rgds, GStC.
-----Original Message-----
From: [Email Removed] [mailto:[Email Removed]]
Sent: Tuesday, February 15, 2005 11:01 AM
To: 'Perl Beginners '
Subject: RE: Start reading from a specific line
you are correct, you do not need " " around $.
thank you!
I was using strict and warnings, but must of had another error.
I am unfamiliar with the variable $.? I tried playing with it, but was
unsuccessful. I could not find it in cookbook nor in programming perl.
--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
John W. Krahn
Feb 15 2005, 09:13 PM
Jenda Krynicky wrote:
QUOTE |
From: [Email Removed]
Could not find this variable in perlvar under 5.8.0 for sun solaris can you explain it?
No. It has always been there since I can remember. (Perl 5.001 I think, definitely Perl 5.003)
|
It has been there since Perl verion 1.0 but back then it was readonly.
John
--
use Perl;
program
fulfillment
John W. Krahn
Feb 15 2005, 09:20 PM
Jeff 'japhy' Pinyan wrote:
QUOTE |
On Feb 15, Eduardo Vzquez Rodrguez said:
open(INPUT, $file) or die "Can't read from file: $! $file"; # Where we "move" the pointer to line number 10
$. = 0;
You don't need to initialize $. to 0. It's a magical variable that holds the right value.
|
That depends. If you open another file with the same filehandle you used
previously and didn't close that filehandle first you will get defined but
probably unexpected behaviour. :-)
John
--
use Perl;
program
fulfillment
Chris Devers
Feb 16 2005, 06:20 PM
On Wed, 16 Feb 2005, Tim Johnson wrote:
QUOTE |
On Wed, 16 Feb 2005, Daniel Kasak wrote:
How do I detect what platform I'm running from inside a Perl script?
Check out the $^O variable.
|
....but be careful in writing code that matches it:
Under Mac OSX:
$ perl -le 'print $^O'
darwin
$
Under Cygwin on a Win2000 machine:
cdevers@bruno ~
$ perl -le 'print $^O'
cygwin
cdevers@bruno ~
$
A CMD shell on the same machine:
C:cygwinusrbin>perl -le 'print $^O'
perl -le 'print $^O'
C:cygwinusrbin>
Yes, it produced no output. I don't know why.
I believe the Activestate version reports 'win32' or something, but I
don't have a copy handy to confirm that.
Note that the string 'win' shows up in several different ways, so if you
just want to match on Windows (or win32, or win2000, or similar) you
have to make sure that you don't also catch the slightly different
Cygwin environment or the much different OSX one with the same pattern.
--
Chris Devers
Tyson Sommer
Feb 18 2005, 10:33 PM
QUOTE |
-----Original Message----- From: Jay [mailto:[Email Removed]] Sent: Friday, February 18, 2005 3:42 PM To: [Email Removed] Subject: Re: Securing user data
On Fri, 18 Feb 2005 14:33:24 -0600, Tyson Sommer <[Email Removed]> wrote: Are there any not-so-obvious problems with untainting user input for passing to the shell with something like (for the sake of simplicity):
# need to be able to use "." and "-" characters as well as alphanumerics chomp ( my $input = <STDIN> ); $input =~ s/[^A-Za-z0-9-.]//g;
system ("some_system_binary_here $input");
Thanks! tyson
That partly depends on how protable you want to be. You might look into posix or unicode classes, e.g. p{IsAlnum} or [:alnum:]. Also, it looks as if you're probably taking a filname here, '_' is probably a valid character,
|
Oh yeah, forgot that one :-)
QUOTE |
and in square brackets, '.' is a literal '.' not a metacharacter, so it shouldn't be escaped.
|
Gotcha. Now... will escaping them in the character class actually be
escaping them? Or did I just allow for "" as well? I'll test it out...
Nope! The result is the same whether I escape "-" with "" or not. Didn't
allow for "" and did allow "-" either way. Learning...
QUOTE |
Beyond that, think a little bit about what your application is here. Figure out what you're expecting here, and look fo it. Will all your filenames have, say, dots and an extension? then perform the subtitution and then do 'next unless /.+..{3}/'.
|
Basically, that's it. Let's say, just for the sake of argument, that I want
to ping something (that may or may not arbitrarily have "."s, "_"s, and/or
"-"s in its filename in arbitrary positions). I want to make sure a user
can't input something like:
"some_valid_device | some_malicious_code"
So that after it tells the system to run the ping (or whatever) they can't
then trick the system into piping thru some other potentially malicious
program.
QUOTE |
What program are you passing to? Is it a *nix system? How will the program react to a bare '-', especially if there's nothing further coming on STDIN? Are there situation in which the input could be interpreted as an argument, rather than a file to open (or vice versa, depending on the application)? For instance, if the user enters '-v', will that return version information on your program. Could that information be abused?
|
Those are some good points. I should definitely strip any leading "-"s. This
appears to work:
$input =~ s/^-*|[^A-Za-z0-9.-_]//g;
QUOTE |
Some thngs to think about.
|
Much thanks!
Anyone think of any more potential problems that I might be missing?
Tyson
Gavin Henry
Feb 18 2005, 11:53 PM
On Friday 18 Feb 2005 23:16, Tyson Sommer wrote:
QUOTE |
Is there any way to squeeze the following into one line? (assume using CGI.pm and that my $q = new CGI;):
|
Don't hijack threads. Please start another.
--
Just getting into the best language ever...
Fancy a [Email Removed] or something
on
http://www.perl.me.uk Just ask!!!
John W. Krahn
Feb 19 2005, 12:53 AM
Tyson Sommer wrote:
QUOTE |
Those are some good points. I should definitely strip any leading "-"s. This appears to work:
$input =~ s/^-*|[^A-Za-z0-9.-_]//g;
|
That could be simplified to:
$input =~ s/^-*|[^a-z.-_]//g;
Because the range [.-_] includes uppercase letters and numbers unless you
really meant to write [^A-Za-z0-9.-_] or [^A-Za-z0-9._-]? :-)
John
--
use Perl;
program
fulfillment
TapasranjanMohapatra
Feb 21 2005, 04:36 AM
-----Original Message-----
From: Harold Castro [mailto:[Email Removed]]
Sent: Mon 2/21/2005 9:42 AM
To: [Email Removed]
Cc:
Subject: Perl waits for while to finish before printing if on the same line,why?(countdown prog)
Hi,
I'm new to perl and i'm trying to create a simple
program that will act like a time bomb but will print
a dot(something like a progress bar every second until
the specified time arrives. Here's my code.
my $countdown = 5;
while ($countdown > 0){
print ".";
sleep 1;
$countdown--;
}
print "Kaboom!!"
And the result..
after waiting for 5 seconds, it displayed this line at
once:
......Kaboom!!
Why does this happens with a while loop? I told it to
print a single dot every 1 second. Do you know another
way of making my progress bar program that display a
single dot every one second on the same line until it
reaches a given time?
Thank you!
__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Hi Harold,
if you set $| to some postive value, it will work.
add one line to your script.
---------------------------------
$| = 1;
my $countdown = 5;
while ($countdown > 0){
print ".";
sleep 1;
$countdown--;
}
print "Kaboom!!"
---------------------------------
The problem is with buffers. Though I am not very clear whats happening internally, I can say setting $| to some positive value will at the beginning of script will solve the problem. It flushes the buffer after every print.
HTH
Tapas
Thomas Btzler
Feb 21 2005, 07:11 AM
Mark Jayson Alvarez <[Email Removed]> asked:
[...]
QUOTE |
Why does this happens with a while loop? I told it to print a single dot every 1 second. Do you know another way of making my progress bar program that display a single dot every one second on the same line until it reaches a given time?
|
Perl uses output buffering by default. Use "$| = 0;"
to unbuffer the currently selected output filehandle.
HTH,
Thomas
Chris Devers
Feb 23 2005, 02:18 PM
On Wed, 23 Feb 2005, Tyson Sommer wrote:
QUOTE |
This might be a question for beginners-cgi, but since it was mentioned here...
|
Sounds like it, but oh well.
This doesn't really fix your problem so much as your error handling, but
why aren't you catching the reason your script dies?
Instead of
my $pinger = Net::Ping->new("icmp") || die;
Why not use
my $pinger = Net::Ping->new("icmp") || die "Can't make pinger: $!";
This might at least give you a more descriptive error message...
Also, are you having CGI::Carp use fatalsToBrowser for testing?
--
Chris Devers
Bob Showalter
Feb 23 2005, 07:31 PM
David Moron wrote:
QUOTE |
Hi everybody,
I'm testing a simple IPC implementation that uses Linux pipes to communicate a producer script and a consumer. The producer script looks like:
while(1){ print "Hellon"; sleep(2); }
And the consumer:
my @lines; while(1){ sleep(10); if (<STDIN>){#Non-blocking reading
|
This reads a line, and it blocks. The line that was read is discarded.
QUOTE |
print "Reading lines...";
|
You don't see this message because STDOUT is line buffered.
This reads all remaining lines. That's your problem, since producer.pl never
quits. Eventually you'll run out of memory and the process will die.
QUOTE |
print "@lines"; }else{ print "No lines received.n"; } }
If I run the producer or the consumer alone, there are no problems. $./producer.pl Hello Hello
$echo "Hello" | ./consumer.pl Hello
|
This works because the input pipe is closed after the echo.
QUOTE |
But if I run them together, then de producer does nothing. $./producer.pl | ./consumer.pl
|
This doesn't work because the input pipe never closes.
QUOTE |
How can I connect the producer standar output and consumer standar input?
|
Well, you have them connected, but I'm not sure what you're trying to do
with consumer.pl
Bob Showalter
Feb 23 2005, 07:49 PM
David Moron wrote:
QUOTE |
The consumer has only to read lines from its STDIN and print them to the STDOUT.
|
print while(<>);
QUOTE |
These scripts are a simplified version of the producer/consumer problem and they try to emulate a multithreaded perl program. IMHO It's not important what I want to do with consumer, the problem is that consumer cann't read lines from producer because producer never sends EOF.
|
Well of course it can read lines. It just can't read *all* the lines into an
array, because the only way for it to know it has read *all* the lines is to
receive an EOF.
From perldoc perlop:
If a <FILEHANDLE> is used in a context that is looking for a list, a
list comprising all input lines is returned, one line per list
element.
It's easy to grow to a rather large data space this way, so use with
care.
David Moron
Feb 23 2005, 08:42 PM
The consumer has only to read lines from its STDIN and print them to the
STDOUT.
These scripts are a simplified version of the producer/consumer problem
and they try to emulate a multithreaded perl program. IMHO It's not
important what I want to do with consumer, the problem is that consumer
cann't read lines from producer because producer never sends EOF.
Bob Showalter wrote:
QUOTE |
David Moron wrote:
Hi everybody,
I'm testing a simple IPC implementation that uses Linux pipes to communicate a producer script and a consumer. The producer script looks like:
while(1){ print "Hellon"; sleep(2); }
And the consumer:
my @lines; while(1){ sleep(10); if (<STDIN>){#Non-blocking reading
This reads a line, and it blocks. The line that was read is discarded.
print "Reading lines...";
You don't see this message because STDOUT is line buffered.
@lines=<STDIN>;
This reads all remaining lines. That's your problem, since producer.pl never quits. Eventually you'll run out of memory and the process will die.
print "@lines"; }else{ print "No lines received.n"; } }
If I run the producer or the consumer alone, there are no problems. $./producer.pl Hello Hello
$echo "Hello" | ./consumer.pl Hello
This works because the input pipe is closed after the echo.
But if I run them together, then de producer does nothing. $./producer.pl | ./consumer.pl
This doesn't work because the input pipe never closes.
How can I connect the producer standar output and consumer standar input?
Well, you have them connected, but I'm not sure what you're trying to do with consumer.pl
|
--
David Morn Ruano
Coordinador de Proyectos
Grupo OpenWired, S.L.
Cardenal Reig, 26, entr. 3? - 08028 - Barcelona (Spain)
Tel (+34) 93/440 99 90 - Fax (+34) 93/448 41 44
www.openwired.net, www.tecnologialinux.com
David Moron
Feb 23 2005, 09:24 PM
I need the read non-blocking of check if there's any
data ready before doing the READ.
The complete consumer is a program that reads account
packets from a radius server (this problem has been solved)
and stores the Radius attributes (MAC,IP,User_name) in a
List of Hashes. This list of hashes has to be processed
every 60 seconds.
The consumer only reads from stdin in order to add new
packets to the listofHashes.
Here there's the consumer that works with:
#cat accounts| ./consumer.pl
where accounts has:
MAC1;IP1;USer1
MAC2;IP2;USer2
#consumer.pl
#!/usr/bin/perl -w
my (%account,@accountList,@accountListTemp);
while(1){
my (@lines,$line,@values);
sleep(60);
@lines = <STDIN>;
foreach $line (@lines){
#MAC;IP;User_Namen
chomp($line);
@values = split ';',$line;
$account{"MAC"} = $values[0];
$account{"IP"} = $values[1];
$account{"Name"} = $values[2];
push @accountList, {%account};
}
my $i;
for $i (0 .. $#accountList){
#Process @accountList <-- IMPORTANT !!
}
}
Bob Showalter wrote:
QUOTE |
David Moron wrote:
The consumer has only to read lines from its STDIN and print them to the STDOUT.
print while(<>);
These scripts are a simplified version of the producer/consumer problem and they try to emulate a multithreaded perl program. IMHO It's not important what I want to do with consumer, the problem is that consumer cann't read lines from producer because producer never sends EOF.
Well of course it can read lines. It just can't read *all* the lines into an array, because the only way for it to know it has read *all* the lines is to receive an EOF.
From perldoc perlop:
If a <FILEHANDLE> is used in a context that is looking for a list, a list comprising all input lines is returned, one line per list element. It's easy to grow to a rather large data space this way, so use with care.
|
--
David Morn Ruano
Coordinador de Proyectos
Grupo OpenWired, S.L.
Cardenal Reig, 26, entr. 3? - 08028 - Barcelona (Spain)
Tel (+34) 93/440 99 90 - Fax (+34) 93/448 41 44
www.openwired.net, www.tecnologialinux.com
Bob Showalter
Feb 24 2005, 02:40 PM
John wrote:
QUOTE |
Hi all
I tried to chroot the perl and when i am trying to run a Net::FTP script
I receive the error
Net::FTP: Bad protocol 'tcp'
Do you know what is going on?
|
/etc/protocols doesn't exist in your chroot environment?
Tyson Sommer
Feb 24 2005, 02:41 PM
QUOTE |
-----Original Message----- From: Chris Devers [mailto:[Email Removed]] Sent: Wednesday, February 23, 2005 1:18 PM To: Tyson Sommer Cc: 'Perl Beginners List' Subject: Re: printing output of ping command -- New question at bottom.
On Wed, 23 Feb 2005, Tyson Sommer wrote:
This might be a question for beginners-cgi, but since it was mentioned here...
Sounds like it, but oh well.
This doesn't really fix your problem so much as your error handling, but why aren't you catching the reason your script dies?
Instead of
my $pinger = Net::Ping->new("icmp") || die;
Why not use
my $pinger = Net::Ping->new("icmp") || die "Can't make pinger: $!";
|
I do that. I just was being lazy in typing here (thought that part was
inconsequential).
QUOTE |
This might at least give you a more descriptive error message...
Also, are you having CGI::Carp use fatalsToBrowser for testing?
|
Yes. Thus the error message that I'm seeing in my browser.
Randal L. Schwartz
Feb 27 2005, 02:40 PM
QUOTE |
"Eduardo" == Eduardo <Vzquez Rodrguez> writes:
|
Eduardo> $sth10 = $dbh->prepare ("COPY dummy FROM /home/postgres/dummy/dummyfile");
The filename needs to be quoted. You can use placeholders if you wish.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +
<[Email Removed]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
understood!
thanks,
"Wiggins
d'Anconia"
<wiggins@danconia To
.org> [Email Removed]
cc
02/28/2005 01:05 Perl Beginners <[Email Removed]>
PM Subject
Re: return code
[Email Removed] wrote:
QUOTE |
$exit_value = $? >> 8 $signal_num = $? & 127; $dumped_core = $? & 128;
ok this helps, thanks! But, in my small block of code I am using $? >> 8 as so
system ("cat /tmp/used"); # /tmp/used does not exist so this should return 0 for false in Perl and 1 false in Shell
|
Nope, because $? is just a way to access what the shell returned. So $?
is in the shell context, not Perl context. So C<system> returns true
because 'cat' does run (aka the shell didn't fail to execute the program
-- permission error, command not found, etc.). But it is 'cat' that
exited (the key here is the difference between failing to run (perl
context) and running, but exiting with a non-zero value (shell context)).
QUOTE |
my $exval = $? >> 8;
|
So the above is the *exit value* of the shelled command, cat.
QUOTE |
print "printing exit value: $exval n"; if ( $exval == 0 ) { print "false cat command did not happenn";
|
This is incorrect, the 'cat' command *did* happen, it failed, but it did
execute!!
QUOTE |
{else} print "$exval is: $exval n"; } }
|
my $return = system('cat /tmp/used') or die "'cat' didn't run at all!";
my $exval = $? >> 8;
if ($exval) {
warn "'cat' ran but exited unsuccessfully: $exval";
}
else {
warn "'cat' succeeded: $exval";
}
Of course 'cat' is a poor example, because you would have to capture the
output, and to do that you need to use backticks rather than 'system'.
(Not to mention it would be silly to shell out to cat since Perl can
just open the file.)
QUOTE |
AND it is returning 2??? WHY? The output is below:
linect is not less than 2: 3 cat: cannot open /tmp/used printing exit value: 2 true exit value is: 2
|
'2' is cat's way of saying I couldn't find a file to read so I am
bailing. It is program specific (but will often use the system
conventions as a guide).
[snip]
For those reading along *THIS* is why it is *always* better to handle
something in Perl directly than to shell out to the system. If you want
to read a file use 'open', if you want to list files in a directory use
'opendir' (or a glob), etc. The code will almost always be longer to
shell out, assuming you do it correctly!
http://danconia.org
Carl Johnson wrote:
QUOTE |
Hello group,
I'm trying to read the directory "C:GISwrapped_data" and write record. My scripts is erroring with "can't open directory: no such file or directory. What am I missing?
$outfile = "$infile.txt"; my $dir = "C:GISwrapped_data"; With double quotes, what perl is seeing is C:GISwrapped_data which is not what you wnat. Change to single quotes or switch / and it will work as you want. |
Wags ;)
QUOTE |
opendir(DIR,"$dir") or die "Can't open $dir directory: $!"; open (OUT, ">$outfile") or die "Error, cannot open file: $outfile. $!"; $record = ""; $index=0; while ( $numbytes = read(DIR, $record, 1200) ) { $index++; if ($numbytes == 1200) { print OUT "$record"; } else { die "File Read Error on record $index: $numbytes bytes read; Should be 1200.n"; } } close DIR; close OUT;
Carl Johnson Principal Consultant - P - F [Email Removed]
|
*******************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
*******************************************************
Mark Taylor
Feb 28 2005, 09:23 PM
Hey Carl -
I got in a hurry and did some cut and paste without paying close enough
attention.
The line should read
my $dir = 'C:GISwrapped_data';
I apologize for the error.
Mark
QUOTE |
got this error line 9 near "= =" aborted due to compilation error
*/Mark Taylor <[Email Removed]>/* wrote:
Hello group,
Hello.
I'm trying to read the directory "C:GISwrapped_data" and write record. My scripts is erroring with "can't open directory: no such file or directory. What am I missing?
$outfile = "$infile.txt"; my $dir = "C:GISwrapped_data"; opendir(DIR,"$dir") or die "Can't open $dir directory: $!"; open (OUT, ">$outfile") or die "Error, cannot open file: $outfile. $!"; $record = ""; $index=0; while ( $numbytes = read(DIR, $record, 1200) ) { $index++; if ($numbytes == 1200) { print OUT "$record"; } else { die "File Read Error on record $index: $numbytes bytes read; Should be 1200.n"; } } close DIR; close OUT;
Try this, my $dir = = 'C:GISwrapped_data' instead.
HTH, Mark
*Carl Johnson* *Principal Consultant* * - P* * - F* *[Email Removed] <mailto:[Email Removed]>* |
Wiggins d'Anconia
Mar 1 2005, 02:01 PM
[Email Removed] wrote:
QUOTE |
I am running AIX 5.2 with gcc 3.3.2
Can anyone help? I looked for the specific list and could not find it.
Here are my errors after a make with CC pointed to cc_r and /usr/local/bin/gcc :
(See attached file: out)(See attached file: outwithgcc)
derek
|
In general the compiler used to compile perl has to be available when
compiling modules for that perl. Was the perl compiled with cc_r or gcc?
It seems from the output that perl expects cc_r otherwise it wouldn't
have unrecognized options, and I suspect it tried that first? You will
either need to match the compiler that perl was originally built with,
or compile a new perl with gcc....
But I am not a build expert, there are a few of those here, but if you
get really stuck there are lots on the p5porters list.
http://danconia.org
yeah thats what I thought and the problem is Perl came installed under
/usr/bin by default. Obviously is was compiled by cc_r and currently I am
trying to get a trial version of XLC IBMs AIX C compiler.
thank you,
Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams
"Wiggins
d'Anconia"
<wiggins@danconia To
.org> [Email Removed]
cc
03/01/2005 09:01 Perl Beginners <[Email Removed]>
AM Subject
Re: Digest-SHA2.10 compilation on
AIX 5.2 fails
[Email Removed] wrote:
QUOTE |
I am running AIX 5.2 with gcc 3.3.2
Can anyone help? I looked for the specific list and could not find it.
Here are my errors after a make with CC pointed to cc_r and /usr/local/bin/gcc :
(See attached file: out)(See attached file: outwithgcc)
derek
|
In general the compiler used to compile perl has to be available when
compiling modules for that perl. Was the perl compiled with cc_r or gcc?
It seems from the output that perl expects cc_r otherwise it wouldn't
have unrecognized options, and I suspect it tried that first? You will
either need to match the compiler that perl was originally built with,
or compile a new perl with gcc....
But I am not a build expert, there are a few of those here, but if you
get really stuck there are lots on the p5porters list.
http://danconia.org
Nevermind I got it!
my @a=();
my $e=0;
my $res = Net::DNS::Resolver->new;
my $query = $res->query(".ohnet", "NS");
if ( $query != 0 or $query != undef or $query != ' ' ) {
foreach ($query->answer) {
if ( $_->type eq 'NS' ) {
#next unless $_->type eq "A";
#print $_->nsdname,"n";
foreach ($_->nsdname) {
push (@a,$_) if $_ =~ "svr";
$e++;
}
} else {
warn "query failed: ", $res->errorstring, "n";
}
}
print $a[0],"n";
print $a[1],"n";
}
Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams
Derek
Smith/Staff/OhioH
ealth To
Perl Beginners <[Email Removed]>
03/01/2005 02:17 cc
PM
Subject
assigning data to array, but only
element 0 is getting the data
my @a=();
my $res = Net::DNS::Resolver->new;
my $query = $res->query(".ohnet", "NS");
if ( $query != 0 or $query != undef or $query != ' ' ) {
foreach ($query->answer) {
if ( $_->type eq 'NS' ) {
#next unless $_->type eq "A";
#print $_->nsdname,"n";
foreach ($_->nsdname) {
push @a,$_;
$a++;
}
print $a[1];
}else{
.....
}
}
}
The end output looks like
svrqip01
svrqip02
svgqip01
svgqip02
svvqip01
svvqip02
and in the end all I want is svrqip 01 and 02 which is why I am assigning
$_->nsdname to an array, but the only element that is getting any data is
0???
thank you,
derek
Jose Nyimi wrote:
QUOTE |
-----Message d'origine----- De: Wagner, David --- Senior Programmer Analyst --- WGO [mailto:[Email Removed]] Envoy: jeudi 3 mars 2005 21:40 : Nishi Prafull Cc: [Email Removed] Objet: RE: Perl program to convert system date to yymmdd
Nishi Prafull wrote: On Thu, 3 Mar 2005 12:12:35 -0800, Wagner, David --- Senior Programmer Analyst --- WGO <[Email Removed]> wrote: Nishi Prafull wrote: HI:
I want to write a perl script that would compute the date in the format yymmdd(050303) and subsitute it for a variable in the perl script. This variable is thereafter subsituted in a command that will be run inside the script.
myTest.pl
var aDate; You can do it a number of ways, here is a start on one way:
my @MyDateTime = (); @MyTime = localtime( time ); $MyDateTime[4]++; #months start at zero, so must add 1 # # Now the array has the date and time info in the following elements: # 0 1 2 3 4 5 6 7 8 # (sec, min, hour, mday, mon, year, wday, yday, isdst) # mday is Day of Month # year is number of eyars from 1900 # wday is day of week: 0:Sun and 6: Sat # yday is number of days from 1 Jan # isdst if true then Daylight savings time is on So to get your date in the format: my $aDate = sprintf "%02d%02d%02d", $MyDateTime[5] % 100, % does a modulo against the year, so 105 comes out as 5, 106 as 6 $MyDateTime[4], $MyDateTime[4];
$aDate should now have 050303
Wags ;) Hi: Thanks. I tried the above but it did not return the correct result my @MyDateTime = (); @MyTime = localtime(time); Missed this and it should be @MyDateTime and not @MyTime
Also as part of your code, you should always use:
use strict; use warnings;
By doing this it will cut down on the number of problems you run into. I made the program as :
#!perl
use strict; use warnings;
my @MyDateTime = (); @MyDateTime = localtime(time); $MyDateTime[4]++; my $someDate = sprintf "%02d%02d%02d", $MyDateTime[5]%100, $MyDateTime[4],$MyDateTime[4];
Typo ? You twice $MyDateTime[4] Souldn't be: $MyDateTime[4],$MyDateTime[3]; ? yes it should be. Any other day and it would hav stood out as wrong, but missed it. |
Wags ;)
QUOTE |
You just lucky that today the month and day have same value 03 :-)
Regards, Jos.
|
*******************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
*******************************************************
Nishi Prafull
Mar 3 2005, 10:12 PM
Hi All:
Thanks for the replies.It works correctly.
I need to do similar thing for extracting the time in the format hhmm
ie if it 2:05 pm then i need to display 1405
If the date command returns
Thu Mar 3 13:56:24 PST 2005
then
i need to convert the time format to
1356
Please let me know how I can do this.
Thanks.
On Thu, 3 Mar 2005 13:01:24 -0800, Wagner, David --- Senior Programmer
Analyst --- WGO <[Email Removed]> wrote:
QUOTE |
Jose Nyimi wrote: -----Message d'origine----- De : Wagner, David --- Senior Programmer Analyst --- WGO [mailto:[Email Removed]] Envoy : jeudi 3 mars 2005 21:40 : Nishi Prafull Cc : [Email Removed] Objet : RE: Perl program to convert system date to yymmdd
Nishi Prafull wrote: On Thu, 3 Mar 2005 12:12:35 -0800, Wagner, David --- Senior Programmer Analyst --- WGO <[Email Removed]> wrote: Nishi Prafull wrote: HI:
I want to write a perl script that would compute the date in the format yymmdd(050303) and subsitute it for a variable in the perl script. This variable is thereafter subsituted in a command that will be run inside the script.
myTest.pl
var aDate; You can do it a number of ways, here is a start on one way:
my @MyDateTime = (); @MyTime = localtime( time ); $MyDateTime[4]++; #months start at zero, so must add 1 # # Now the array has the date and time info in the following elements: # 0 1 2 3 4 5 6 7 8 # (sec, min, hour, mday, mon, year, wday, yday, isdst) # mday is Day of Month # year is number of eyars from 1900 # wday is day of week: 0:Sun and 6: Sat # yday is number of days from 1 Jan # isdst if true then Daylight savings time is on So to get your date in the format: my $aDate = sprintf "%02d%02d%02d", $MyDateTime[5] % 100, % does a modulo against the year, so 105 comes out as 5, 106 as 6 $MyDateTime[4], $MyDateTime[4];
$aDate should now have 050303
Wags ;) Hi: Thanks. I tried the above but it did not return the correct result my @MyDateTime = (); @MyTime = localtime(time); Missed this and it should be @MyDateTime and not @MyTime
Also as part of your code, you should always use:
use strict; use warnings;
By doing this it will cut down on the number of problems you run into. I made the program as :
#!perl
use strict; use warnings;
my @MyDateTime = (); @MyDateTime = localtime(time); $MyDateTime[4]++; my $someDate = sprintf "%02d%02d%02d", $MyDateTime[5]%100, $MyDateTime[4],$MyDateTime[4];
Typo ? You twice $MyDateTime[4] Souldn't be: $MyDateTime[4],$MyDateTime[3]; ? yes it should be. Any other day and it would hav stood out as wrong, but missed it. Wags ;)
You just lucky that today the month and day have same value 03 :-)
Regards, Jos.
******************************************************* This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. *******************************************************
-- To unsubscribe, e-mail: [Email Removed] For additional commands, e-mail: [Email Removed] <http://learn.perl.org/> <http://learn.perl.org/first-response
|
David --- Senior Programmer Anal
Mar 3 2005, 10:18 PM
Nishi Prafull wrote:
QUOTE |
Hi All:
Thanks for the replies.It works correctly. I need to do similar thing for extracting the time in the format hhmm ie if it 2:05 pm then i need to display 1405
If the date command returns Thu Mar 3 13:56:24 PST 2005 then i need to convert the time format to 1356
Please let me know how I can do this. Thanks.
On Thu, 3 Mar 2005 13:01:24 -0800, Wagner, David --- Senior Programmer Analyst --- WGO <[Email Removed]> wrote: Jose Nyimi wrote: -----Message d'origine----- De : Wagner, David --- Senior Programmer Analyst --- WGO [mailto:[Email Removed]] Envoy : jeudi 3 mars 2005 21:40 : Nishi Prafull Cc : [Email Removed] Objet : RE: Perl program to convert system date to yymmdd
Nishi Prafull wrote: On Thu, 3 Mar 2005 12:12:35 -0800, Wagner, David --- Senior Programmer Analyst --- WGO <[Email Removed]> wrote: Nishi Prafull wrote: HI:
I want to write a perl script that would compute the date in the format yymmdd(050303) and subsitute it for a variable in the perl script. This variable is thereafter subsituted in a command that will be run inside the script.
myTest.pl
var aDate; You can do it a number of ways, here is a start on one way:
my @MyDateTime = (); @MyTime = localtime( time ); $MyDateTime[4]++; #months start at zero, so must add 1 # # Now the array has the date and time info in the following elements: # 0 1 2 3 4 5 6 7 8 # (sec, min, hour, mday, mon, year, wday, yday, isdst) Nishi, |
It is right here already. When you do the call to localtime, it has in element 0 seconds, 1 minutes and 2 hours. So to have the hours and seconds just requires %02d%02d and MyDateTime[2],$MyDateTime[1] as part of your sprintf
So either as an add to the existing code or
my $MyTime = sprintf "%02d%02d", MyDateTime[2],$MyDateTime[1];
Will give time always as 24 hour regards of how the user is doing it for their machine.
Wags ;)
QUOTE |
# mday is Day of Month # year is number of eyars from 1900 # wday is day of week: 0:Sun and 6: Sat # yday is number of days from 1 Jan # isdst if true then Daylight savings time is on So to get your date in the format: my $aDate = sprintf "%02d%02d%02d", $MyDateTime[5] % 100, % does a modulo against the year, so 105 comes out as 5, 106 as 6 $MyDateTime[4], $MyDateTime[4];
$aDate should now have 050303
Wags ;) Hi: Thanks. I tried the above but it did not return the correct result my @MyDateTime = (); @MyTime = localtime(time); Missed this and it should be @MyDateTime and not @MyTime
Also as part of your code, you should always use:
use strict; use warnings;
By doing this it will cut down on the number of problems you run into. I made the program as :
#!perl
use strict; use warnings;
my @MyDateTime = (); @MyDateTime = localtime(time); $MyDateTime[4]++; my $someDate = sprintf "%02d%02d%02d", $MyDateTime[5]%100, $MyDateTime[4],$MyDateTime[4];
Typo ? You twice $MyDateTime[4] Souldn't be: $MyDateTime[4],$MyDateTime[3]; ? yes it should be. Any other day and it would hav stood out as wrong, but missed it. Wags ;)
You just lucky that today the month and day have same value 03 :-)
Regards, Jos.
******************************************************* This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. *******************************************************
-- To unsubscribe, e-mail: [Email Removed] For additional commands, e-mail: [Email Removed] <http://learn.perl.org/> <http://learn.perl.org/first-response |
Nishi Prafull
Mar 3 2005, 10:22 PM
yes :-)
I realised that after sending you the email. Sorry!
Thanks for the reply.
On Thu, 3 Mar 2005 14:18:04 -0800, Wagner, David --- Senior Programmer
Analyst --- WGO <[Email Removed]> wrote:
QUOTE |
Nishi Prafull wrote: Hi All:
Thanks for the replies.It works correctly. I need to do similar thing for extracting the time in the format hhmm ie if it 2:05 pm then i need to display 1405
If the date command returns Thu Mar 3 13:56:24 PST 2005 then i need to convert the time format to 1356
Please let me know how I can do this. Thanks.
On Thu, 3 Mar 2005 13:01:24 -0800, Wagner, David --- Senior Programmer Analyst --- WGO <[Email Removed]> wrote: Jose Nyimi wrote: -----Message d'origine----- De : Wagner, David --- Senior Programmer Analyst --- WGO [mailto:[Email Removed]] Envoy : jeudi 3 mars 2005 21:40 : Nishi Prafull Cc : [Email Removed] Objet : RE: Perl program to convert system date to yymmdd
Nishi Prafull wrote: On Thu, 3 Mar 2005 12:12:35 -0800, Wagner, David --- Senior Programmer Analyst --- WGO <[Email Removed]> wrote: Nishi Prafull wrote: HI:
I want to write a perl script that would compute the date in the format yymmdd(050303) and subsitute it for a variable in the perl script. This variable is thereafter subsituted in a command that will be run inside the script.
myTest.pl
var aDate; You can do it a number of ways, here is a start on one way:
my @MyDateTime = (); @MyTime = localtime( time ); $MyDateTime[4]++; #months start at zero, so must add 1 # # Now the array has the date and time info in the following elements: # 0 1 2 3 4 5 6 7 8 # (sec, min, hour, mday, mon, year, wday, yday, isdst) Nishi, It is right here already. When you do the call to localtime, it has in element 0 seconds, 1 minutes and 2 hours. So to have the hours and seconds just requires %02d%02d and MyDateTime[2],$MyDateTime[1] as part of your sprintf
So either as an add to the existing code or my $MyTime = sprintf "%02d%02d", MyDateTime[2],$MyDateTime[1];
Will give time always as 24 hour regards of how the user is doing it for their machine.
Wags ;)
# mday is Day of Month # year is number of eyars from 1900 # wday is day of week: 0:Sun and 6: Sat # yday is number of days from 1 Jan # isdst if true then Daylight savings time is on So to get your date in the format: my $aDate = sprintf "%02d%02d%02d", $MyDateTime[5] % 100, % does a modulo against the year, so 105 comes out as 5, 106 as 6 $MyDateTime[4], $MyDateTime[4];
$aDate should now have 050303
Wags ;) Hi: Thanks. I tried the above but it did not return the correct result my @MyDateTime = (); @MyTime = localtime(time); Missed this and it should be @MyDateTime and not @MyTime
Also as part of your code, you should always use:
use strict; use warnings;
By doing this it will cut down on the number of problems you run into. I made the program as :
#!perl
use strict; use warnings;
my @MyDateTime = (); @MyDateTime = localtime(time); $MyDateTime[4]++; my $someDate = sprintf "%02d%02d%02d", $MyDateTime[5]%100, $MyDateTime[4],$MyDateTime[4];
Typo ? You twice $MyDateTime[4] Souldn't be: $MyDateTime[4],$MyDateTime[3]; ? yes it should be. Any other day and it would hav stood out as wrong, but missed it. Wags ;)
You just lucky that today the month and day have same value 03 :-)
Regards, Jos.
******************************************************* This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. *******************************************************
-- To unsubscribe, e-mail: [Email Removed] For additional commands, e-mail: [Email Removed] <http://learn.perl.org/> <http://learn.perl.org/first-response
|
Jose Nyimi
Mar 3 2005, 10:27 PM
QUOTE |
-----Message d'origine----- De: Nishi Prafull [mailto:[Email Removed]] Envoy: jeudi 3 mars 2005 23:12 : Wagner, David --- Senior Programmer Analyst --- WGO Cc: Jose Nyimi; [Email Removed] Objet: Re: RE : Perl program to convert system date to yymmdd
Hi All:
Thanks for the replies.It works correctly. I need to do similar thing for extracting the time in the format hhmm ie if it 2:05 pm then i need to display 1405
If the date command returns Thu Mar 3 13:56:24 PST 2005 then i need to convert the time format to 1356
Please let me know how I can do this. Thanks.
|
David has implicitly replied to this ;)
Anyway try this:
#!perl
use strict;
use warnings;
my($sec,$min,$hour)= localtime(time);
my $someTime = sprintf "%02d%02d",$hour,$min;
print "$someTimen";
Regards,
Jos.
Bob Showalter
Mar 9 2005, 03:38 PM
Bret Goodfellow wrote:
QUOTE |
Hi all,
I am trying to evaluate a string, and determine if the last charater of the string has a backslash '' . The piece of code I am using doesn't appear to work. What I've found with this peice of code is that if the string does contain a , then the following code still adds another slash. If there is not backslash in the string, the the code appears to work fine by adding a backslash. Any ideas?
print "Deleting old directory $dir_no: $Dir_namen"; $arg_length = length($ARGV[0]); $arg_lastchar = substr($ARGV[0], $arg_length-1, 1);
|
Why did you switch from $Dir_name to $ARGV[0]?
Anyway, this can be simplified to
$arg_lastchar = substr($ARGV[0], -1);
QUOTE |
print "last char is: $arg_lastcharn"; ################################################ # add a if one was not present in argument # ################################################ if ($arg_lastchar ne '\' or $arg_lastchar ne '/') {
|
Logic flaw. This will always be TRUE. (*any* character will either be not a
backslash or not a forward slash.)
You want "and" (or "&&") instead of "or".
You can also use a regex and do the whole thing in one line:
$Dir_name .= '\' unless ($Dir_name =~ m![\/]z!);
QUOTE |
$Dir_name = '\' . $Dir_name; print "Adding \ to: $Dir_namen"; }
|
I'm not sure what your code is doing overall, but you should typically use
modules like File::Spec, File::Basename, and File::Path for working with
directory and file names and paths in a portable fashion.
HTH,
Bob
got it dude! thanks;
my code is
foreach ( $file1, $file2, $file3, $file4 ) {
my $logs = new Logfile::Rotate (File => $_,
Count => 10,
Gzip => '/usr/bin/gzip',
Dir => '/usr/local/log/old',
Flock => 'yes',
Persist => 'yes' );
$logs->rotate();
}
Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams
Rathna N
<rathnasuresh@gma
il.com> To
"[Email Removed]"
03/09/2005 06:45 <[Email Removed]>
AM cc
Subject
Please respond to Re: log rotate on multiple files
Rathna N
<rathnasuresh@gma
il.com>
seems like, u should do it in a loop for each file.
http://search.cpan.org/~paulg/Logfile-Rotate-1.04/Rotate.pmthanks,
Rathna
On Tue, 8 Mar 2005 22:03:44 -0500, [Email Removed]
<[Email Removed]> wrote:
QUOTE |
Will someone provide clues as to how use logfile rotate on multiple files? I looked at the rotate.pm and did not see anything this the code. I tried one File => line followed by multipl scalers but that did not work then I tried multiple File => references and this did not work???
thank you,
Here is my code:
use strict; use warnings; use diagnostics; use MIME::Lite; use Logfile::Rotate;
$ENV{"PATH"} = qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);
my $wd=40; my $tpexports = qq(/usr/local/log/exports); my $hrtlabtapeszs = qq(/usr/local/log/heartlab_tapeszs.log); my $fujitapeszs = qq(/usr/local/log/fuji_tapeszs.log); my $slhtapeszs = qq(/usr/local/log/slh_tapeszs.log);
open (FFFF_, "+>$tpexports") || die "could not open file: $tpexports $!"; open (FFF_, "+<$hrtlabtapeszs") || die "could not open file: $hrtlabtapeszs $!"; open (FF_, "+<$fujitapeszs") || die "could not open file: $fujitapeszs $!"; #open (F_, "+<$slhtapeszs") || die "could not open file: $slhtapeszs $!";
my $logs = new Logfile::Rotate (File => $tpexports, File => $hrtlabtapeszs, Count => 10, Gzip => '/usr/bin/gzip', Dir => '/usr/local/log/old', Flock => 'yes', Persist => 'yes' ); $logs->rotate();
Derek B. Smith OhioHealth IT UNIX / TSM / EDM Teams
-- To unsubscribe, e-mail: [Email Removed] For additional commands, e-mail: [Email Removed] <http://learn.perl.org/> <http://learn.perl.org/first-response
|
--
Regards,
Rathna.
John W. Krahn
Mar 9 2005, 10:36 PM
Bob Showalter wrote:
QUOTE |
Bret Goodfellow wrote:
I am trying to evaluate a string, and determine if the last charater of the string has a backslash '' . The piece of code I am using doesn't appear to work. What I've found with this peice of code is that if the string does contain a , then the following code still adds another slash. If there is not backslash in the string, the the code appears to work fine by adding a backslash. Any ideas?
print "Deleting old directory $dir_no: $Dir_namen"; $arg_length = length($ARGV[0]); $arg_lastchar = substr($ARGV[0], $arg_length-1, 1);
Why did you switch from $Dir_name to $ARGV[0]?
Anyway, this can be simplified to
$arg_lastchar = substr($ARGV[0], -1);
print "last char is: $arg_lastcharn"; ################################################ # add a if one was not present in argument # ################################################ if ($arg_lastchar ne '\' or $arg_lastchar ne '/') {
Logic flaw. This will always be TRUE. (*any* character will either be not a backslash or not a forward slash.)
You want "and" (or "&&") instead of "or".
You can also use a regex and do the whole thing in one line:
$Dir_name .= '\' unless ($Dir_name =~ m![\/]z!);
|
Or even:
$Dir_name =~ s|(?<![\/])z|\|;
:-)
John
--
use Perl;
program
fulfillment
Tim Johnson
Mar 9 2005, 11:38 PM
Chances are the correct 8.3 filename you're looking for is Acroba~2, but
why are you using the old 8.3 filenames?
If you're just worried about spaces, try putting quotes in the command
you're sending to the system, like this:
system(""C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe"
C:\PDFs\test.pdf");
-----Original Message-----
From: Daniel Kasak [mailto:[Email Removed]]
Sent: Wednesday, March 09, 2005 3:32 PM
To: [Email Removed]
Subject: Execute application with a space in it's path
[Tim Johnson] <snip>
I can get to the 'Adobe' folder with the commands ( from the command
prompt ):
c:
cd c:Progra~1Adobe
However I can't get into the 'Acrobat 7.0' folder in the same way, ie:
c:
cd c:Progra~1AdobeAcroba~1
[Tim Johnson] <snip>
My code was going to be:
system("C:\Progra~1\Adobe\Acroba~1\Reader\AcroRd32
C:\PDFs\test.pdf");
[Tim Johnson] <snip>
Daniel Kasak
Mar 9 2005, 11:43 PM
Tim Johnson wrote:
QUOTE |
Chances are the correct 8.3 filename you're looking for is Acroba~2, but why are you using the old 8.3 filenames?
Because I don't know any better :) |
QUOTE |
If you're just worried about spaces, try putting quotes in the command you're sending to the system, like this:
system(""C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe" C:\PDFs\test.pdf");
That works. Thanks :) |
--
Daniel Kasak
IT Developer
NUS Consulting Group
Level 5, 77 Pacific Highway
North Sydney, NSW, Australia 2060
T: (+61) 2 9922-7676 / F: (+61) 2 9922 7989
email: [Email Removed]
website:
http://www.nusconsulting.com.au
Errin M HMMA/IT Larsen
Mar 10 2005, 03:06 PM
QUOTE |
-----Original Message----- From: Wiggins d'Anconia [mailto:[Email Removed]] Sent: Thursday, March 10, 2005 8:55 AM To: Marcos Rebelo Cc: Perl Beginners Subject: Re: Simplify perl -e '$a = [1,2,3,4,7]; print $a->[$#{@$a}]'
Marcos Rebelo wrote: This is correctly printing '7' but '$a->[$#{@$a}]' seems to be encripted code.
Can I write this in a cleaner way?
$a->[-1]; ???
Hi Wiggins, |
for those of us tryin' to keep up at home, can you walk us through
that bit a little?
Here's what I spot:
$a = [1,2,3,4,7] # this is initializing a scalar, $a, with a reference
to an array, [1,2,3,4,7]
# $a-> this is dereferencing the array
# as I understand it, and I really don't, the $#ARRAYNAME will give you
the number of elements, minus one, of an array?
# if that is the case, and then {@$a} ALSO derefernces the array, so
then
# $#{@$a} will be the number of elements in the array referenced by
$a, minus one (or, '4', in this example)
# so
print $a->[$#{@$a}]
# is equivelant to
print $a->[4]
# or, since $#{@$a} will always be the index of the last element of the
array:
print $a->[-1]
Did I get it right? That looks like homework to me ... Why would you
ever do that in a practical script?
--Errin
Wiggins d'Anconia
Mar 10 2005, 03:20 PM
Larsen, Errin M HMMA/IT wrote:
QUOTE |
-----Original Message----- From: Wiggins d'Anconia [mailto:[Email Removed]] Sent: Thursday, March 10, 2005 8:55 AM To: Marcos Rebelo Cc: Perl Beginners Subject: Re: Simplify perl -e '$a = [1,2,3,4,7]; print $a->[$#{@$a}]'
Marcos Rebelo wrote:
This is correctly printing '7' but '$a->[$#{@$a}]' seems to be encripted code.
Can I write this in a cleaner way?
$a->[-1]; ???
Hi Wiggins,
for those of us tryin' to keep up at home, can you walk us through that bit a little?
Here's what I spot:
$a = [1,2,3,4,7] # this is initializing a scalar, $a, with a reference to an array, [1,2,3,4,7]
# $a-> this is dereferencing the array # as I understand it, and I really don't, the $#ARRAYNAME will give you the number of elements, minus one, of an array? # if that is the case, and then {@$a} ALSO derefernces the array, so then # $#{@$a} will be the number of elements in the array referenced by $a, minus one (or, '4', in this example)
|
Actually I wasn't quite convinced about the $#{@$a} construct. Because
theoretically @$a has already dereferenced the array, but it appears
Perl will just do the right thing (amazing how it does that). It could
be written as $#{$a} or even!! $#$a, but I am not entirely sure where to
hunt in the docs to find that one, probably under the $# construct, but
not sure what that is called. possibly in perlvar but I don't have the
time to check, and I suspect John Krahn or somebody will chime in with
something brilliant :-).
QUOTE |
# so print $a->[$#{@$a}]
# is equivelant to print $a->[4]
# or, since $#{@$a} will always be the index of the last element of the array: print $a->[-1]
Did I get it right? That looks like homework to me ... Why would you ever do that in a practical script?
--Errin
|
I think you got it. Ever want the last item in a list, I can think of
lots of reasons to want that...
http://danconia.org
Errin M HMMA/IT Larsen
Mar 10 2005, 03:59 PM
<<SNIP>>
QUOTE |
# or, since $#{@$a} will always be the index of the last element of the array: print $a->[-1]
Did I get it right? That looks like homework to me ... Why would you ever do that in a practical script?
--Errin
I think you got it. Ever want the last item in a list, I can think of lots of reasons to want that...
http://danconia.org
|
Yeah. I agree. However, I always use the $ARRAY[-1] (or,
$ARRAYREF->[-1]) syntax to get at the last element.
Why would I ever type: $#{@$ARRAYREF} ?!?
Btw ... What perldoc can I read to read about '$#'?
--Errin
Wiggins d'Anconia
Mar 10 2005, 04:10 PM
Larsen, Errin M HMMA/IT wrote:
QUOTE |
<<SNIP
# or, since $#{@$a} will always be the index of the last
element of
the array: print $a->[-1]
Did I get it right? That looks like homework to me ... Why
would you
ever do that in a practical script?
--Errin
I think you got it. Ever want the last item in a list, I can think of lots of reasons to want that...
http://danconia.org
Yeah. I agree. However, I always use the $ARRAY[-1] (or, $ARRAYREF->[-1]) syntax to get at the last element.
Why would I ever type: $#{@$ARRAYREF} ?!?
|
Uh, I wouldn't either, apparently if you didn't know about the -1
capability like the OP :-). TMTOWTDI.
QUOTE |
Btw ... What perldoc can I read to read about '$#'?
|
Not sure, a quick glance at perldata discusses the -1 index usage, but
didn't turn up $# that I could see. Its in the books :-).
Jenda Krynicky
Mar 10 2005, 09:26 PM
From: "Larsen, Errin M HMMA/IT" <[Email Removed]>
QUOTE |
# $#{@$a} will be the number of elements in the array referenced by $a, minus one (or, '4', in this example)
|
Well, yes most likely it will be, but it doesn't have to ;-)
$#array is defined as the highest index in the array. The definition
doesn't say anything about the number of elements. And while it's
true that under normal circumstances Perl array indexes are zero
based and thus the maximum index is the number of elements minus one
it's not quaranteed.
#perl
$[ = 8;
@a = (1,2,3);
print "And the max index is: $#an";
You should not fiddle with $[ though. Unless you are creating an
entry into the obfuscated code contest or a YAPH.
Jenda
===== [Email Removed] ===
http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
Gerhard Meier
Mar 11 2005, 08:41 AM
On Thu, Mar 10, 2005 at 11:10:06AM -0500, Wiggins d'Anconia wrote:
QUOTE |
Btw ... What perldoc can I read to read about '$#'?
Not sure, a quick glance at perldata discusses the -1 index usage, but didn't turn up $# that I could see. Its in the books :-).
|
wiggim$ perldoc perldata | fgrep -c '$#'
6
/GM
FlashMX
Mar 11 2005, 02:34 PM
Hi,
I need to modify some existing code. The below line checks a file I'm reading in and does a search/replace based on the match. Currently I'm looking for...
if (/00000 set/) { ...etc.
Because the "000000" can change to different numbers (but always 6 figures which could include a period) how can I do a wildcard search?
For example I also need to search for ".00000". Or it could also be ".45306" or "123456" etc.
if (/WILDCARD set/) { ...etc.
Wiggins d'Anconia
Mar 11 2005, 02:44 PM
FlashMX wrote:
QUOTE |
Hi,
I need to modify some existing code. The below line checks a file I'm reading in and does a search/replace based on the match. Currently I'm looking for...
if (/00000 set/) { ...etc.
Because the "000000" can change to different numbers (but always 6 figures which could include a period) how can I do a wildcard search?
For example I also need to search for ".00000". Or it could also be ".45306" or "123456" etc.
if (/WILDCARD set/) { ...etc.
|
You can use a character class that includes the digits and a dot, and
match on it six times,
if (/[0-9.]{6} set/) {
HTH,
http://danconia.org
FlashMX
Mar 11 2005, 02:53 PM
Cool...that worked...thanks
I forgot to mention that one the match is found I do a search and replace
if (/[0-9.]{6} setgray/) {
s/.90000 set/-50.2 v n.90000 set/;
This is the issue. I need to "grab" the match number (whatever it is) and add it into the s/
So...
if (/[0-9.]{6} setgray/) {
s/NUMBER_FROM_ABOVE set/-50.2 v nNUMBER_FROM_ABOVE set/;
QUOTE |
On Fri, 11 Mar 2005 09:44:16 -0500, Wiggins d'Anconia wrote:
FlashMX wrote: Hi,
I need to modify some existing code. The below line checks a file I'm reading in and does a search/replace based on the match. Currently I'm looking for...
if (/00000 set/) { ...etc.
Because the "000000" can change to different numbers (but always 6 figures which could include a period) how can I do a wildcard search?
For example I also need to search for ".00000". Or it could also be ".45306" or "123456" etc.
if (/WILDCARD set/) { ...etc.
You can use a character class that includes the digits and a dot, and match on it six times,
if (/[0-9.]{6} set/) {
HTH,
http://danconia.org
-- To unsubscribe, e-mail: [Email Removed] For additional commands, e-mail: [Email Removed] <http://learn.perl.org/> <http://learn.perl.org/first-response
|
Wiggins d'Anconia
Mar 11 2005, 03:12 PM
Because it's up-side down.
Why is that?
It makes replies harder to read.
Why not?
Please don't top-post.
(Sherm Pendley, MacOSX List)
FlashMX wrote:
QUOTE |
Cool...that worked...thanks
I forgot to mention that one the match is found I do a search and replace
if (/[0-9.]{6} setgray/) { s/.90000 set/-50.2 v n.90000 set/;
This is the issue. I need to "grab" the match number (whatever it is) and add it into the s/
So...
if (/[0-9.]{6} setgray/) { s/NUMBER_FROM_ABOVE set/-50.2 v nNUMBER_FROM_ABOVE set/;
|
perldoc perlretut
perldoc perlre
You need to capture the value then...
if (/([0-9.]{6}) setgray/) {
s/$1 set/-50.2 v n$1 set/;
}
Which can be shortened more, but I would leave it readable. Capture
values with ()'s and then use those ordered matches by accessing, $1,
$2, $3, etc.
http://danconia.org
Renard
Mar 11 2005, 03:23 PM
I am running ActivePerl 5.8.6 on Windows XP.
I am unable to execute a statement like -- perl -e '<perl code>' --
I always get this response: Can't find string terminator "'" anywhere before
EOF at -e line 1.
Does not matter if I use (', ", or `) .. same response expect it can't find
the string terminator matching the used quotation mark.
What am I doing wrong?
Thank you for any assistance you may give me.