Re: just got around to posting this.. (long) SMTP TRACE
On 8/23/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: snip my $cfg = TTXData::get('CONFIG'); snip if ($cfg-get('smtptrace')) { snip open STDERR, ''.$cfg-get('basedir').'/smtptrace.txt'; snip my $s = Net::SMTP-new($smtphost, Debug = $cfg- get('smtptrace') ? 1:0); snip It looks like there is a config file somewhere that you can edit to turn on debugging. That config file also contains an entry for basedir which is where the debugging information will be written. Given this information from their website: How do I change the administration password for the setup.cgi script? The password is kept in the configuration file - ttxcfg.cgi. You will need editing the file using plain text editor like Notepad (Windows) or vi (Unix). Look for the line, which reads admpwd=password Please be advised that the file exists on your web server only. I am willing to bet that ttxcfg.cgi is the config file. You might also try looking in TTXData.pm for the get function to see if the name of the config file is hard coded there. All of that said, this is not a Perl question. This is a question about an application that happens to be written in Perl. If there is no community for this tool then perhaps you would be better off with a different tool such as RT (http://bestpractical.com/rt/) which not only has a large community ([EMAIL PROTECTED]), but also a book from O'Reilly (http://www.oreilly.com/catalog/rtessentials/index.html). -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
manipulating csv file fields through perl
Hi, I have a csv file. I wanted to do some calculations on some of its fields, like multiplying the 7th field with the 13th field and overwriting the 13th field with the answer of my calculation. Regarding this, can I do the calculations on the input file and overwrite it after calculating, or I will have to open the output file and write into it? I am asking this because I will have several files in the directory. So, I will have to read the directory using readdir, and process each file. It is better if I open the file in read-write mode, process it and overwrite the file. Just wanted to know if it is safe? Please guide on how to get started with this? Also can I do something like below:- while ($line=readline($IN_FILE)) { my @cdr=split (/,/, $line) ; $cdr[13] = $cdr[6]*5 ; ###Can I do something like this } Thanks, Mihir
Re: unfamiliar array reference
On Aug 18, 7:58 pm, [EMAIL PROTECTED] (Paul Lalli) wrote: snip Is that seriously from the book? UGH. Just declare $array_ref in the proper scope, and there's no need to take a reference to a dereference of the reference. while (my $array_ref = $sth1-fetchrow_arrayref) { push @stash, $array_ref; } Great - this worked fine. In this specific example, data is the sixth column in the SELECT query. You would therefore access it by $array_ref-[5]. No luck here, I could not retrieve anything (example code below). I would generally recommend you use the fetchrrow_hashref method rather than fetchrow_arrayref, so that you can refer to the column by name. This will prevent bugs in your code if you later change the query to add or remove columns. With fetchrow_hashref, the return value is a reference to a hash rather than to an array: while (my $hash_ref = $sth1-fetchrow_hashref()) { print $hash_ref-{data} . \n; } I was able to replicate this and also do a push to an array, but again, I could not retrieve rows. I agree with other posters - you should read: perldoc perlref perldoc perlreftut perldoc perllol perldoc perldsc Paul Lalli Thanks to all who suggested these perldocs - the perlreftut and perllol were especially helpful. I've learned more about references and also about scoping of variables - now I'm doing use strict and it took a while to make it work here. A side question: why is it necessary to declare our @variable more than once in a program? The main problem is that I still can't retrieve data at will. See below: First, as an array - I tried commented-out options one at a time. Msg is error message from Perl. $sth1-execute(); while (my $array_ref = $sth1-fetchrow_arrayref) { push ((our @stash), $array_ref); ##copy array contents } #use Data::Dumper 'Dumper'; #print Dumper our @stash ; #Msg: $VAR19586 = $VAR1; ###dump stash contents foreach my $array_ref (our @stash ) { # print Row: $array_ref-[5]\n; #Msg:Use of uninitialized value in concatenation # print $array_ref-[5]; #Msg: Use of uninitialized value in concatenation print Row: @$array_ref\n; #works, prints entire rows (from book) } foreach my $row (our @stash) { # print DATA: $row-[5]\n; #Msg: Use of uninitialized value in concatenation or string # print DATA: @$row-[5]\n; #Msg: Use of uninitialized value in join or string #} Now as a hash, which is what I would prefer, for the reasons stated by Paul. $sth1-execute(); while (my $hash_ref = $sth1-fetchrow_hashref()) { #print $hash_ref-{data} . \n; # works, prints data push our @stash, [ %$hash_ref ]; #works, copies hash contents } #use Data::Dumper 'Dumper'; #print Dumper our @stash; #showed good results (see below) foreach my $hash_ref (our @stash ) { #print Row: %$hash_ref\n; #Msg: Row: %ARRAY(0x13f88d44) #print Row: $hash_ref\n; #Msg: Row: %ARRAY(0x13f88d44) #print %$hash_ref . \n; #Msg: 'Can't coerce array into hash at' } from hash Data Dumper: $VAR790 = [ 'time_stamp', 'Mon Mar 05 2007 11:07:11', 'message_event', 'MESSAGE OUT', 'message_no', '12101589', 'data', ' MSH|^~\\|HUB|HOSP.MED..EDU|PM3.0|HOSP.MED..EDU|200 ',A|AA|9}|MESSAGE ACCEPTED|| 'message_index', '36314470' ]; $VAR791 = [ 'time_stamp', 'Mon Mar 05 2007 11:07:11', etc. What am I doing wrong? Thanks for all the help. Peter Link -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: manipulating csv file fields through perl
On 8/24/07, Jeff Pang [EMAIL PROTECTED] wrote: 2007/8/24, Mihir Kamdar [EMAIL PROTECTED]: $cdr[13] = $cdr[6]*5 ; ###Can I do something like this sure,why can't? Hi, Please look at my code below and comment. I am trying to manipulate 13th field of my record. But I am not getting the desired result in the output. The output file is the same as the input file. #!/usr/bin/perl use warnings ; my $file_path = '/home/user71/RangerDatasource/Customization/TelekomMalaysia/Scripts/Tests/cprogs/files/ratetest'; my $write_path = '/home/user71/RangerDatasource/Customization/TelekomMalaysia/Scripts/Tests/cprogs/files/rateop' ; my %times ; my $continue = 1; $SIG{INT} = $SIG{TERM} = sub { $continue = 0 }; while ($continue) { opendir my $dh, $file_path or die $!; while (my $file = readdir $dh) { my $fname = $file_path/$file ; next unless -f $fname; unless (exists $times{$file}){ my $line; open (my $IN_FILE,,$file_path/$file) or die $!. file not found ; while ($line=readline($IN_FILE)) { my @cdr=split (/,/, $line) ; $cdr[13] = $cdr[6]*5 ; $hash{@cdr[2,3,6,7]}=$line; } close $IN_FILE ; open (my $OUT_FILE,,$write_path/$file.out) or die $!; while (my($key, $value) = each %hash) { print $OUT_FILE $value; } close $OUT_FILE; } } closedir $dh ; } Thanks, Mihir
Re: entering regular expressions from the keyboard
Jay Savage schreef: Dr.Ruud: Christopher Spears: #print $regexp; Make that print qr/$regexp/; Not sure where your headed with this. My headed? :) It was an alternative for the commented debug line. First, OP wants to print the input back to the user. And I presume that it is more a developer directed print statement. -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Perl Courses
On Tue, 21 Aug 2007 17:02:23 -0400, John Arbes wrote: Does anyone have any recommendations on Perl Courses I believe Stonehenge are excellent (http://www.stonehenge.com/) - they certainly should be given their roster! -- Dan Otterburn [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: manipulating csv file fields through perl
2007/8/24, Mihir Kamdar [EMAIL PROTECTED]: $cdr[13] = $cdr[6]*5 ; ###Can I do something like this sure,why can't? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: global substitution with files
Gunnar Hjalmarsson schreef: my $content = do { local $/; $file }; That idiom uses an extra buffer, as big as the file. my $content; { local $/; $content = file } -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: manipulating csv file fields through perl
Your idea should work pretty well assuming that you are 100% sure that the thing in field 7 really is a number, you might get strange results if field 7 is empty or somehting else then a number. As for how to handle the files that is really up to you and the environment you work in... if your files are masive things of several hundreds of Mb each you might not want to place them completely in memory, if they are changed by other processes you might want to use some form of a locking scheme while handeling them. Etc, etc... On 8/24/07, Mihir Kamdar [EMAIL PROTECTED] wrote: Hi, I have a csv file. I wanted to do some calculations on some of its fields, like multiplying the 7th field with the 13th field and overwriting the 13th field with the answer of my calculation. Regarding this, can I do the calculations on the input file and overwrite it after calculating, or I will have to open the output file and write into it? I am asking this because I will have several files in the directory. So, I will have to read the directory using readdir, and process each file. It is better if I open the file in read-write mode, process it and overwrite the file. Just wanted to know if it is safe? Please guide on how to get started with this? Also can I do something like below:- while ($line=readline($IN_FILE)) { my @cdr=split (/,/, $line) ; $cdr[13] = $cdr[6]*5 ; ###Can I do something like this } Thanks, Mihir
Re: manipulating csv file fields through perl
Of course it is Your %hash gets filled with a structure that looks like this: { KEY = VALUE } { @cdr[2,3,6,7]= $line} Then you take all the values and write them out to a file. Since you never changed the $line variable you should get the same result in your out file as you had in the in file. It is good to see perl still works. Try the following just before the line: $hash{@cdr[2,3,6,7]}=$line; $line = join(/,/,@cdr); This way you fill the $line variable with the contents of the just modified array this should get you the desired result (though there are more stylish ways of writting it it will get you the result you are looking for) Rob. On 8/24/07, Mihir Kamdar [EMAIL PROTECTED] wrote: On 8/24/07, Jeff Pang [EMAIL PROTECTED] wrote: 2007/8/24, Mihir Kamdar [EMAIL PROTECTED]: $cdr[13] = $cdr[6]*5 ; ###Can I do something like this sure,why can't? Hi, Please look at my code below and comment. I am trying to manipulate 13th field of my record. But I am not getting the desired result in the output. The output file is the same as the input file. #!/usr/bin/perl use warnings ; my $file_path = '/home/user71/RangerDatasource/Customization/TelekomMalaysia/Scripts/Tests/cprogs/files/ratetest'; my $write_path = '/home/user71/RangerDatasource/Customization/TelekomMalaysia/Scripts/Tests/cprogs/files/rateop' ; my %times ; my $continue = 1; $SIG{INT} = $SIG{TERM} = sub { $continue = 0 }; while ($continue) { opendir my $dh, $file_path or die $!; while (my $file = readdir $dh) { my $fname = $file_path/$file ; next unless -f $fname; unless (exists $times{$file}){ my $line; open (my $IN_FILE,,$file_path/$file) or die $!. file not found ; while ($line=readline($IN_FILE)) { my @cdr=split (/,/, $line) ; $cdr[13] = $cdr[6]*5 ; $hash{@cdr[2,3,6,7]}=$line; } close $IN_FILE ; open (my $OUT_FILE,,$write_path/$file.out) or die $!; while (my($key, $value) = each %hash) { print $OUT_FILE $value; } close $OUT_FILE; } } closedir $dh ; } Thanks, Mihir
Re: manipulating csv file fields through perl
2007/8/24, Mihir Kamdar [EMAIL PROTECTED]: $cdr[13] = $cdr[6]*5 ; $hash{@cdr[2,3,6,7]}=$line; Hello, I checked the codes,but I'm not sure what's the meanings of those 2 lines above. I can't see the logic relation with the 2 lines. Yes $cdr[13] = $cdr[6]*5 has changed the array element,but didn't change the line content.Are you sure this? Also as I've said, @[EMAIL PROTECTED] is different with $hash{@arr}.see below, $ perl -Mstrict -Mwarnings -MData::Dumper -e 'my %hash ;@hash{qw/a b c/} = 111;print Dumper \%hash' $VAR1 = { 'c' = undef, 'a' = 111, 'b' = undef }; $ perl -Mstrict -Mwarnings -MData::Dumper -e 'my %hash ;$hash{qw/a b c/} = 111;print Dumper \%hash' $VAR1 = { 'abc' = 111 }; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Perl Courses
24 Aug 2007 00:21:09 -, Dan Otterburn [EMAIL PROTECTED]: On Tue, 21 Aug 2007 17:02:23 -0400, John Arbes wrote: Does anyone have any recommendations on Perl Courses I believe Stonehenge are excellent (http://www.stonehenge.com/) - they certainly should be given their roster! Yes some Stonehenge teachers are on this list like Tom Phoenix,Randal etc.From their replied messages you could also see they are a great Perl training organization. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: manipulating csv file fields through perl
Hi, Thanks...i got my mistake...it worked after the below as Rob suggested:- my @cdr=split (/,/, $line) ; $cdr[13] = $cdr[6]*5.0 ; $line = join(,,@cdr); $hash{@cdr[2,3,6,7]}=$line; But a few improvizations. $cdr[13] that I am trying to manipulate is a amount field and should be a floating point value...how can I print it as floating point in perl? Also I am reading from an input file and writing to an output file. Is it possible to read and write to the same file without any side-effects?? @Jeff:- As you said that @[EMAIL PROTECTED] is different with $hash{@arr}, I am sorry but I am not sure what you are trying to point out through this. Can you please explain at a very basic level so that I can understand how it works and how it affects my code? Thanks, Mihir
Re: manipulating csv file fields through perl
2007/8/24, Mihir Kamdar [EMAIL PROTECTED]: But a few improvizations. $cdr[13] that I am trying to manipulate is a amount field and should be a floating point value...how can I print it as floating point in perl? Perl isn't a strong type language,it doesn't mind which data type it used. You can use printf() for printing a floating vlaue,like, $ perl -e 'printf(%.2f,3)' 3.00 Also I am reading from an input file and writing to an output file. Is it possible to read and write to the same file without any side-effects?? Yes,just open the file for reading and handling,store the results to an array as you did,close the file.then re-open the file for writing,and write the content in the array to the file.It's no problem. @Jeff:- As you said that @[EMAIL PROTECTED] is different with $hash{@arr}, I am sorry but I am not sure what you are trying to point out through this. @[EMAIL PROTECTED] means this hash has more than one keys (given the @arr has 3 elements,then this hash has 3 keys,each key is one of this array's elements). $hash{@arr} means the hash has only one key,it's the same as: $key = join '',@arr; $hash{$key} = ...; Also @[EMAIL PROTECTED] is a hash slice,but $hash{@arr} isn't. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Trying to dynamically create arrays, getting can't use string as ARRAY ref
On Aug 23, 2:00 am, [EMAIL PROTECTED] (Lists User) wrote: *snip* my %grouplist; open HD,'groups.txt' or die $!; while(HD) { chomp; my @tmp = split; my $groupname = shift @tmp; $grouplist{$groupname} = [EMAIL PROTECTED];} close HD; OK, thanks! The Llama presents hashes as single value to a key, so I never thought to make a hash of arrays. Here is what I am using, and I think my comments are correct on how it works: while (CONFIG) { # Remove any extra carriage returns chomp; # for each line of the config file, place the entries # into a temp array my @temp = split; # pull the first entry, which is the group name, and put it into # the variable $groupname to be used when creating the 'master' list my $groupname = shift @temp; # Create the hash named grouplist. Each key is the value of $groupname. The values # of the key are what is left of the line we are working with contained in # the array @temp $grouplist{$groupname} = [EMAIL PROTECTED]; } # Close the file handle on the config file close CONFIG; Do you know how to loop through a hash?If not,please write back to the list. Yes and no. I did some searching and found the following. I think I understand most of it, I'm just not 100% clear on the 'for my' loops. Or should I say that I understand them enough to lift the code from the example and make it work with my script, but don't fully understand the logic behind it. :-) for my $foo ( keys %grouplist ){ *directory check/create code* # File Pull loop # This creates a temp variable named i. It is used as a counter # to tell the loop which entry in the array to pull the vru # number from. The $# gets the number of the last value of the # referenced array and uses it as the number of times to run # through the loop as well as the index for the array. for my $i ( 0 .. $#{ $grouplist{$groupname} } ) { *file pull code* } } Thank you everyone, this has been very helpful! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Trying to dynamically create arrays, getting can't use string as ARRAY ref
On Aug 23, 3:42 am, [EMAIL PROTECTED] (Paul Lalli) wrote: On Aug 23, 2:00 am, [EMAIL PROTECTED] (Justin The Cynical) wrote: *snip* Can't use string (prod01) as an ARRAY ref while strict refs in use perldoc -q variable name Ah, OK, thanks. I've got a bit of reading to do. :-) Am I missing something really basic that is covered in the Llama (3rd edition), No, because it's not basic. You should be using a multi-dimensional structure, as Lists User recommended. Rather than storing the name of an array, store the name of a key to your hash of arrays, and let the value be a reference to an anonymous array. Oh good, I'm not missing something obvious then. I have redone the code and posted it for comprehension purposes. And after testing the script, it does seem to run a bit quicker than the original using arrays alone. *snip* See also: perldoc perlreftut perldoc perllol perldoc perldsc Hope that helps, Paul Lalli It does, thank you again Paul. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
How to create a package level variable
I need to have a package level variable. For that I am using the following method: package XYZ; $XYZ::db_initialised = 0; sub init_db() { DB::init() unless $XYZ::db_initialized; $XYZ::db_initialized = 1; } or package XYZ; my $db_initialised = 0; sub init_db() { DB::init() unless $db_initialized; $db_initialized = 1; } Method 1 works fine and the database is initialized only once (DB::init() is other module used by me for all db ops) Method 2 fails and tries to initialize database again... I can't use method 1 as per company standards... and I can't understand the reason why method 2 is failing... -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: How to create a package level variable
2007/8/24, Sundeep [EMAIL PROTECTED]: I need to have a package level variable. For that I am using the following method: package XYZ; $XYZ::db_initialised = 0; sub init_db() { DB::init() unless $XYZ::db_initialized; $XYZ::db_initialized = 1; } or package XYZ; my $db_initialised = 0; sub init_db() { DB::init() unless $db_initialized; $db_initialized = 1; } Method 1 works fine and the database is initialized only once (DB::init() is other module used by me for all db ops) Method 2 fails and tries to initialize database again... I can't use method 1 as per company standards... and I can't understand the reason why method 2 is failing... I'm not sure why you said it try to initialize database again (each time?). But in method2 you made a closure,when calling method2 from out of package XYZ,the value of $db_initialized would be kept,and looks like it's a real package variable. see this test, $ cat XYZ.pm package XYZ; my $db_initialised = 0; sub init_db() { $db_initialized ++; print $db_initialized,\n; } 1; $ cat xyz.pl use strict; use XYZ; XYZ::init_db; XYZ::init_db; XYZ::init_db; $ perl xyz.pl 1 2 3 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: manipulating csv file fields through perl
Perl isn't a strong type language,it doesn't mind which data type it used. You can use printf() for printing a floating vlaue,like, $ perl -e 'printf(%.2f,3)' 3.00 that's right, but with respect to this particular code of mine, the $cdr[13] is the amount field which should be a float. I am manipulating it like this:- while ($line=readline($IN_FILE)) { my @cdr=split (/,/, $line) ; $cdr[13] = $cdr[6]*5.0 ; $line = join(,,@cdr); $hash{@cdr[2,3,6,7]}=$line; } and then writing the output like this:- open (my $OUT_FILE,,$write_path/$file.out) or die $!; while (my($key, $value) = each %hash) { print $OUT_FILE $value; } Here if I want the $cdr[13] to be written as a float(ex. 15.00), then how do I write it? Thanks, Mihir
Re: Trying to dynamically create arrays, getting can't use string as ARRAY ref
2007/8/24, Justin The Cynical [EMAIL PROTECTED]: On Aug 23, 2:00 am, [EMAIL PROTECTED] (Lists User) wrote: *snip* my %grouplist; open HD,'groups.txt' or die $!; while(HD) { chomp; my @tmp = split; my $groupname = shift @tmp; $grouplist{$groupname} = [EMAIL PROTECTED];} close HD; Do you know how to loop through a hash?If not,please write back to the list. Yes and no. Hi, practicalperl's solution is right.He/she created a correct datastru for you. This is the general way to loop through that hash, for my $key (keys %grouplist) { print groupname: , $key,\n; print group content: , @{$grouplist{$key}} ,\n; } Because $grouplist{$key} is an anonymous array,so you need to dereference it,saying @{ $grouplist{$key}} would do that. All you need is to read 'perldoc perldata'. Good luck! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: manipulating csv file fields through perl
2007/8/24, Mihir Kamdar [EMAIL PROTECTED]: Here if I want the $cdr[13] to be written as a float(ex. 15.00), then how do I write it? $cdr[13] = sprintf %.2f, $cdr[6]*5.0 ; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: How to create a package level variable
Thanks Jeff, It works fine now. The problem was that this module (XYZ) module uses another module, which again has the use statement to load XYZ. I don't know if the perl loads the module again and might be this caused to reset the value back to 0. When I commented the use statement of that module, it just works fine. Thanks, Sundeep On 8/24/07, Jeff Pang [EMAIL PROTECTED] wrote: 2007/8/24, Sundeep [EMAIL PROTECTED]: I need to have a package level variable. For that I am using the following method: package XYZ; $XYZ::db_initialised = 0; sub init_db() { DB::init() unless $XYZ::db_initialized; $XYZ::db_initialized = 1; } or package XYZ; my $db_initialised = 0; sub init_db() { DB::init() unless $db_initialized; $db_initialized = 1; } Method 1 works fine and the database is initialized only once (DB::init() is other module used by me for all db ops) Method 2 fails and tries to initialize database again... I can't use method 1 as per company standards... and I can't understand the reason why method 2 is failing... I'm not sure why you said it try to initialize database again (each time?). But in method2 you made a closure,when calling method2 from out of package XYZ,the value of $db_initialized would be kept,and looks like it's a real package variable. see this test, $ cat XYZ.pm package XYZ; my $db_initialised = 0; sub init_db() { $db_initialized ++; print $db_initialized,\n; } 1; $ cat xyz.pl use strict; use XYZ; XYZ::init_db; XYZ::init_db; XYZ::init_db; $ perl xyz.pl 1 2 3 -- ~!~ Sun ~!~ { Its simple to be happy. But its difficult to be simple}
Re: How to create a package level variable
On Aug 24, 2007, at 11:58 AM, Sundeep Gupta wrote: The problem was that this module (XYZ) module uses another module, which again has the use statement to load XYZ. I don't know if the perl loads the module again and might be this caused to reset the value back to 0. When I commented the use statement of that module, it just works fine. If you load a module with use(), it will only get loaded once, no matter how many use()s for that module are executed throughout the source tree. In particular the package variable is not reset: [EMAIL PROTECTED]:~/tmp$ cat Foo.pm use strict; package Foo; our $x = 0; 1; [EMAIL PROTECTED]:~/tmp$ perl -MFoo -wle '$Foo::x = 1; use Foo; print $Foo::x' 1 -- fxn -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Net:SFTP Configuration
Hi Chas, I was not given the proper arguments to the *new* function. That is why, it was failed to connect. This time I have another trouble. As part of my code, I have used the below to statments to list the files in the directory. my @ls = $sftp-ls($srcDir/); print \n-@ls\n; But I am not getting the output eventhough the directory got some files except --- part. sometimes I am getting the output as below: - HASH(0xd03334) HASH(0xd033dc) HASH(0xd03498) HASH(0xd03540) HASH(0xd035e8) HASH(0xd03690) HASH(0xd03738) HASH(0xd037e0) HASH(0xd044a4) HASH(0xd0454c) HASH(0xd045f4) HASH(0xd0469c) HASH(0xd04744) HASH(0xd047ec) HASH(0xd05da0) HASH(0xd05dac) HASH(0xd05e24) HASH(0xd05e9c) HASH(0xd05f50) HASH(0xd0601c) HASH(0xd060c4) HASH(0xd0616c) HASH(0xd06214) HASH(0xd062bc) HASH(0xd06364) HASH(0xcdbc20) HASH(0xcdbcc8) HASH(0xcdbd70) HASH(0xcdbe18) HASH(0xcdbec0) HASH(0xcdbf68) I am not sure about the return value of the $sftp-ls(). I thought it will return a list as in Net::FTP. How I should use it? Please help me. Thanks, Rajeev Kilaru On 8/16/07, Chas Owens [EMAIL PROTECTED] wrote: On 8/16/07, kilaru rajeev [EMAIL PROTECTED] wrote: snip Another common problem is that different versions of Unix (and Linux) use different implementations of ssh. This isn't a problem for most things, but they tend to use different formats for the private and public key files. You may have to convert the key generated on one platform to the format expected by another. snip I had generated the Key pair. I installed the public key on the server. And, while generating the keys I had not passed any passphrase. That is why, it is not asking for the password/passphrase while connecting. I guess we need to provide the configuration details in our program while connecting to the server. snip If you did not provide a passphrase then the key is unencrypted. Depending on what you want to do, this is a good thing. It is common for program accounts (as opposed to user accounts) to have unencrypted keys since it removes the complications of have to use ssh-agent. However, your response did not answer the primary question: can you connect to the other box from the command line without typing a password? If you can and Perl is having a problem doing the same then there is something wrong with the installation of Net::SCP, if you can't then the problem is most likely incompatible key types (since you have ruled out encrypted keys as a problem).
Re: connecting to Oracle
[EMAIL PROTECTED] (Octavian Rasnita) writes: Hi, I want to make a program that connects to a remote Oracle database and then make it a .exe Windows executable. Is it possible to make it not depend on Oracle's client? Install Oracle Instant Client, build DBD::Oracle and then try which libraries are necessary to include in your exe program via pp (Perl Packager). -- Radek -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Get names of files ONLY using module Net::SFTP::Foreign
Hi all, I am a newbie in this forum and Perl too ;-) I am trying writing script transfering files using module Net::SFTP::Foreign. I need to retrieve from remote directory only names of files (not directories, links,etc.). Something like this doesn't work: -- use strict; use warnings; use Net::SFTP::Foreign; my $remote_dir = /tmp; my $sftp = Net::SFTP::Foreign-new(host); $sftp-error and print SSH connection failed: . $sftp-error . \n; $sftp-setcwd($remote_dir); my $DH = $sftp-opendir($remote_dir); while ($_ = $sftp-readdir($DH)) { print $_-{filename} \n if -f $_-{filename}; } --- Script above write out nothing, but if last line is this print $_-{filename} \n; It writes out names of files, directories, but I need only names of files. How can I get it easy? Thanks. Marian
Re: unfamiliar array reference
On Aug 23, 7:13 pm, [EMAIL PROTECTED] wrote: On Aug 18, 7:58 pm, [EMAIL PROTECTED] (Paul Lalli) wrote: I've learned more about references and also about scoping of variables - now I'm doing use strict and it took a while to make it work here. A side question: why is it necessary to declare our @variable more than once in a program? 'our', like 'my', is lexically scoped. Its effects are terminated when the innermost enclosing block ends. So if you're using the same package variable in two different blocks, you have to use 'our' in each of them: package Foo; { our $bar; #for the duration of this scope, you can use $Foo::bar by just saying $bar } #now scope of 'our' has ended, must fully qualify $Foo::bar again { #new scope, must still say $Foo::bar our $bar; #Can now call $Foo:: bar as just $bar again } The main problem is that I still can't retrieve data at will. See below: First, as an array - I tried commented-out options one at a time. Msg is error message from Perl. $sth1-execute(); while (my $array_ref = $sth1-fetchrow_arrayref) { push ((our @stash), $array_ref); ##copy array contents } #use Data::Dumper 'Dumper'; #print Dumper our @stash ; #Msg: $VAR19586 = $VAR1; Your syntax for 'our' is confusing at best. Just declare it once, at the very top of your program. From the output however, it looks like you have one of the older versions of DBI that Randal mentioned in his post. That is, you cannot do what I suggested. You have to make an explicit copy. my @stash; #there's no reason this has to be 'our' to begin with. while (my $array_ref = $sth1-fetchrow_arrayref) { push @stash, [ @{$array_ref} ]; } print Dumper([EMAIL PROTECTED]); ###dump stash contents foreach my $array_ref (our @stash ) { Again. Stop using 'our' all over the place. Declare your variables once, at the beginning of your program, if it's being used throughout the program. # print Row: $array_ref-[5]\n; #Msg:Use of uninitialized value in concatenation # print $array_ref-[5]; #Msg: Use of uninitialized value in concatenation print Row: @$array_ref\n; #works, prints entire rows (from book) And what is the output of that? If $array_ref-[5] is undefined, then you do not have six elements in your result arrays, which means you're using a different query than the one you originally posted, and you'll have to adjust your code accordingly. Now as a hash, which is what I would prefer, for the reasons stated by Paul. $sth1-execute(); while (my $hash_ref = $sth1-fetchrow_hashref()) { #print $hash_ref-{data} . \n; # works, prints data push our @stash, [ %$hash_ref ]; #works, copies hash contents I wouldn't say works here. I would say nothing more than doesn't generate compiler errors. This doesn't do what you think it does. This creates a reference to an *array* that contains the keys and values of your hash. You don't want to do that. You want to create a reference to a *hash*. To do that, we use { } instead of [ ] push @stash, { %{$hash_ref} }; } #use Data::Dumper 'Dumper'; #print Dumper our @stash; #showed good results (see below) foreach my $hash_ref (our @stash ) { #print Row: %$hash_ref\n; #Msg: Row: %ARRAY(0x13f88d44) #print Row: $hash_ref\n; #Msg: Row: %ARRAY(0x13f88d44) #print %$hash_ref . \n; #Msg: 'Can't coerce array into hash at' Right. All these messages are because you put array refs into @stash rather than hash refs. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Get names of files ONLY using module Net::SFTP::Foreign
On Friday 24 August 2007 08:57:26 Marian Bednar wrote: Hi all, I am a newbie in this forum and Perl too ;-) I am trying writing script transfering files using module Net::SFTP::Foreign. I need to retrieve from remote directory only names of files (not directories, links,etc.). Something like this doesn't work: -- use strict; use warnings; use Net::SFTP::Foreign; my $remote_dir = /tmp; my $sftp = Net::SFTP::Foreign-new(host); $sftp-error and print SSH connection failed: . $sftp-error . \n; $sftp-setcwd($remote_dir); my $DH = $sftp-opendir($remote_dir); while ($_ = $sftp-readdir($DH)) { print $_-{filename} \n if -f $_-{filename}; } --- Script above write out nothing, but if last line is this print $_-{filename} \n; It writes out names of files, directories, but I need only names of files. How can I get it easy? Thanks. Marian You cannot use -f. You'll need to stat the file and check the mode. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: just got around to posting this.. (long) SMTP TRACE
[EMAIL PROTECTED] wrote: #1. I am not a SIR I am a Miss #2 I was told I could post this...im my other post I had said I didn't have any experience with Perl, I didn't have any classes in college and my boss gave me a side project to do. I'm trying to avoid paying 60 dollars an hour for troubleshooting. It is recommended that you that advance questions to another forum. I suggest the Perl Monks at http://www.perlmonks.org/?node=Seekers%20of%20Perl%20Wisdom -- Just my 0.0002 million dollars worth, Shawn For the things we have to learn before we can do them, we learn by doing them. Aristotle If you Terrans are comprehensible, you don't understand them. Great Fang Talphon -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: global substitution with files
Dr.Ruud wrote: Gunnar Hjalmarsson schreef: my $content = do { local $/; $file }; That idiom uses an extra buffer, as big as the file. my $content; { local $/; $content = file } Does it? In that case, why is it mentioned at http://faq.perl.org/perlfaq5.html#How_can_I_read_in_an -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: unfamiliar array reference
Paul Lalli wrote: 'our', like 'my', is lexically scoped. Its effects are terminated when the innermost enclosing block ends. So if you're using the same package variable in two different blocks, you have to use 'our' in each of them: 'our' is not lexically scoped; it is package scoped. Once you declare a variable using 'our' you may use it anywhere in the package. Think of it as an alias to the fully-qualified variable. A variable declare via 'our' in one package is independent of a variable of the same name in another package. See `perldoc -f our` for more details. #!/usr/bin/perl use strict; use warnings; package Foo; our $bar = 'Foo\'s bar'; # $bar is an alias to $Foo::bar print $bar\n; sub bar { print Foo::bar sub: $bar\n; } package main; our $bar = 'main\'s bar'; # $bar is an alias to $main::bar or $::bar; print $bar\n; print $Foo::bar\n; print $main::bar\n; print $::bar\n; Foo::bar(); __END__ -- Just my 0.0002 million dollars worth, Shawn For the things we have to learn before we can do them, we learn by doing them. Aristotle If you think Terrans are comprehensible, you don't understand them. Great Fang Talphon -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: unfamiliar array reference
On 8/24/07, Paul Lalli [EMAIL PROTECTED] wrote: snip 'our', like 'my', is lexically scoped. Its effects are terminated when the innermost enclosing block ends. So if you're using the same package variable in two different blocks, you have to use 'our' in each of them: snip Your syntax for 'our' is confusing at best. Just declare it once, at the very top of your program. snip Again. Stop using 'our' all over the place. Declare your variables once, at the beginning of your program, if it's being used throughout the program. snip I can't say that I agree with the advice to declare all of your our variables once at the top of your program. To my way of thinking this defeats the purpose of having the our function*. I have a once-per-function rule for our; if a function needs to use a global variable, then it has to request it. This means that when you come back to the code a year later, and are looking at just that one function, you can easily see that the variable is special (i.e. used by many functions). That said, the code you were referring to was definitely overusing our. It doesn't need to be attached to every reference to the variable. * yes, the variable is still visible to all packages in the same file without having to be fully qualified, but I consider that aspect of our a misfeature. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Net:SFTP Configuration
On 8/24/07, kilaru rajeev [EMAIL PROTECTED] wrote: Hi Chas, I was not given the proper arguments to the *new* function. That is why, it was failed to connect. This time I have another trouble. As part of my code, I have used the below to statments to list the files in the directory. my @ls = $sftp-ls($srcDir/); print \n-@ls\n; But I am not getting the output eventhough the directory got some files except --- part. sometimes I am getting the output as below: - HASH(0xd03334) HASH(0xd033dc) HASH(0xd03498) HASH(0xd03540) HASH(0xd035e8) HASH(0xd03690) HASH(0xd03738) HASH(0xd037e0) HASH(0xd044a4) HASH(0xd0454c) HASH(0xd045f4) HASH(0xd0469c) HASH(0xd04744) HASH(0xd047ec) HASH(0xd05da0) HASH(0xd05dac) HASH(0xd05e24) HASH(0xd05e9c) HASH(0xd05f50) HASH(0xd0601c) HASH(0xd060c4) HASH(0xd0616c) HASH(0xd06214) HASH(0xd062bc) HASH(0xd06364) HASH(0xcdbc20) HASH(0xcdbcc8) HASH(0xcdbd70) HASH(0xcdbe18) HASH(0xcdbec0) HASH(0xcdbf68) I am not sure about the return value of the $sftp-ls(). I thought it will return a list as in Net::FTP. How I should use it? Please help me. from the docs: $sftp-ls($remote [, $subref ]) Fetches a directory listing of $remote. If $subref is specified, for each entry in the directory, $subref will be called and given a reference to a hash with three keys: filename, the name of the entry in the directory listing; longname, an entry in a long listing like ls -l; and a, a Net::SFTP::Attributes object, which contains the file attributes of the entry (atime, mtime, permissions, etc.). If $subref is not specified, returns a list of directory entries, each of which is a reference to a hash as described in the previous paragraph. So if you only want the filenames you can say my @ls = map { $_-{filename} } $sftp-ls($srcDir); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: problem with regex in a while loop.
On 8/24/07, Pat Rice [EMAIL PROTECTED] wrote: open (DATA, $data_file) or die can't open $data_file $!; my @array_of_data = DATA; That reads the entire file into the array. while ($line = DATA) That goes back to try to read more, but you're already at end-of-file. So it won't find anything. If you want to re-read the file, you could re-open it first, since that resets the read position to the start of the file. Or you could move the position directly by using seek(). But you probably don't need to re-read the file, since you have the data in memory. Change the while loop to a foreach loop and you can avoid doing the I/O twice: foreach my $line (@array_of_data) { Hope this helps! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
problem with regex in a while loop.
Hi all, I'm trying to get the follwoing to work: what I am aiming to do is the following: 1. Read a file 2. Regex the df file and look for the line which contains Used. Problem: - part 1 reads the file, confirmed, because I output this to the screen - Part 2 Is where the problem is, as I connot get in to the while loop while ($line = DATA) I think the syntax is ok, could some one confirm this Code below. Thanks in Advance Pat #!/usr/bin/perl use strict; use warnings; my $data_file = 'df.'; print $data_file \n; # Open the file for reading. open (DATA, $data_file) or die can't open $data_file $!; my @array_of_data = DATA; print this is the file: \n; print @array_of_data; print \n; #chomp($line); my $line; while ($line = DATA) { # any action here will be applied to each line of the file. print in While loop \n; # chomp($line); foreach my $line (@array_of_data) { # Start an if statement, the condition of which is # If this particular line contains the word dangerous. if ($line =~ m/ Used/i) { # If the line contains dangerous then print the line out. print This line contains the word dangerous: $line\n; } # End the if condition here. } # End the foreach loop here. } close (DATA);
Re: How to create a package level variable
On 8/23/07, Sundeep [EMAIL PROTECTED] wrote: $XYZ::db_initialised = 0; sub init_db() { DB::init() unless $XYZ::db_initialized; $XYZ::db_initialized = 1; } Do you spell it initialised or initialized? Cheers! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: How to create a package level variable
Tom Phoenix wrote: Do you spell it initialised or initialized? Yes. british-english: initialise american-english: initialize -- Just my 0.0002 million dollars worth, Shawn For the things we have to learn before we can do them, we learn by doing them. Aristotle If you think Terrans are comprehensible, you don't understand them. Great Fang Talphon -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Net:SFTP Configuration
Hi Chas, It is perfect. Thanks a lot. Regards, Rajeev Kilaru On 8/24/07, Chas Owens [EMAIL PROTECTED] wrote: On 8/24/07, kilaru rajeev [EMAIL PROTECTED] wrote: Hi Chas, I was not given the proper arguments to the *new* function. That is why, it was failed to connect. This time I have another trouble. As part of my code, I have used the below to statments to list the files in the directory. my @ls = $sftp-ls($srcDir/); print \n-@ls\n; But I am not getting the output eventhough the directory got some files except --- part. sometimes I am getting the output as below: - HASH(0xd03334) HASH(0xd033dc) HASH(0xd03498) HASH(0xd03540) HASH(0xd035e8) HASH(0xd03690) HASH(0xd03738) HASH(0xd037e0) HASH(0xd044a4) HASH(0xd0454c) HASH(0xd045f4) HASH(0xd0469c) HASH(0xd04744) HASH(0xd047ec) HASH(0xd05da0) HASH(0xd05dac) HASH(0xd05e24) HASH(0xd05e9c) HASH(0xd05f50) HASH(0xd0601c) HASH(0xd060c4) HASH(0xd0616c) HASH(0xd06214) HASH(0xd062bc) HASH(0xd06364) HASH(0xcdbc20) HASH(0xcdbcc8) HASH(0xcdbd70) HASH(0xcdbe18) HASH(0xcdbec0) HASH(0xcdbf68) I am not sure about the return value of the $sftp-ls(). I thought it will return a list as in Net::FTP. How I should use it? Please help me. from the docs: $sftp-ls($remote [, $subref ]) Fetches a directory listing of $remote. If $subref is specified, for each entry in the directory, $subref will be called and given a reference to a hash with three keys: filename, the name of the entry in the directory listing; longname, an entry in a long listing like ls -l; and a, a Net::SFTP::Attributes object, which contains the file attributes of the entry (atime, mtime, permissions, etc.). If $subref is not specified, returns a list of directory entries, each of which is a reference to a hash as described in the previous paragraph. So if you only want the filenames you can say my @ls = map { $_-{filename} } $sftp-ls($srcDir);
Re: Get names of files ONLY using module Net::SFTP::Foreign
On 8/24/07, Marian Bednar [EMAIL PROTECTED] wrote: snip print $_-{filename} \n if -f $_-{filename}; snip Script above write out nothing, but if last line is this print $_-{filename} \n; It writes out names of files, directories, but I need only names of files. How can I get it easy? snip either of these should work. print $_-{filename}\n unless $_-{longname} =~ /^d/; print $_-{filename}\n unless $_-{a}-perm() 16384; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: How to create a package level variable
On 8/24/07, Mr. Shawn H. Corey [EMAIL PROTECTED] wrote: Tom Phoenix wrote: Do you spell it initialised or initialized? Yes. british-english: initialise american-english: initialize snip Yes, but in Perl you have to choose one and stick to it. The original code example was: package XYZ; $XYZ::db_initialised = 0; sub init_db() { DB::init() unless $XYZ::db_initialized; $XYZ::db_initialized = 1; } or package XYZ; my $db_initialised = 0; sub init_db() { DB::init() unless $db_initialized; $db_initialized = 1; } Unless you have some funky source filter installed that normalizes spelling variants Perl is going to have a problem. This is why the strict pragma is so important. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: unfamiliar array reference
On 8/24/07, Mr. Shawn H. Corey [EMAIL PROTECTED] wrote: Paul Lalli wrote: 'our', like 'my', is lexically scoped. Its effects are terminated when the innermost enclosing block ends. So if you're using the same package variable in two different blocks, you have to use 'our' in each of them: 'our' is not lexically scoped; it is package scoped. Once you declare a variable using 'our' you may use it anywhere in the package. Think of it as an alias to the fully-qualified variable. A variable declare via 'our' in one package is independent of a variable of the same name in another package. Nope, it is lexically scoped. from perldoc -f our our associates a simple name with a package variable in the current package for use within the current scope. When use strict 'vars' is in effect, our lets you use declared global variables without qualifying them with package names, within the lexical scope of the our declaration. In this way our differs from use vars , which is package scoped. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: unfamiliar array reference
On Aug 24, 9:37 am, [EMAIL PROTECTED] (Chas Owens) wrote: On 8/24/07, Paul Lalli [EMAIL PROTECTED] wrote: snip 'our', like 'my', is lexically scoped. Its effects are terminated when the innermost enclosing block ends. So if you're using the same package variable in two different blocks, you have to use 'our' in each of them: snip Your syntax for 'our' is confusing at best. Just declare it once, at the very top of your program. snip Again. Stop using 'our' all over the place. Declare your variables once, at the beginning of your program, if it's being used throughout the program. snip I can't say that I agree with the advice to declare all of your our variables once at the top of your program. To my way of thinking this defeats the purpose of having the our function*. I have a once-per-function rule for our; if a function needs to use a global variable, then it has to request it. I tend to agree. However, this poster was not using functions. He was simply declaring 'our' every time he saw the variable. That's why I told him to move the declaration of 'our' to the top. The reason he had to keep declaring 'our' is that he was only declaring it in the insanely small scope of the actual expression. There is no need for that. Just declare it before the big block of code that uses it. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: unfamiliar array reference
On Aug 24, 9:55 am, [EMAIL PROTECTED] (Mr. Shawn H. Corey) wrote: Paul Lalli wrote: 'our', like 'my', is lexically scoped. Its effects are terminated when the innermost enclosing block ends. So if you're using the same package variable in two different blocks, you have to use 'our' in each of them: 'our' is not lexically scoped; it is package scoped. No. Package variables are package scoped. 'our' is lexically scoped. The effects of 'our' are gone as soon as the lexical scope in which it was used are gone. The package variable, of course, remains. Once you declare a variable using 'our' you may use it anywhere in the package. Incorrect. Once you declare a variable using 'our', you may use its shorthand name anywhere in the lexical scope of that declaration. You can ALWAYS use the fully-qualified name anywhere, regardless of strict, regardless of 'our'. $ perl -le' use strict; $main::foo = hello world; print $main::foo; { our $foo; print $foo; } print $foo; ' Variable $foo is not imported at -e line 9. Global symbol $foo requires explicit package name at -e line 9. Execution of -e aborted due to compilation errors. See `perldoc -f our` for more details. You may wish to read that yourself... perldoc -f our An our declaration declares a global variable that will be visible across its entire lexical scope, even across package boundaries. #!/usr/bin/perl use strict; use warnings; package Foo; our $bar = 'Foo\'s bar'; # $bar is an alias to $Foo::bar Yes, but only for the LEXICAL SCOPE, which in this case is the entire file, since there was no enclosing block. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Trying to dynamically create arrays, getting can't use string as ARRAY ref
Justin == Justin The Cynical [EMAIL PROTECTED] writes: Justin The Llama presents hashes as single value to a key, so I never thought Justin to make a hash of arrays. That's because (a) an arrayref is still a single value, so we haven't really lied, so much as just simplified to what can be handled in the first 30 hours with Perl, and (b) you *still* can't make a hash of arrays, just a hash of arrayrefs, and that distinction is *important*. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 [EMAIL PROTECTED] 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! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Perl Courses
Jeff == Jeff Pang [EMAIL PROTECTED] writes: Jeff Yes some Stonehenge teachers are on this list like Tom Phoenix,Randal Jeff etc.From their replied messages you could also see they are a great Jeff Perl training organization. *blush* -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 [EMAIL PROTECTED] 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! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: aggregating time steps
On Aug 23, 2007, at 11:17 PM, Gunnar Hjalmarsson wrote: Kirk Wythers wrote: I don't see how $totals{$year}{$month}{count} ++; is holding the count. Read about the auto-increment operator in perldoc perlop. OK. I'll try and be more clear to the degree of my ignorance. First, I do not understand the use of $totals in both the sum of the scalar tmax, and the incremented count. # store totals by month as: $totals{$year}{$month}{tmax} += $tmax; ... # keep track of the count $totals{$year}{$month}{count} ++; I think what I am confused by is the relationship between the scalar $tmax and the (whatever you call it) $totals{$year}{$month}{tmax} Therefore, if I want the sum of tmax, I'm not sure what is holding the total of tmax. Is it $tmax, or $totals{$year}{$month}{tmax}? The same goes for count. What is the holding the count? I guess I'm just used to the very simple $ as defining the scalar. SImilarly, if I want the average of $tmax, then I would want to divide $totals{$year}{$month}{tmax} by $totals{$year}{$month}{count} Thank you Kirk -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: $/ variable trouble
On 8/24/07, Yoyoyo Yoyoyoyo [EMAIL PROTECTED] wrote: Hi all, I am using the diamond operator to redirect input to my perl script. I want to copy all of the input on to a single variable using the following subroutine: sub getfile # Copies the file redirected to this perl script to the varialbe $x { my $x; local $/ = undef; $x = ; return $x; } The problem is that when I try to print what the above subroutine returns, the program kindof hangs. That is, it doesn't just print the output and then send me back to the command line, instead I have to press Ctrl-c to break out of it, like it is expecting input from the keyboard. Any help is appreciated, Robert The Perl operator, , reads from STDIN. To pipe a file to STDIN, you need to run the script like so: script.pl file_to_pipe Please note the '' If you do this: script.pl file_to_read the filename gets put onto $ARGV[0]. You can open the file inside Perl like so (untested code): die Please run like: $0 filename\n if (not -f $ARGV[0]); open $filehandle, $ARGV[0]; my $x; local $/ = undef; $x = $filehandle; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
$/ variable trouble
Hi all, I am using the diamond operator to redirect input to my perl script. I want to copy all of the input on to a single variable using the following subroutine: sub getfile # Copies the file redirected to this perl script to the varialbe $x { my $x; local $/ = undef; $x = ; return $x; } The problem is that when I try to print what the above subroutine returns, the program kindof hangs. That is, it doesn't just print the output and then send me back to the command line, instead I have to press Ctrl-c to break out of it, like it is expecting input from the keyboard. Any help is appreciated, Robert - Luggage? GPS? Comic books? Check out fitting gifts for grads at Yahoo! Search.
Re: $/ variable trouble
Yoyoyo Yoyoyoyo wrote: Hi all, I am using the diamond operator to redirect input to my perl script. I want to copy all of the input on to a single variable using the following subroutine: sub getfile # Copies the file redirected to this perl script to the varialbe $x { my $x; local $/ = undef; $x = ; return $x; } The problem is that when I try to print what the above subroutine returns, the program kindof hangs. That is, it doesn't just print the output and then send me back to the command line, instead I have to press Ctrl-c to break out of it, like it is expecting input from the keyboard. If you're reading from STDIN, you have to send an end-of-file marker. In Windows, press control-Z on an empty line. In *NIX, control-D. -- Just my 0.0002 million dollars worth, Shawn For the things we have to learn before we can do them, we learn by doing them. Aristotle If you think Terrans are comprehensible, you don't understand them. Great Fang Talphon -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: aggregating time steps
On 8/24/07, Kirk Wythers [EMAIL PROTECTED] wrote: On Aug 23, 2007, at 11:17 PM, Gunnar Hjalmarsson wrote: Kirk Wythers wrote: I don't see how $totals{$year}{$month}{count} ++; is holding the count. Read about the auto-increment operator in perldoc perlop. OK. I'll try and be more clear to the degree of my ignorance. First, I do not understand the use of $totals in both the sum of the scalar tmax, and the incremented count. snip I think what I am confused by is the relationship between the scalar $tmax and the (whatever you call it) $totals{$year}{$month}{tmax} snip The $tmax variable is the current value the fourth field for each line you read. The name of the totals variable is %totals. It is a hash. A hash is like an array, but instead of using numbers as offsets it uses strings as keys (it is also unordered). Elements in the hash are accessed by giving the hash a key like this: my %hash = ( foo = 10; bar = 20; ); my $scalar = $hash{foo}; #scalar is now 10 Hashes can only contain scalar values, but a reference to a hash (or an array) is a scalar, so Perl mimics multi-dimensional data structures using references. You can create hash references in many ways. One common way is to use an anonymous hash reference: my $hashref = { foo = 10, bar = 20 }; You can get at the values stored in a hash ref with the arrow operator: my $scalar = $hashref-{bar}; #$scalar is now 20 So if I want a multi-dimensional hash I can declare it like this: my %hash = ( foo = $hashref bar = { foo = 30, bar = 40 } ); I could get the first level like this my $ref = $hash{foo}; #$ref is now a reference to $hashref and the second like this my $scalar = $ref-{bar}; #$scalar is now 20 You could also do it more directly like this my $scalar $hash{foo}-{bar}; #$scalar is now 20 Now, this happens often enough and is fairly unambiguous, so Perl has some syntactic sugar that lets drop the arrow operator between {} and [] operators. So you could write the last example more clearly as my $scalar $hash{foo}{bar}; #$scalar is now 20 In addition, Perl also has something called auto-vivification that causes hash references to auto-magically come into existence when needed, so I can say my %hash; $hash{foo}{bar} = 10; and the hash reference needed to store the second level will auto-magically come into existence. This is how $total{$year}{$month}{tmax} += $tmax; works. It is creating a three level deep hash and storing the totals in the last level. snip Therefore, if I want the sum of tmax, I'm not sure what is holding the total of tmax. Is it $tmax, or $totals{$year}{$month}{tmax}? The same goes for count. What is the holding the count? snip After the loop finishes the sum of all of the tmax fields for a given year and month will be in $total{$year}{$month}{tmax} where $year is the year you want and $month is the month you want. I guess I'm just used to the very simple $ as defining the scalar. $ does is the sigil associated with scalar values, which is why, in Perl 5, you also see them when referring single element (which is a scalar) in an array or hash (you see @ when referring to multiple values, as my solution did frequently). snip SImilarly, if I want the average of $tmax, then I would want to divide $totals{$year}{$month}{tmax} by $totals{$year}{$month}{count} snip Yes. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: aggregating time steps
Kirk Wythers schreef: Gunnar Hjalmarsson: Kirk Wythers: I don't see how $totals{$year}{$month}{count} ++; is holding the count. Read about the auto-increment operator in perldoc perlop. OK. I'll try and be more clear to the degree of my ignorance. First, I do not understand the use of $totals in both the sum of the scalar tmax, and the incremented count. # store totals by month as: $totals{$year}{$month}{tmax} += $tmax; ... # keep track of the count $totals{$year}{$month}{count} ++; I think what I am confused by is the relationship between the scalar $tmax and the (whatever you call it) $totals{$year}{$month}{tmax} Therefore, if I want the sum of tmax, I'm not sure what is holding the total of tmax. Is it $tmax, or $totals{$year}{$month}{tmax}? The same goes for count. What is the holding the count? I guess I'm just used to the very simple $ as defining the scalar. SImilarly, if I want the average of $tmax, then I would want to divide $totals{$year}{$month}{tmax} by $totals{$year}{$month}{count} Put use Data::Dumper; print Dumper $totals; at the end of the code. -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: global substitution with files
Gunnar Hjalmarsson schreef: Dr.Ruud: Gunnar Hjalmarsson: my $content = do { local $/; $file }; That idiom uses an extra buffer, as big as the file. my $content; { local $/; $content = file } Does it? In that case, why is it mentioned at http://faq.perl.org/perlfaq5.html#How_can_I_read_in_an Because it looks and reads nice? But probably you mean I should have submitted a bug report. Well, this idiom gets discussed a few times per year in news:comp.lang.perl.misc and still many people stick to the wasteful way, because in practice it often doesn't matter that much. Now find the problem in the example code in `perldoc -f alarm`. -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: $/ variable trouble
Yoyoyo Yoyoyoyo wrote: Is there anyway to add the End of file marker to the variable before I print it, so I don't have to do ctrl-d? The control-D is part of the shell, not Perl. You didn't say so but from your message I assume that this did the trick. In other words, you are not redirecting the file as you think you are; your program is still trying to read STDIN. -- Just my 0.0002 million dollars worth, Shawn For the things we have to learn before we can do them, we learn by doing them. Aristotle If you think Terrans are comprehensible, you don't understand them. Great Fang Talphon -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: aggregating time steps
On 8/24/07, Dr.Ruud [EMAIL PROTECTED] wrote: Kirk Wythers schreef: Gunnar Hjalmarsson: Kirk Wythers: I don't see how $totals{$year}{$month}{count} ++; is holding the count. Read about the auto-increment operator in perldoc perlop. OK. I'll try and be more clear to the degree of my ignorance. First, I do not understand the use of $totals in both the sum of the scalar tmax, and the incremented count. # store totals by month as: $totals{$year}{$month}{tmax} += $tmax; ... # keep track of the count $totals{$year}{$month}{count} ++; I think what I am confused by is the relationship between the scalar $tmax and the (whatever you call it) $totals{$year}{$month}{tmax} Therefore, if I want the sum of tmax, I'm not sure what is holding the total of tmax. Is it $tmax, or $totals{$year}{$month}{tmax}? The same goes for count. What is the holding the count? I guess I'm just used to the very simple $ as defining the scalar. SImilarly, if I want the average of $tmax, then I would want to divide $totals{$year}{$month}{tmax} by $totals{$year}{$month}{count} Put use Data::Dumper; print Dumper $totals; at the end of the code. snip Shouldn't that be print Dumper \%totals; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: How to create a package level variable
Chas Owens schreef: [$db_initiali.ed] Unless you have some funky source filter installed that normalizes spelling variants Perl is going to have a problem. Yes, I like the idea: use autocorect; (which of course corrects that line last, to signal that it's done) -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: aggregating time steps
Thank you very much for the explanation Chas. It is starting to make more sense. The reason I was attracted to the solution #! /usr/bin/perl -w use strict; use warnings; $, = ' '; # set output field separator $\ = \n; # set output record separator my ( $year, $month, $doy, $tmax, $tmin, $par, $precip, $NH4, $NO3, $O3, $CO2, $V1, $V2, $V3, $V4 ) = split; # store totals by month as: $totals{$year}{$month}{tmax} += $tmax; ... # store the count $totals{$year}{$month}{count} ++; # After you read all the input, you can calculate the averages. Is that is seems to allow for easy re-arranging of output. For example I can envision the need to change precip to a monthly total (the sum), instead of the monthly average. On Aug 24, 2007, at 12:52 PM, Chas Owens wrote: The name of the totals variable is %totals. It is a hash. This is part of what was confusing me. I didn't see %totals declared anywhere is the suggestion. A hash is like an array, but instead of using numbers as offsets it uses strings as keys (it is also unordered). Elements in the hash are accessed by giving the hash a key like this: my %hash = ( foo = 10; bar = 20; ); my $scalar = $hash{foo}; #scalar is now 10 Now, this happens often enough and is fairly unambiguous, so Perl has some syntactic sugar that lets drop the arrow operator between {} and [] operators. So you could write the last example more clearly as my $scalar $hash{foo}{bar}; #$scalar is now 20 In addition, Perl also has something called auto-vivification that causes hash references to auto-magically come into existence when needed, so I can say my %hash; Then, should I be declaring my%totals right up top and treating the variables like this? #! /usr/bin/perl -w use strict; use warnings; $, = ' '; # set output field separator $\ = \n; # set output record separator my%totals; my ( $year, $month, $doy, $tmax, $tmin, $par, $precip, $NH4, $NO3, $O3, $CO2, $V1, $V2, $V3, $V4 ) = split; # store totals by month as: $totals{$year}{$month}{doy} += $doy; $totals{$year}{$month}{tmax} += $tmax; $totals{$year}{$month}{tmin} += $tmin; $totals{$year}{$month}{par} += $par; $totals{$year}{$month}{precip} += $precip; etc # store the count $totals{$year}{$month}{count} ++; print $year, $totals{$year}{$month}{doy} / $totals{$year}{$month} {count}, $totals{$year}{$month}{tmax} / $totals{$year}{$month} {count}, $totals{$year}{$month}{tmin} / $totals{$year}{$month}{count, etc Kirk $hash{foo}{bar} = 10; and the hash reference needed to store the second level will auto-magically come into existence. This is how $total{$year}{$month}{tmax} += $tmax; works. It is creating a three level deep hash and storing the totals in the last level. snip Therefore, if I want the sum of tmax, I'm not sure what is holding the total of tmax. Is it $tmax, or $totals{$year}{$month}{tmax}? The same goes for count. What is the holding the count? snip After the loop finishes the sum of all of the tmax fields for a given year and month will be in $total{$year}{$month}{tmax} where $year is the year you want and $month is the month you want. I guess I'm just used to the very simple $ as defining the scalar. $ does is the sigil associated with scalar values, which is why, in Perl 5, you also see them when referring single element (which is a scalar) in an array or hash (you see @ when referring to multiple values, as my solution did frequently). snip SImilarly, if I want the average of $tmax, then I would want to divide $totals{$year}{$month}{tmax} by $totals{$year}{$month}{count} snip Yes. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
foreach and map..how are they different?
Aside from the syntax, is there a difference in the way 'map' and 'foreach' process? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: How to create a package level variable
On Aug 24, 2007, at 8:13 PM, Dr.Ruud wrote: Chas Owens schreef: [$db_initiali.ed] Unless you have some funky source filter installed that normalizes spelling variants Perl is going to have a problem. Yes, I like the idea: use autocorect; I think there's an Acme:: module that did something like that, it used some heuristic (edit distance or whatever) and chose an already existing indentifier. Something like that. But I can't find it. -- fxn -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: aggregating time steps
Chas Owens schreef: Dr.Ruud: [my %totals;] Put print Dumper $totals; at the end of the code. Shouldn't that be print Dumper \%totals; Yes, thanks for the correction. -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: aggregating time steps
On 8/24/07, Kirk Wythers [EMAIL PROTECTED] wrote: snip This is part of what was confusing me. I didn't see %totals declared anywhere is the suggestion. snip Yeah, you can only tell it is a hash because of how it is used. I believe the person who posted the example expected you to know what a hash was and how to declare it. snip Then, should I be declaring my%totals right up top and treating the variables like this? #! /usr/bin/perl -w use strict; use warnings; $, = ' '; # set output field separator $\ = \n; # set output record separator my%totals; my ( $year, $month, $doy, $tmax, $tmin, $par, $precip, $NH4, $NO3, $O3, $CO2, $V1, $V2, $V3, $V4 ) = split; # store totals by month as: $totals{$year}{$month}{doy} += $doy; $totals{$year}{$month}{tmax} += $tmax; $totals{$year}{$month}{tmin} += $tmin; $totals{$year}{$month}{par} += $par; $totals{$year}{$month}{precip} += $precip; etc # store the count $totals{$year}{$month}{count} ++; print $year, $totals{$year}{$month}{doy} / $totals{$year}{$month} {count}, $totals{$year}{$month}{tmax} / $totals{$year}{$month} {count}, $totals{$year}{$month}{tmin} / $totals{$year}{$month}{count, etc snip Yes, assuming that the += code is inside a loop that reads lines from a file. Also, I am incredibly lazy and the thought of typing all of that code gives me the shivers, so I would say #!/usr/bin/perl use strict; use warnings; my @sum = qwtmax tmin par precip NH4 NO3 O3 CO2 V1 V2 V3 V4; my %totals; while () { chomp; my %rec; my ($y, $m, $doy); #put year, month, and doy in $y, $m, and $doy and the rest in %rec ($y, $m, $doy, @[EMAIL PROTECTED]) = split; $totals{$y}{$m}{$_} += $rec{$_} for @sum; $totals{$y}{$m}{count}++; } for my $y (sort keys %totals) { for my $m (sort keys %{$totals{$y}}) { my $rec = $totals{$y}{$m}; my @avg = map { $rec-{$_}/$rec-{count} } @sum; print join(\t, $y, $m, @avg), \n; } } If I decided later that some columns should be summed and others averaged then I could change it to #!/usr/bin/perl use strict; use warnings; #fields to sum my @sum = qwtmax tmin par precip NH4 NO3 O3 CO2 V1 V2 V3 V4; #fields to average my @avg = qwtmax tmin par NH4 NO3 O3 CO2 V1 V2 V3 V4; my %totals; while () { chomp; my %rec; my ($y, $m, $doy); #put year, month, and doy in $y, $m, and $doy and the rest in %rec ($y, $m, $doy, @[EMAIL PROTECTED]) = split; $totals{$y}{$m}{$_} += $rec{$_} for @sum; $totals{$y}{$m}{count}++; } for my $y (sort keys %totals) { for my $m (sort keys %{$totals{$y}}) { my %rec; #make a copy of the data so totals can be used for other things @[EMAIL PROTECTED], 'count'} = @[EMAIL PROTECTED],'count'}; #average only the fields in @avg @[EMAIL PROTECTED] = map { $rec{$_}/$rec{count} } @avg; print join(\t, $y, $m, @[EMAIL PROTECTED]), \n; } } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: How to create a package level variable
Xavier Noria wrote: On Aug 24, 2007, at 8:13 PM, Dr.Ruud wrote: Chas Owens schreef: [$db_initiali.ed] Unless you have some funky source filter installed that normalizes spelling variants Perl is going to have a problem. Yes, I like the idea: use autocorect; I think there's an Acme:: module that did something like that, it used some heuristic (edit distance or whatever) and chose an already existing indentifier. Something like that. But I can't find it. -- fxn Actually, it was I problem I encountered on an international project. The British team spelled if initialise and the American, initialize. We could fix it by search and replace but since we didn't have control of either source, we had to redo the sr every time we got an update. And neither team would do it since they had other, more important items to finish first. Sadly, the project was in C :( In Perl, we could have used: *initialise = \$initialize; -- Just my 0.0002 million dollars worth, Shawn For the things we have to learn before we can do them, we learn by doing them. Aristotle If you think Terrans are comprehensible, you don't understand them. Great Fang Talphon -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: foreach and map..how are they different?
Dan == Dan Sopher [EMAIL PROTECTED] writes: Dan Aside from the syntax, is there a difference in the way 'map' and Dan 'foreach' process? Yes. map is an expression. foreach is a statement. foreach can't be nested inside a larger expression. map is *meant* to do that, and using it in a void context is generally frowned upon. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 [EMAIL PROTECTED] 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! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: foreach and map..how are they different?
On Aug 24, 2:21 pm, [EMAIL PROTECTED] (Dan Sopher) wrote: Aside from the syntax, is there a difference in the way 'map' and 'foreach' process? foreach is just a loop. map returns values. Specifically, it returns the value of the BLOCK/EXPR for each iteration of the loop. my @doubles = map { $_ * 2 } @nums; is exactly equivalent to: my @doubles; foreach (@nums) { push @doubles, $_ * 2; } Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: $/ variable trouble
On Aug 24, 1:12 pm, [EMAIL PROTECTED] (Yoyoyo Yoyoyoyo) wrote: Hi all, I am using the diamond operator to redirect input to my perl script. I want to copy all of the input on to a single variable using the following subroutine: sub getfile # Copies the file redirected to this perl script to the varialbe $x { my $x; local $/ = undef; $x = ; return $x; } The problem is that when I try to print what the above subroutine returns, the program kindof hangs. That is, it doesn't just print the output and then send me back to the command line, instead I have to press Ctrl-c to break out of it, like it is expecting input from the keyboard. I'm willing to bet you're calling your script by doing: $ ./file.pl text.txt when you should be doing: $ ./file.pl text.txt That is, let read from the command line argument, rather than having it read from STDIN and redirecting the process's STDIN to be from this file. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: How to create a package level variable
On 8/24/07, Xavier Noria [EMAIL PROTECTED] wrote: On Aug 24, 2007, at 8:13 PM, Dr.Ruud wrote: Chas Owens schreef: [$db_initiali.ed] Unless you have some funky source filter installed that normalizes spelling variants Perl is going to have a problem. Yes, I like the idea: use autocorect; I think there's an Acme:: module that did something like that, it used some heuristic (edit distance or whatever) and chose an already existing indentifier. Something like that. But I can't find it. I remember something about it as well (using a map of UK-US and Levenshtein distance to catch typos), but can't find anything in CPAN either. Thankfully Damian is busy with Perl 6 or he might write it. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: How to create a package level variable
On 8/24/07, Mr. Shawn H. Corey [EMAIL PROTECTED] wrote: Xavier Noria wrote: On Aug 24, 2007, at 8:13 PM, Dr.Ruud wrote: Chas Owens schreef: [$db_initiali.ed] Unless you have some funky source filter installed that normalizes spelling variants Perl is going to have a problem. Yes, I like the idea: use autocorect; I think there's an Acme:: module that did something like that, it used some heuristic (edit distance or whatever) and chose an already existing indentifier. Something like that. But I can't find it. -- fxn Actually, it was I problem I encountered on an international project. The British team spelled if initialise and the American, initialize. We could fix it by search and replace but since we didn't have control of either source, we had to redo the sr every time we got an update. And neither team would do it since they had other, more important items to finish first. Sadly, the project was in C :( In Perl, we could have used: *initialise = \$initialize; snip That would only fix global variables (and even then only if it came after the globals were declared). Lexical variables would still be broken, not to mention hash keys. If a simple search and replace was all you needed then a source filter would probably work best (i.e. %80 of the time). For instance, should this be translated? my $key = colour; It depends on how it is used later: my $colour = $property{$key}; #yes, I think. my $word = $uk_to_us{$key}; #no and there is no way (baring strong AI*) a program can make that decision for you. * and if we get strong AI, well UK vs US word differences will be the least of our problems. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: foreach and map..how are they different?
On 8/24/07, Dan Sopher [EMAIL PROTECTED] wrote: Aside from the syntax, is there a difference in the way 'map' and 'foreach' process? snip There are many differences, in addition to what has already been said map can be more or less cpu efficient than depending on the task*. If you can use map to avoid a temporary array, then it can save you some time, but if you need the intermediate result, then for is faster. Personally, I prefer map (and its cousin grep) for things that can be done in one line: my @files = map { $_-{filename} } $sftp-ls(); rather than my @files; push @files, $_-{filename} for $sftp-ls(); But if the code being passed to map becomes to complex, I prefer a for loop: my @headers; for my $filename (@filenames) { open my $file, , $filename or die could not open $filename:$!; push @headers, scalar $file; } rather than my @headers = map { open my $f, , $_ or die could not open $_:$!; scalar $_; } @filenames; * Here are the benchmarks for a two different scenarios Map vs for vs map with temp variable Rate map_with_var for map map_with_var 3.51/s -- -11% -57% for 3.92/s 12% -- -52% map 8.11/s 131% 107% -- #!/usr/bin/perl use strict; use warnings; use Benchmark; my @a = 1 .. 1_000_000; my %subs = ( for = sub { my @b; push @b, ($_ + 4) for @a; return @b; }, map = sub { return map { $_ + 4 } @a; }, map_with_var = sub { my @b = map { $_ + 4 } @a; return @b; } ); Benchmark::cmpthese(-1, \%subs); Map of Map vs Two for loops vs Map of Map with temp variable vs map of map with two temp variables: Rate map_with_2var for map_with_var map map_with_2var 1793/s-- -8% -16% -45% for 1950/s9%-- -8% -40% map_with_var 2124/s 18%9%-- -35% map 3258/s 82% 67% 53%-- #!/usr/bin/perl use strict; use warnings; use Benchmark; my @a = 1 .. 1_000; my %subs = ( for = sub { my @b; push @b, ($_ + 4) for @a; my @c; push @c, ($_ + 4) for @b; return @b; }, map = sub { return map { $_ + 4 } map { $_ + 4 } @a; }, map_with_var = sub { my @b = map { $_ + 4 } map { $_ + 4 } @a; return @b; }, map_with_2var = sub { my @b = map { $_ + 4 } @a; my @c = map { $_ + 4 } @b; return @c; } ); Benchmark::cmpthese(-1, \%subs); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: foreach and map..how are they different?
Dan Sopher wrote: Aside from the syntax, is there a difference in the way 'map' and 'foreach' process? Hi Dan Internally they're very similar, but you shouldn't be thinking like that. As Randal said, foreach is a statement - a language construct like 'if', 'while', 'else' and so on - while map is an expression - more specifically a function. Use foreach if you want to execute a block of Perl code for every element in a list. Use map to implement a /mapping/ between two lists. It takes an input list and a statement or a block specifying a transformation, and (in list context) returns the list with that transformation applied to each element. Conceptually there is no loop - the entire list is transformed at once, and a scalar expression like $a = 2 * $b corresponds exactly to the list expression @a = map 2 * $_, @b HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: aggregating time steps
Kirk Wythers wrote: Sorry for the not sure where to even begin nature of this email, but I am stuck. I am trying to put together a aggregating script that takes daily climate data and produces monthly averages. For example, the input file has the form: year monthdoytmax tmin parprecipNH4NO3O3 CO2V1V2V3V4 1949110-2.78440.16000000 000 [ SNIP ] 1949236-2.78-12.22489.3100000 0000 I need to step through 50 years of data, returning a single line of monthly averages. SOmething like: yearmonthdoytmaxtminparprecipNH4 NO3O3CO2V1V2V3V4 19491155.45-3.13461.3 00000000 19492614.61.24212.1 00000000 $ echo year monthdoytmax tmin parprecipNH4NO3O3 CO2V1V2V3V4 1949110-2.78440.1600000000 0 1949120.56-6.67501.210000000 00 194913-1.11-9.44374.20000000 00 1949141.67-3.89180.040.0300000 000 1949153.89-11.11225.070.3300000 000 1949163.33-11.67190.570000000 00 1949178.891.67358.30000000 00 1949182.78-1.11447.570000000 00 1949191.11-6.67185.170000000 00 1949110-6.11-8.33402.950.0300000 000 1949111-2.78-7.78438.610000000 00 1949112-2.22-6.67197.340000000 00 19491130-3.89194.770000000 00 1949114-1.11-3.89428.670000000 00 19491155.56-1.11196.740.6100000 000 19491165.56-6.67198.010.0500000 000 1949117-5.56-7.78403.660.100000 000 1949118-3.89-11.67205.60.300000 000 1949119-3.89-16.11217.640.3300000 000 1949120-10-16.67477.620.0300000 000 1949121-1.67-10213.430.1300000 000 1949122-6.11-10.56514.380.0300000 000 1949123-3.33-8.33221.181.1400000 000 1949124-1.11-6.67222.150.3300000 000 1949125-6.67-11.11366.210.0800000 000 1949126-3.89-11.11353.450.100000 000 1949127-1.67-6.67230.490000000 00 1949128-2.78-14.44369.511.7300000 000 1949129-13.33-19.44324.27000000 000 1949130-6.11-18.33479.95000000 000 1949131-6.11-16.67455.250.0300000 000 1949232-6.67-15508.90000000 00 1949233-10.56-18.89709.76000000 000 1949234-6.11-13.89253.290.0500000 000 1949235-3.33-7.224110.28000000 00 1949236-2.78-12.22489.31000000 000 | perl -ne' next unless /^\d/; # skip headers my ( $year, $mon, $day, @fields ) = split; my $key = sprintf %04d%02d, $year, $mon; for ( 0 .. $#fields ) { $data{ $key }{ total }[ $_ ] += $fields[ $_ ]; } $data{ $key }{ count }++; END { for my $key ( sort keys %data ) { print join( \t, unpack( a4a2, $key ), map $_ ? sprintf( %.2f, $_ / $data{ $key }{ count } ) : 0, @{ $data{ $key }{ total } } ), \n; } } ' 194901 -1.81 -8.89 323.04 0.170 0 0 0 0 0 0 0 194902 -5.89 -13.44 474.45 0.070 0 0 0 0 0 0 0 John -- Perl isn't a
Re: global substitution with files
Dr.Ruud wrote: Gunnar Hjalmarsson schreef: Dr.Ruud: Gunnar Hjalmarsson: my $content = do { local $/; $file }; That idiom uses an extra buffer, as big as the file. my $content; { local $/; $content = file } Does it? In that case, why is it mentioned at http://faq.perl.org/perlfaq5.html#How_can_I_read_in_an Because it looks and reads nice? But probably you mean I should have submitted a bug report. No, I rather got defensive... Actually, I find it annoying that an idiom suggested in the FAQ has such a drawback, especially when it can easily be avoided. -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Get names of files ONLY using module Net::SFTP::Foreign
Stephen Kratzer wrote: On Friday 24 August 2007 08:57:26 Marian Bednar wrote: I am a newbie in this forum and Perl too ;-) I am trying writing script transfering files using module Net::SFTP::Foreign. I need to retrieve from remote directory only names of files (not directories, links,etc.). Something like this doesn't work: -- use strict; use warnings; use Net::SFTP::Foreign; my $remote_dir = /tmp; my $sftp = Net::SFTP::Foreign-new(host); $sftp-error and print SSH connection failed: . $sftp-error . \n; $sftp-setcwd($remote_dir); my $DH = $sftp-opendir($remote_dir); while ($_ = $sftp-readdir($DH)) { print $_-{filename} \n if -f $_-{filename}; } --- Script above write out nothing, but if last line is this print $_-{filename} \n; It writes out names of files, directories, but I need only names of files. How can I get it easy? You cannot use -f. You'll need to stat the file and check the mode. -f *does* stat the file. The reason that you can't use -f (or stat) is because $_-{filename} is not on a locally mounted file system. 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: How to create a package level variable
2007/8/24, Mr. Shawn H. Corey [EMAIL PROTECTED]: Tom Phoenix wrote: Do you spell it initialised or initialized? Yes. british-english: initialise american-english: initialize But why not just spell it as init?for us both initialise and initialize are complicated,I can't remember and differ them at all. :) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: foreach and map..how are they different?
Both map and foreach can do the same thing,though they're used in different syntax environment. I follow a rule,if you need to return a result list,use map.Otherwise use for/foreach. Also sometime we can use map to do some flexible translation,like Schwartz Translation. 2007/8/25, Dan Sopher [EMAIL PROTECTED]: Aside from the syntax, is there a difference in the way 'map' and 'foreach' process? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Convert from unix newlines to dos newlines
Hi all, I use a mac and I was wondering if there was a way to convert unix newlines in a text file to dos newlines. Robert - Boardwalk for $500? In 2007? Ha! Play Monopoly Here and Now (it's updated for today's economy) at Yahoo! Games.
Re: Convert from unix newlines to dos newlines
the simple way is to use shell command 'unix2dos'. $ unix2dos file.txt 2007/8/25, Yoyoyo Yoyoyoyo [EMAIL PROTECTED]: Hi all, I use a mac and I was wondering if there was a way to convert unix newlines in a text file to dos newlines. Robert - Boardwalk for $500? In 2007? Ha! Play Monopoly Here and Now (it's updated for today's economy) at Yahoo! Games. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: foreach and map..how are they different?
Jeff == Jeff Pang [EMAIL PROTECTED] writes: Jeff Also sometime we can use map to do some flexible translation,like Jeff Schwartz Translation. That's a new one for me. Are you trying to say Schwartzian Transform? -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 [EMAIL PROTECTED] 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! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: foreach and map..how are they different?
2007/8/25, Randal L. Schwartz [EMAIL PROTECTED]: Jeff == Jeff Pang [EMAIL PROTECTED] writes: Jeff Also sometime we can use map to do some flexible translation,like Jeff Schwartz Translation. That's a new one for me. Are you trying to say Schwartzian Transform? Yes,sorry for spelling wrong.You're right.:) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: global substitution with files
Dr.Ruud wrote: Gunnar Hjalmarsson schreef: my $content = do { local $/; $file }; That idiom uses an extra buffer, as big as the file. my $content; { local $/; $content = file } Or: read( FH, my $content, -s FH ) == -s FH or warn Could not read the entire file.\n; 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/