Re: problem printing contents of file in directory
alok nath wrote: Hi, Hello, Can anybody tell me why its not printing the contents of each inside a particular folder ? Yes I can, and so can Perl's documentation: perldoc -f readdir my $tstToRunDir = "C:\\perlScripts" ; my $fileInTstToRunDir ; opendir TST2RUN, $tstToRunDir || die "Failed to open $tstToRunDir $!\n" ; open RESULTS_LOG, ">>ResultLog.log" || die "Failed to open log\n" ; The logical or operator '||' has relatively high precedence so neither opendir nor open will die if an error is encountered. You need to either use parentheses: opendir( TST2RUN, $tstToRunDir ) || die "Failed to open $tstToRunDir $!\n" ; open( RESULTS_LOG, ">>ResultLog.log" ) || die "Failed to open log\n" ; or use the low precedence 'or' operator: opendir TST2RUN, $tstToRunDir or die "Failed to open $tstToRunDir $!\n" ; open RESULTS_LOG, ">>ResultLog.log" or die "Failed to open log\n" ; while ($fileInTstToRunDir = readdir (TST2RUN)){ chomp $fileInTstToRunDir ; $fileInTstToRunDir comes directly from the file system so there is no reason to use chomp. #open the file and get connection description and test description open FILE_2RUN, $fileInTstToRunDir || die " Failed to open $fileInTstToRunDir:$!\n" ; Again, there is a precedence problem with the '||' operator. The file name in $fileInTstToRunDir was obtained from the $tstToRunDir directory so you need to include the directory name in order to access it: open FILE_2RUN, "$tstToRunDir/$fileInTstToRunDir" or die " Failed to open $tstToRunDir/$fileInTstToRunDir:$!\n" ; print "File is : $fileInTstToRunDir\n" ; next if($fileInTstToRunDir =~ m/^./ ) ; while( ){ print $_ ; if ($_ =~ m/ } print RESULTS_LOG "File is : $fileInTstToRunDir\n" ; #$count++ ; # close FILE_2RUN ; } close RESULTS_LOG ; close TST2RUN ; 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: String Manipulation
Thank you. But i am unable to understand the working of the code which you have written. Can you please explain it? Thanks and Regards, Dharshana On 6/28/07, Chas Owens <[EMAIL PROTECTED]> wrote: On 6/27/07, Dharshana Eswaran <[EMAIL PROTECTED]> wrote: > On 6/28/07, Tom Phoenix <[EMAIL PROTECTED]> wrote: > > > > On 6/27/07, Dharshana Eswaran <[EMAIL PROTECTED]> wrote: > > > > > I am unable to get a generalised way in which it can extract them as few > > > structures have comments, few does not hav comments etc. > > > > Does the data have some defined grammar, or a definable one at least? > > > > The defined Grammer here is > { > xyz1 abc1; /*Comments*/ > xyz2 abc2; > xyz3 abc3[req]; > xyz4 abc4[req]; /*Comments*/ > }; > > Here, i have defined different possibility of occurences of the structure > elements. If i could get a regex for extracting xyz1, xyz2, xyz3, xyz4 and > abc1, abc2, abc3[req], abc4[req] would be helpful. Here, the comments are of > no use, i just need to ignore them. > > >If you are up to using Parse::RecDescent, it will probably do the job. > > I am restricted from using modules and i am unable to come up with a regex > or regexes to do this job. > > >http://search.cpan.org/author/DCONWAY/Parse-RecDescent-1.94 > >/lib/Parse/RecDescent.pod > > >Hope this helps! > > >--Tom Phoenix > >Stonehenge Perl Training > > Can anyone guide me in this? > > Thanks and Regards, > Dharshana > It is fragile, but here are a set of regexes that parse the string you mentioned. I did notice that this string differs significantly from the ones you gave earlier and this set of regexes will not correctly handle them. #!/usr/bin/perl use strict; use warnings; my $comment= qr{\s* (?:/\* .*? \*/ \s*)*}xs; my $identifier = qr{ [A-Za-z_]\w* }xs; my $statement = qr{ \s* ($identifier) \s+ ($identifier) \s* (?: \[ (.*?) \] )? \s* ; \s* $comment? }xs; my $str = <()) == 3) { if ($elems) { $type = "array of $type with $elems elements"; } print "type is $type and variable is $var\n"; } sub by_n { my ($n, $a) = @_; my $i = 0; sub { return undef if $i > $#$a; my @ret = @{$a}[$i .. $i + $n - 1]; $i += $n; return @ret; } }
Re: using a homemade perl module
Brad Baxter wrote: > On Jun 14, 10:22 pm, [EMAIL PROTECTED] (Mathew Snyder) wrote: >> I fixed all of the bugs save one. I can't access any of my subroutines >> without >> explicitly using it with dates_emails::subroutine. I was under the >> impression >> that if I was exporting them all from the module, the subroutine would be >> found >> regardless. >> >> package dates_emails; >> require Exporter; >> use strict; >> >> our @ISA = qw(Exporter); >> our @EXPORT = qw(startDate, endDate, searchStart, searchEnd); >> our @EXPORT_OK = qw($emailTo, $emailFrom, $emailBcc); >> our %EXPORT_TAGS = { >> dates => [qw(startDate, endDate, searchStart, searchEnd)], >> emails => [qw($emailTo, $emailFrom, $emailBcc)], >> }; >> our $VERSION = '1'; >> >> It doesn't even work with 'use dates_emails("dates");'. I get an error that >> dates is not an exported subroutine. I don't understand what I'm not doing >> right as I've got the %EXPORT_TAGS hash set up, I've got the @EXPORTS array >> set >> up. I've got this in my opening block: >> use lib '/usr/local/bin/lib/'; >> use dates_emails; > >> use strict; > > add: > > use warnings; > > ... > Possible attempt to separate words with commas at dates_emails.pm line > 6. > Possible attempt to separate words with commas at dates_emails.pm line > 7. > Possible attempt to separate words with commas at dates_emails.pm line > 9. > Possible attempt to separate words with commas at dates_emails.pm line > 10. > Reference found where even-sized list expected at dates_emails.pm line > 8. > > That may not be your whole problem, but it might get you a little > farther. > > -- > Brad > > I need to revisit this. I've broken things down a bit and separated the email addresses from the date subs and now have two files under /usr/local/bin/lib/Reports: Dates.pm and Emails.pm. *Dates.pm:* package Dates; require Exporter; use strict; use warnings; our @ISA = qw(Exporter); our @EXPORT = qw(startDate endDate searchStart searchEnd); our $VERSION = '1'; # Declare our global variables my (@date, @days, @months, @years, @searchDate); my $time = time(); our (@searchDate, $startDate, $endDate, $searchStart, $searchEnd); sub getDates { for (1 .. 7) { $time -= 24*60*60; @date = (localtime($time))[3 .. 5]; push @days, (sprintf '%02d', $date[0]); push @months,(sprintf '%02d',$date[1] + 1); push @years, $date[2] + 1900; return; } } sub searchDate { getDates(); push @searchDate, join "-", ($date[2] + 1900), (sprintf '%02d',$date[1] + 1), (sprintf '%02d', $date[0]); return [EMAIL PROTECTED]; } sub startDate { getDates(); $startDate = join "-", $months[$#months], $days[$#days], $years[$#years]; return $startDate; } sub endDate { getDates(); $endDate = join "-", $months[0], $days[0], $years[0]; return $endDate; } sub searchStart { getDates(); $searchStart = join "-", $years[$#years], $months[$#months], $days[$#days]; return $searchStart; } sub searchEnd { getDates(); $searchEnd = join "-", $years[0], $months[0], $days[0]; return $searchEnd; } return 1; The simple thing I'm trying to do to test all of this is: #!/usr/bin/perl ### # Title:module_test.pl # Author: Mathew Snyder # Reliease: 0.1 # Date: June 13, 2007 ### use warnings; use strict; use lib "/usr/local/bin/lib"; use Reports::Dates; my $today = startDate(); print $today . "\n"; Doing things this way gives me an error telling me that &main::startDate isn't defined. However, if I use 'my $today = Dates::startDate;' it works. I'm confused since I told it to 'use Reports::Dates;'. Anyone have any insight on why this isn't working the way I've been told it should? Thanks, Mathew -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
printing content of found file
I'm trying to do the following: I want to search for a specific file/s in my current dir and when it finds it, print its contents. So I did the following: #!/usr/bin/perl opendir(CURRENT,"."); @list = readdir(CURRENT); closedir(CURRENT); foreach $item (@list){ if($item =~ /notes/){ open(FILE,"@item"); @file = ; while(){ print }; close(FILE); print "@file\n"; } } I keep getting the following error: In string, @item now must be written as [EMAIL PROTECTED] at ./obj14-2.pl line 11, near "@item" Execution of ./obj14-2.pl aborted due to compilation errors. So I changed the script: #!/usr/bin/perl opendir(CURRENT,"."); @list = readdir(CURRENT); closedir(CURRENT); foreach $item (@list){ if($item =~ /messages/){ open(FILE,"[EMAIL PROTECTED]"); #NOTE THE \ @file = ; while(){ print }; close(FILE); print "@file\n"; } } I don't get any output. The file for sure exists. Any ideas on what I'm doing wrong? Amichai
shuffling cards
I want to shuffle a deck of cards and then print out the top five cards. #!/usr/bin/perl @startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H", "9 H","10 H","J H","Q H","K H", "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D", "9 D","10 D","J D","Q D","K D", "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C", "9 C","10 C","J C","Q C","K C", "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S", "9 S","10 S","J S","Q S","K S"); for ($x=0;$x<100;$x++){ $shuffle1 = shift(@startingdeck); $ahuffle2 = shift(@startingdeck); $ahuffle3 = pop(@startingdeck); $ahuffle4 = pop(@startingdeck); push(@startingdeck,$shuffle1,$shuffle3,$shuffle2,$shuffle4); print "@startingdeck\n"; } I know I'm meant to use loops. Maybe "for loops". I still don't quite understand the loops. I've read over the doc for loops several times. I want to learn this, so please provide me with hints and tips as opposed to plain solutions if possible please. Thanks Amichai
Re: printing content of found file
Amichai Teumim 写道: I'm trying to do the following: I want to search for a specific file/s in my current dir and when it finds it, print its contents. So I did the following: #!/usr/bin/perl please add 'use strict' and 'use warnings' at the the begin of a script. opendir(CURRENT,"."); opendir CURRENT,"." or die $!; @list = readdir(CURRENT); closedir(CURRENT); foreach $item (@list){ if($item =~ /notes/){ open(FILE,"@item"); I think you want to open the file $item,not the array of @item.If you 'use strict',you'll find this array was not declared before you use it. so change it to: open FILE,$item or die $!; @file = ; while(){ print }; Since you've read all the content by the first statement,the file pointer has reached the end of file.So if you need to re-read it,please seek() it: @file = ; seek(FILE,0,0); print while(); close(FILE); print "@file\n"; } } Good luck! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: printing content of found file
Amichai Teumim wrote: I'm trying to do the following: I want to search for a specific file/s in my current dir and when it finds it, print its contents. So I did the following: #!/usr/bin/perl The next two lines in your program should be: use warnings; use strict; opendir(CURRENT,"."); You should *ALWAYS* verify that the directory opened correctly: opendir CURRENT, '.' or die "Cannot open the current directory: $!"; @list = readdir(CURRENT); closedir(CURRENT); foreach $item (@list){ if($item =~ /notes/){ open(FILE,"@item"); You should *ALWAYS* verify that the file opened correctly: You are trying to open the 'file' "@item"? "@item" is the same as saying join( ' ', @item ). open FILE, $item or die "Cannot open '$item' $!"; @file = ; while(){ print }; close(FILE); print "@file\n"; } } This should do what you want: @ARGV = <*notes*>; print while <>; 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: shuffling cards
Amichai Teumim 写道: I want to shuffle a deck of cards and then print out the top five cards. But what's "top five cards" since I saw all the cards are unique. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: shuffling cards
Amichai Teumim wrote: I want to shuffle a deck of cards and then print out the top five cards. I want to learn this, so please provide me with hints and tips as opposed to plain solutions if possible please. perldoc -q shuffle 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: shuffling cards
Amichai Teumim <[EMAIL PROTECTED]> asked: > I want to shuffle a deck of cards and then print out the top > five cards. Read the Perl faq entry on shuffling arrays (i.e. perldoc -q shuffle). If you're using a fairly recent version of Perl, this'll get you started. #!/usr/bin/perl -w use strict; use 5.008; use List::Util 'shuffle'; my @deck = shuffle("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H", "9 H","10 H","J H","Q H","K H", "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D", "9 D","10 D","J D","Q D","K D", "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C", "9 C","10 C","J C","Q C","K C", "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S", "9 S","10 S","J S","Q S","K S"); print "Your hand: " . join( ', ', @deck[0..4] ) . "\n"; __END__ HTH, Thomas -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: printing content of found file
Hi, Hope you can find your solution from the following two approaches Approach I #!/usr/bin/perl while (<*>) { if ($_ =~ /note/){ open(FH, "$_"); @fcontent = ; print @fcontent; close(FH); } } APPROACH II opendir(CURRENT,"."); @list = readdir(CURRENT); closedir(CURRENT); foreach $item (@list){ if($item =~ /note/){ open(FILE,"$item"); @file = ; print @file; close(FILE); } } Thanks, Prabu.M.A @teumim.com> wrote: I'm trying to do the following: I want to search for a specific file/s in my current dir and when it finds it, print its contents. So I did the following: #!/usr/bin/perl opendir(CURRENT,"."); @list = readdir(CURRENT); closedir(CURRENT); foreach $item (@list){ if($item =~ /notes/){ open(FILE,"@item"); @file = ; while(){ print }; close(FILE); print "@file\n"; } } I keep getting the following error: In string, @item now must be written as [EMAIL PROTECTED] at ./obj14-2.pl line 11, near "@item" Execution of ./obj14-2.pl aborted due to compilation errors. So I changed the script: #!/usr/bin/perl opendir(CURRENT,"."); @list = readdir(CURRENT); closedir(CURRENT); foreach $item (@list){ if($item =~ /messages/){ open(FILE,"[EMAIL PROTECTED]"); #NOTE THE \ @file = ; while(){ print }; close(FILE); print "@file\n"; } } I don't get any output. The file for sure exists. Any ideas on what I'm doing wrong? Amichai - Moody friends. Drama queens. Your life? Nope! - their life, your story. Play Sims Stories at Yahoo! Games.
Re: printing content of found file
Hi, Hope you can find your solution from the following two approaches Approach I #!/usr/bin/perl while (<*>) { if ($_ =~ /note/){ open(FH, "$_"); @fcontent = ; print @fcontent; close(FH); } } APPROACH II opendir(CURRENT,"."); @list = readdir(CURRENT); closedir(CURRENT); foreach $item (@list){ if($item =~ /note/){ open(FILE,"$item"); @file = ; print @file; close(FILE); } } Thanks, Prabu.M.A @teumim.com> wrote: I'm trying to do the following: I want to search for a specific file/s in my current dir and when it finds it, print its contents. So I did the following: #!/usr/bin/perl opendir(CURRENT,"."); @list = readdir(CURRENT); closedir(CURRENT); foreach $item (@list){ if($item =~ /notes/){ open(FILE,"@item"); @file = ; while(){ print }; close(FILE); print "@file\n"; } } I keep getting the following error: In string, @item now must be written as [EMAIL PROTECTED] at ./obj14-2.pl line 11, near "@item" Execution of ./obj14-2.pl aborted due to compilation errors. So I changed the script: #!/usr/bin/perl opendir(CURRENT,"."); @list = readdir(CURRENT); closedir(CURRENT); foreach $item (@list){ if($item =~ /messages/){ open(FILE,"[EMAIL PROTECTED]"); #NOTE THE \ @file = ; while(){ print }; close(FILE); print "@file\n"; } } I don't get any output. The file for sure exists. Any ideas on what I'm doing wrong? Amichai - Get the free Yahoo! toolbar and rest assured with the added security of spyware protection.
Re: printing content of found file
Hi, Hope you can find your solution from the following two approaches Approach I #!/usr/bin/perl while (<*>) { if ($_ =~ /note/){ open(FH, "$_"); @fcontent = ; print @fcontent; close(FH); } } APPROACH II opendir(CURRENT,"."); @list = readdir(CURRENT); closedir(CURRENT); foreach $item (@list){ if($item =~ /note/){ open(FILE,"$item"); @file = ; print @file; close(FILE); } } Thanks, Prabu.M.A @teumim.com> wrote: I'm trying to do the following: I want to search for a specific file/s in my current dir and when it finds it, print its contents. So I did the following: #!/usr/bin/perl opendir(CURRENT,"."); @list = readdir(CURRENT); closedir(CURRENT); foreach $item (@list){ if($item =~ /notes/){ open(FILE,"@item"); @file = ; while(){ print }; close(FILE); print "@file\n"; } } I keep getting the following error: In string, @item now must be written as [EMAIL PROTECTED] at ./obj14-2.pl line 11, near "@item" Execution of ./obj14-2.pl aborted due to compilation errors. So I changed the script: #!/usr/bin/perl opendir(CURRENT,"."); @list = readdir(CURRENT); closedir(CURRENT); foreach $item (@list){ if($item =~ /messages/){ open(FILE,"[EMAIL PROTECTED]"); #NOTE THE \ @file = ; while(){ print }; close(FILE); print "@file\n"; } } I don't get any output. The file for sure exists. Any ideas on what I'm doing wrong? Amichai - Expecting? Get great news right away with email Auto-Check. Try the Yahoo! Mail Beta.
shuffling cards
Thanks for all the answers. I know there are other better ways of doing this shuffle. I must however, use pop shift ans push. #!/usr/bin/perl @startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H", "9 H","10 H","J H","Q H","K H", "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D", "9 D","10 D","J D","Q D","K D", "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C", "9 C","10 C","J C","Q C","K C", "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S", "9 S","10 S","J S","Q S","K S"); for ($x=0;$x<100;$x++){ $shuffle1 = shift(@startingdeck); $ahuffle2 = shift(@startingdeck); $ahuffle3 = pop(@startingdeck); $ahuffle4 = pop(@startingdeck); push(@startingdeck,$shuffle1,$shuffle3,$shuffle2,$shuffle4); print "@startingdeck\n"; } I get it all shuffled up the way I want. I just want now the top five cards printed. Which when running this script: 3 H 4 H 5 H 6 H 7 H Anyway of doing that without changing all the pushing, shifing and popping? Thanks for all your help. I've been stuck on this for days (or is it a week already?) Amichai
More loops
I need to use two loops and an if statement to sort the contents of this array so that the number go from lowest to highest. #!/usr/bin/perl @array = (5,3,2,1,4); ## include your code here ## foreach $elem (@array){ print "$elem"; } Looking further into this it was revealed to me that I should use a bubble loop. Such as this: for (i=0; i
Re: shuffling cards
Hi, If you don't use rand() you will allways get the same result after shuffeling. Is that OK for you? ( you're cheating in card games, right? *eg* ) On Thu, 28 Jun 2007 12:37:29 +0300 "Amichai Teumim" <[EMAIL PROTECTED]> wrote: > Thanks for all the answers. I know there are other better ways of doing this > shuffle. I must however, use pop shift ans push. > > #!/usr/bin/perl > > @startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H", > "9 H","10 H","J H","Q H","K H", > "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D", > "9 D","10 D","J D","Q D","K D", > "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C", > "9 C","10 C","J C","Q C","K C", > "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S", > "9 S","10 S","J S","Q S","K S"); > > for ($x=0;$x<100;$x++){ > >$shuffle1 = shift(@startingdeck); >$ahuffle2 = shift(@startingdeck); >$ahuffle3 = pop(@startingdeck); >$ahuffle4 = pop(@startingdeck); > >push(@startingdeck,$shuffle1,$shuffle3,$shuffle2,$shuffle4); >print "@startingdeck\n"; > } > > I get it all shuffled up the way I want. I just want now the top five cards > printed. Which when running this script: > > 3 H 4 H 5 H 6 H 7 H > > Anyway of doing that without changing all the pushing, shifing and popping? > > Thanks for all your help. I've been stuck on this for days (or is it a week > already?) > > Amichai -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
parsing a line
Hi, I am parsing a file which has lines like this. Got stuck up while trying to extract values of fields called Description, ID ? Pls help. Thanks Alok. Shape Yahoo! in your own image. Join our Network Research Panel today! http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7
don't understand working script
I have this script, If you run it you can see how it nicely idents the directories. I don't understand everything in this script. Please see my comments. #!/usr/bin/perl $startdir = "/lib"; $level = 0;#WHAT DOES THIS DO? list_dirs($startdir,$level); #WHAT DOES THIS DO? sub list_dirs(){ my $dir = shift (@_); #WHAT DOES THIS DO? my $lev = shift (@_); #WHAT DOES THIS DO? opendir(TOP,$dir); my @files = readdir(TOP); closedir(TOP); shift(@files); shift(@files); foreach $file (@files){ if(-d "$dir/$file"){ spaces($lev); #WHAT DOES THIS DO? print "$file\n"; list_dirs("$dir/$file",$lev+1); #WHAT DOES THIS DO? } } } #WHAT DOES THIS WHOLE SECTION DO? sub spaces(){ my($num) = shift(@_); for($i=0;$i<$num;$i++){ print " "; } } Thanks Amichai
Re: String Manipulation
On 6/28/07, Dharshana Eswaran <[EMAIL PROTECTED]> wrote: Thank you. But i am unable to understand the working of the code which you have written. Can you please explain it? Thanks and Regards, Dharshana What, specifically, do you not understand? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: don't understand working script
On 6/28/07, Amichai Teumim <[EMAIL PROTECTED]> wrote: I have this script, If you run it you can see how it nicely idents the directories. I don't understand everything in this script. Please see my comments. #!/usr/bin/perl $startdir = "/lib"; $level = 0;#WHAT DOES THIS DO? It assigns 0 to the scalar variable $level. list_dirs($startdir,$level); #WHAT DOES THIS DO? it calls the subroutine &list_dirs with the arguments $startdir and $level sub list_dirs(){ This is a misuse of prototypes. it should be sub list_dirs { Luckily, if you want to call it that, the misuse has no effect on the program because the call to the subroutine occurs before the definition of the subroutine, thus causing the code to ignore the prototype. my $dir = shift (@_); #WHAT DOES THIS DO? my $lev = shift (@_); #WHAT DOES THIS DO? @_ is an array. In this context it holds the arguments passed to list_dirs. So the scalar $dir is being assigned the contents $startdir variable from above. Likewise $lev is being assigned the contents of $level. opendir(TOP,$dir); my @files = readdir(TOP); closedir(TOP); shift(@files); shift(@files); foreach $file (@files){ if(-d "$dir/$file"){ spaces($lev); #WHAT DOES THIS DO? calls the spaces subroutine with the argument $lev print "$file\n"; list_dirs("$dir/$file",$lev+1); #WHAT DOES THIS DO? The subroutine is calling itself. This is called recursion. It is a form of looping. You might want to read http://en.wikipedia.org/wiki/Recursion_(computer_science) } } } #WHAT DOES THIS WHOLE SECTION DO? sub spaces(){ my($num) = shift(@_); for($i=0;$i<$num;$i++){ print " "; } } It is poorly written and poorly indented, but this subroutine prints out the number of spaces passed as an argument. It also is misusing prototypes. Thanks Amichai -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: using a homemade perl module
On 06/28/2007 03:00 AM, Mathew Snyder wrote: our @ISA = qw(Exporter); our @EXPORT = qw(startDate endDate searchStart searchEnd); our $VERSION = '1'; Those lines need to be within a BEGIN block. See perlmod: http://perldoc.perl.org/perlmod.html -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: More loops
From: "Amichai Teumim" <[EMAIL PROTECTED]> > I need to use two loops and an if statement to sort the contents of > this array so > that the number go from lowest to highest. @sorted = sort {$a <=> $b} @unsorted; You can use the builtin function sort(). All you have to do is to tell it how do you want the elements compared. > So I tried to implement this: > > #!/usr/bin/perl > > @array = (5,3,2,1,4); > > for (i=0; i for (j=0; j if ($array[j+1] < $array[j]) { /* compare the two neighbors */ > tmp = $array[j]; /* swap $array[j] and $array[j+1] */ > $array[j] = $array[j+1]; > $array[j+1] = tmp; > } > } The first thing you are missing are sigils. All variables in Perl have to start with a sigil ($, @, %, ...). for ($i=0; $i<$n-1; $i++) { for ($j=0; $j<$n-1-$i; $j++) if ($array[$j+1] < $array[$j]) { /* compare the two neighbors */ $tmp = $array[$j]; /* swap $array[j] and $array[j+1] */ $array[$j] = $array[$j+1]; $array[$j+1] = $tmp; } } Next thing is that you do not need a third variable to exchange the values of two variables. ($a, $b) = ($b, $a); is perfectly legal and more efficient. So you can write the body of the inner loop like this: ($array[j], $array[j+1]) = ($array[j+1], $array[j]); You may even use so called "array slices". That is instead of writing ($array[j+1], $array[j]) you can write just @array[j+1,j] so the body of the loop will be @array[j,j+1] = @array[j+1,j]; You may also use the foreach style of loop instead of the C-style for(): foreach my $i (0 .. $#array) { foreach my $j (0 .. $#array-1-$i) { if ($array[$j+1] < $array[$j]) { @array[j,j+1] = @array[j+1,j]; } } } > foreach $elem (@array){ > print "$elem\n"; > } This can be simplified to print join("\n", @array), "\n"; Jenda = [EMAIL PROTECTED] === 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 PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: shuffling cards
I dont understand why you dont like your solution? whats wrong at 3 H 4 H 5 H 6 H 7 H? On Thu, 28 Jun 2007 12:59:21 +0300 "Amichai Teumim" <[EMAIL PROTECTED]> wrote: > Yeah I don't mind cheating in this one ;) > > I just need the top five cards printed, even if the same each time. > > Any ideas? > > On 6/28/07, Martin Barth <[EMAIL PROTECTED]> wrote: > > > > Hi, > > > > If you don't use rand() you will allways get the same result after > > shuffeling. Is that OK for you? > > ( you're cheating in card games, right? *eg* ) > > > > On Thu, 28 Jun 2007 12:37:29 +0300 > > "Amichai Teumim" <[EMAIL PROTECTED]> wrote: > > > > > Thanks for all the answers. I know there are other better ways of doing > > this > > > shuffle. I must however, use pop shift ans push. > > > > > > #!/usr/bin/perl > > > > > > @startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H", > > > "9 H","10 H","J H","Q H","K H", > > > "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D", > > > "9 D","10 D","J D","Q D","K D", > > > "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C", > > > "9 C","10 C","J C","Q C","K C", > > > "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S", > > > "9 S","10 S","J S","Q S","K S"); > > > > > > for ($x=0;$x<100;$x++){ > > > > > >$shuffle1 = shift(@startingdeck); > > >$ahuffle2 = shift(@startingdeck); > > >$ahuffle3 = pop(@startingdeck); > > >$ahuffle4 = pop(@startingdeck); > > > > > >push(@startingdeck,$shuffle1,$shuffle3,$shuffle2,$shuffle4); > > >print "@startingdeck\n"; > > > } > > > > > > I get it all shuffled up the way I want. I just want now the top five > > cards > > > printed. Which when running this script: > > > > > > 3 H 4 H 5 H 6 H 7 H > > > > > > Anyway of doing that without changing all the pushing, shifing and > > popping? > > > > > > Thanks for all your help. I've been stuck on this for days (or is it a > > week > > > already?) > > > > > > Amichai > > > > -- > > 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/
Re: using a homemade perl module
On Thu, Jun 28, 2007 at 06:58:36AM -0500, Mumia W. wrote: > On 06/28/2007 03:00 AM, Mathew Snyder wrote: > > > >our @ISA = qw(Exporter); > >our @EXPORT = qw(startDate endDate searchStart searchEnd); > >our $VERSION = '1'; > > Those lines need to be within a BEGIN block. See perlmod: Are you sure? The package name should be Reports::Dates, not just Dates. -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: More loops
On 6/28/07, Jenda Krynicky <[EMAIL PROTECTED]> wrote: From: "Amichai Teumim" <[EMAIL PROTECTED]> snip > foreach $elem (@array){ > print "$elem\n"; > } This can be simplified to print join("\n", @array), "\n"; snip or (since this is Perl and TIMTOWTDI) print map { "$_\n" } @array; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: shuffling cards
On 6/28/07, Martin Barth <[EMAIL PROTECTED]> wrote: snip > #!/usr/bin/perl You are missing two lines here. use strict; use warnings; If you don't put those two lines you will be surprised by what Perl does with your code. > > @startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H", > "9 H","10 H","J H","Q H","K H", > "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D", > "9 D","10 D","J D","Q D","K D", > "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C", > "9 C","10 C","J C","Q C","K C", > "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S", > "9 S","10 S","J S","Q S","K S"); > > for ($x=0;$x<100;$x++){ Don't do this. This is a C style for loop. C style for loops are bad for a number of reasons I will go into if you ask. Instead use the Perl style for loop: for my $x (0 .. 99) { > >$shuffle1 = shift(@startingdeck); >$ahuffle2 = shift(@startingdeck); >$ahuffle3 = pop(@startingdeck); >$ahuffle4 = pop(@startingdeck); > >push(@startingdeck,$shuffle1,$shuffle3,$shuffle2,$shuffle4); Remember how I said you would be surprised? Well, here you are assigning $ahuffle2 and later you are using $shuffle2. Without the strict pragma Perl thinks this is just fine and uses the empty variable $shuffle2 even though you meant it to use $affulle2. Also, never number your variables like this. It is sign you are using the wrong type of variable. What yo really need is an array: my @shuffle = shift(@startingdeck), shift(@startingdeck), pop(@startingdeck), pop(@startingdeck); push @startingdeck, @shuffle; >print "@startingdeck\n"; > } > > I get it all shuffled up the way I want. I just want now the top five cards > printed. Which when running this script: snip Well, you have a couple options. You could use a slice: print "@startingdeck[0 .. 4]\n"; You could use indexes: print "$startingdeck[0] $startingdeck[1] $startingdeck[2] $startingdeck[3] $startingdeck[4]\n"; You could use a loop for my $i (0 .. 4) { print shift $startingdeck; } print "\n"; You could flatten the list into a string and take a substr of it print substr("@startingdeck", 0, 4*5), "\n"; #four characters five times And a bunch of other ways. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: parsing a line
On 6/28/07, alok nath <[EMAIL PROTECTED]> wrote: Hi, I am parsing a file which has lines like this. Got stuck up while trying to extract values of fields called Description, ID ? What have you tried? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: More loops
From: "Chas Owens" <[EMAIL PROTECTED]> > On 6/28/07, Jenda Krynicky <[EMAIL PROTECTED]> wrote: > > From: "Amichai Teumim" <[EMAIL PROTECTED]> > snip > > > foreach $elem (@array){ > > > print "$elem\n"; > > > } > > > > This can be simplified to > > > > print join("\n", @array), "\n"; > snip > > or (since this is Perl and TIMTOWTDI) > > print map { "$_\n" } @array; or print "$_\n" for @array; or (and all beginners should close their eyes since I'm going dirty now) { local $" = "\n"; print "@array\n"; } or { local $, = "\n"; local $\ = "\n"; print @array; } or { local $\ = "\n"; print for @array; } Hello Tim, how's missis Toady? ;-) Jenda = [EMAIL PROTECTED] === 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 PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: parsing a line
Hi, So I tried something like this .It works ! if( $_ =~ m/ID\s=\s"(.*?)"\sDirAbsolute/){ print " Test ID is $1 \n" ; }else{ print " FAILED to locate Test ID \n" ; } May be its crude way. Thanks Alok. - Original Message From: Chas Owens <[EMAIL PROTECTED]> To: alok nath <[EMAIL PROTECTED]> Cc: beginners@perl.org Sent: Thursday, June 28, 2007 6:38:09 PM Subject: Re: parsing a line On 6/28/07, alok nath <[EMAIL PROTECTED]> wrote: > Hi, > I am parsing a file which has lines like this. > > Got stuck up while trying to extract values of fields called > Description, ID ? What have you tried? Ready for the edge of your seat? Check out tonight's top picks on Yahoo! TV. http://tv.yahoo.com/
Re: More loops
On 6/28/07, Jenda Krynicky <[EMAIL PROTECTED]> wrote: From: "Chas Owens" <[EMAIL PROTECTED]> > On 6/28/07, Jenda Krynicky <[EMAIL PROTECTED]> wrote: > > From: "Amichai Teumim" <[EMAIL PROTECTED]> > snip > > > foreach $elem (@array){ > > > print "$elem\n"; > > > } > > > > This can be simplified to > > > > print join("\n", @array), "\n"; > snip > > or (since this is Perl and TIMTOWTDI) > > print map { "$_\n" } @array; or print "$_\n" for @array; or (and all beginners should close their eyes since I'm going dirty now) { local $" = "\n"; print "@array\n"; } or { local $, = "\n"; local $\ = "\n"; print @array; } or { local $\ = "\n"; print for @array; } Hello Tim, how's missis Toady? ;-) Jenda = [EMAIL PROTECTED] === 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 Well, if we are going to be silly, how about s/$/\n/s for @array; print @array; s/\n$//s for @array; or printf join('',("%s\n") x @array), @array; or { local $" = ''; printf qq/@{[("%s\n") x @array]}/, @array; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: parsing a line
On 6/28/07, alok nath <[EMAIL PROTECTED]> wrote: snip if( $_ =~ m/ID\s=\s"(.*?)"\sDirAbsolute/){ snip It does look fragile. A lot depends on how likely the real input matches the example you gave. That regex will break if the input is Note the second space after the "ID =". Also, you can generalize the code by using character classes: if (my %rec = $s =~ /\s*([\w ]*\w)\s*=\s*"(.*?)"/g) { my $id = exists $rec{ID} ? $rec{ID} : "not set"; my $dir = exists $rec{DirAbsolute} ? $rec{DirAbsolute} : "not set"; my $desc = exists $rec{'Test Description'} ? $rec{'Test Description'} : "not set"; print "id $id dir $dir desc $desc\n"; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
missing curly - brain fried
Where is the open curly missing here? #!/usr/bin/perl @array = (5,3,2,1,4); for ($i=0; $i<$n-1; $i++) { ( for ($j=0; $j<$n-1-$i; $j++) if ($array[$j+1] < $array[$j]) { /* compare the two neighbors */ $tmp = $array[$j]; /* swap $array[j] and $array[j+1] */ $array[$j] = $array[$j+1]; $array[$j+1] = $tmp; } } foreach $elem (@array){ print "$elem"; }
Re: String Manipulation
On 6/27/07, Dharshana Eswaran <[EMAIL PROTECTED]> wrote: On 6/28/07, Tom Phoenix <[EMAIL PROTECTED]> wrote: > > On 6/27/07, Dharshana Eswaran <[EMAIL PROTECTED]> wrote: > > > I am unable to get a generalised way in which it can extract them as few > > structures have comments, few does not hav comments etc. > > Does the data have some defined grammar, or a definable one at least? The defined Grammer here is { xyz1 abc1; /*Comments*/ xyz2 abc2; xyz3 abc3[req]; xyz4 abc4[req]; /*Comments*/ }; Here, i have defined different possibility of occurences of the structure elements. If i could get a regex for extracting xyz1, xyz2, xyz3, xyz4 and abc1, abc2, abc3[req], abc4[req] would be helpful. Here, the comments are of no use, i just need to ignore them. Correct me if I'm wrong, but from your example, it looks like all the "Dtata Types" are uppercase alphanumerics and underscores, and all the "variable names" are preceded with at least one space. I would start with something like the (fragile) following: my @lines = split /\n/, $string; while (@lines) { $_ = shift @lines; next if /^{/ or /^}/; my ($type, $name) = /([A-Z_]+)\s*(\S+)/; if ($name =~ /\[\S+\]/) { do something with the extra data, or not } } HTH, -- jay -- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org values of β will give rise to dom!
Re: missing curly - brain fried
Hi Amichai, first of all never write own code without these two lines: use strict; use warnings; this should allways help a lot. Remember the e-mail from Chas Owens regarding the "shuffling cards". making comments in perl goes this way: # hello, i am a comment. you made C-style comments: //, /* and */ are not allowed in perl. there are some other mistakes in your code (e.g use "for" always in this way: for(..) { .. } ) but you should fix your comments first and then you can start over trying to learn perl :) Have fun! HTH, Martin On Thu, 28 Jun 2007 17:54:01 +0300 "Amichai Teumim" <[EMAIL PROTECTED]> wrote: > Where is the open curly missing here? > > #!/usr/bin/perl > > @array = (5,3,2,1,4); > > > for ($i=0; $i<$n-1; $i++) { > ( for ($j=0; $j<$n-1-$i; $j++) > > > if ($array[$j+1] < $array[$j]) { /* compare the two neighbors > */ > $tmp = $array[$j]; /* swap $array[j] and $array[j+1] > */ > $array[$j] = $array[$j+1]; > $array[$j+1] = $tmp; > } > } > > > foreach $elem (@array){ > print "$elem"; > } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: missing curly - brain fried
> Where is the open curly missing here? > > #!/usr/bin/perl > > @array = (5,3,2,1,4); > > > for ($i=0; $i<$n-1; $i++) { I think its missing on the line bellow. I count three brackets bellow. > ( for ($j=0; $j<$n-1-$i; $j++) > > > if ($array[$j+1] < $array[$j]) { /* compare the two neighbors > */ > $tmp = $array[$j]; /* swap $array[j] and $array[j+1] > */ > $array[$j] = $array[$j+1]; > $array[$j+1] = $tmp; > } > } > > > foreach $elem (@array){ > print "$elem"; > } > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: printing content of found file
Amichai Teumim wrote: Thank you John Why do I need use warnings; use strict; It works without them: with them i get: Global symbol "@list" requires explicit package name at ./obj14-2.pl line 7. Global symbol "$item" requires explicit package name at ./obj14-2.pl line 10. Global symbol "@list" requires explicit package name at ./obj14-2.pl line 10. Global symbol "$item" requires explicit package name at ./obj14-2.pl line 11. Global symbol "$item" requires explicit package name at ./obj14-2.pl line 14. Global symbol "@file" requires explicit package name at ./obj14-2.pl line 15. Execution of ./obj14-2.pl aborted due to compilation errors. Thanks As you can see you don't *need* them, however you should use them to help you find problems with your code. 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: missing curly - brain fried
On 6/28/07, Amichai Teumim <[EMAIL PROTECTED]> wrote: Where is the open curly missing here? Here it is: @array = sort { $a <=> $b } @array; But if you really want to do it the hard, slow way Well, then you should be programming this as a shell script. But let's at least translate your code to Perl. #!/usr/bin/perl use strict; use warnings; @array = (5,3,2,1,4); Declare most new variables with my(). my @array = (5, 3, 2, 1, 4); for ($i=0; $i<$n-1; $i++) { ( for ($j=0; $j<$n-1-$i; $j++) The curly braces of a block are never optional in Perl, unlike in C. Most uses of the C-style computed for loop are simpler as a foreach loop in Perl: for my $i (0..$#array-1) { for my $j (0..$#array-1-$i) { if ($array[$j+1] < $array[$j]) { /* compare the two neighbors */ $tmp = $array[$j]; /* swap $array[j] and $array[j+1] */ $array[$j] = $array[$j+1]; $array[$j+1] = $tmp; } } Perl doesn't have multi-line comments like C, and it doesn't need to use temp variables to swap two items. # compare two neighbors if ($array[$j+1] < $array[$j]) { # swap these two ($array[$j], $array[$j+1]) = ($array[$j+1], $array[$j]); } # end two nested loops } } foreach $elem (@array){ print "$elem"; } print "Results: @array\n"; But I like the one liner better, perhaps because I have more confidence in its algorithm. 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/
Re: don't understand working script
Amichai Teumim wrote: I have this script, If you run it you can see how it nicely idents the directories. I don't understand everything in this script. [ SNIPPED an example of *BAD* code ] Your example would be better written as: #!/usr/bin/perl use warnings; use strict; my $startdir = '/lib'; my $level= 0; sub list_dirs { # remove first argument from @_ my $dir = shift; # remove second argument from @_ my $lev = shift; opendir TOP, $dir or die "Cannot open '$dir' $!"; # get all directory names except '.' and '..' my @files = grep -d "$dir/$_" && !/\A\.\.?\z/, readdir TOP; closedir TOP; foreach my $file ( @files ) { print ' ' x $lev, "$file\n"; list_dirs( "$dir/$file", $lev + 1 ); } } list_dirs( $startdir, $level ); __END__ 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/
SOLVED - Re: Tie::Handle::CSV
Hi folks The problem was that the files I'm reading are generated on M$ servers in DOS format. This meant that at the end of the file is a line containing the DOS EOF char ^Z. Using grep to remove this before reading the file has fixed the problem. The error's gone and my program continues as it should. Thanks for your help. Gary On Wednesday 27 June 2007 19:49, Chas Owens wrote: > On 6/27/07, Jay Savage <[EMAIL PROTECTED]> wrote: > snip > > > Make sure that the last record in your file is correctly terminated > > (i.e. there are no unclosed quotes), and that the last record is > > followed immediately by the final newline, which is alos the last > > character of the file. > > snip > > Try applying the following patch against > $YOUR_PERL_DIR/Tie/Handle/CSV.pm. It extends the error message on a > bad parse to make it more readable. > > --- CSV.pm.bak 2007-06-27 14:39:54.0 -0400 > +++ CSV.pm 2007-06-27 14:46:10.0 -0400 > @@ -151,7 +151,7 @@ >if (defined $csv_line) > { > $opts->{'csv_parser'}->parse($csv_line) > -|| croak $opts->{'csv_parser'}->error_input(); > +|| croak "could not parse: [" . > $opts->{'csv_parser'}->error_input() . "]"; > if ( $opts->{'header'} ) > { > my $parsed_line = $opts->{'simple_reads'} -- Gary Stainburn This email does not contain private or confidential material as it may be snooped on by interested government parties for unknown and undisclosed purposes - Regulation of Investigatory Powers Act, 2000 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Need idea for doing automatic iteration, please.
Dear my friends... I want my code does an action if it find a directory or file, namely: storing the url/path of a file or a directory onto mysql database. I am meaning, my code should look up every file/directory name resides under the "$rootdir" iteratively for doing storing onto mysql on each find. The algorithm For doing find from beginning to the end of the 1 level under $rootdir is still simple, it's only a "while{..}{...}". But the problem comes when the directory has a/some directory/-es whereas my code should also find them and stores them onto the mysql. If every directory has a limited level (for instance maximum only 3 level subdirectories below to the bottom) then all I have to do is simply creating nested "while(...)[..] " for 3 level. But in this case of course each directory may unpredictably has hundreds or thousands subdirectories/files in hundreds or thousands levels below to the bottom. Please suggest me some ideas where I can implement into my codes in order to enable my code to find all subdirectories and files where placed under $rootdir. A very simple code-sample is very..very...welcomed. Here is my current code under below. This code still only can find 1 level, only exactly 1 level under $rootdir. - package iterdir; use kueri; sub baru{ my $kelas = shift if @_; print "Nama superkelas: $kelas \n"; return( bless{} ); } sub bukadir{ my $kelas = shift; $rootdir="/home/patrikh/sementara/tes"; opendir("dirku", "$rootdir"); $statusp=chdir($rootdir); if ($statusp){ print "berhasil membuka direktori-->$rootdir\n"; while ($entridir=readdir("dirku")){ print "entridir: $entridir\n"; $sqlku=kueri->baru; $sqlku->konek; if (($entridir ne '.') and ($entridir ne '..')){ my $strsql = "insert into tblarsip (location) values ('".$entridir."')"; print "strsql: $strsql\n"; $sth=$kueri::dbh->prepare($strsql); $sth->execute; $sth->finish; } } } else{ print "gagal membuka direktori yang diinginkan: $rootdir\n"; exit 1; } } 1; -- Patrik Hasibuan <[EMAIL PROTECTED]> Junior Programmer -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
RE: Need idea for doing automatic iteration, please.
> -Original Message- > From: Patrik Hasibuan [mailto:[EMAIL PROTECTED] > Sent: Thursday, June 28, 2007 12:55 > To: Milis CPAN-Perl-Beginners > Subject: Need idea for doing automatic iteration, please. > > Dear my friends... > > I want my code does an action if it find a directory or file, > namely: storing the url/path of a file or a directory onto > mysql database. Working too hard, just use File::Find and you can control how deep you want to go, but if you just want all files, then take a look at the doc and with thexamples provided, should be very simple to do. Wags ;) David R Wagner Senior Programmer Analyst FedEx Freight 1.408.323.4225x2224 TEL 1.408.323.4449 FAX http://fedex.com/us > > I am meaning, my code should look up every file/directory > name resides under the "$rootdir" iteratively for doing > storing onto mysql on each find. > > The algorithm For doing find from beginning to the end of the > 1 level under $rootdir is still simple, it's only a "while{..}{...}". > But the problem comes when the directory has a/some > directory/-es whereas my code should also find them and > stores them onto the mysql. > If every directory has a limited level (for instance maximum > only 3 level subdirectories below to the bottom) then all I > have to do is simply creating nested "while(...)[..] " for 3 > level. But in this case of course each directory may > unpredictably has hundreds or thousands subdirectories/files > in hundreds or thousands levels below to the bottom. > > Please suggest me some ideas where I can implement into my > codes in order to enable my code to find all subdirectories > and files where placed under $rootdir. > A very simple code-sample is very..very...welcomed. > > Here is my current code under below. This code still only can > find 1 level, only exactly 1 level under $rootdir. > - > package iterdir; > use kueri; > > sub baru{ > my $kelas = shift if @_; > print "Nama superkelas: $kelas \n"; > return( bless{} ); > } > > sub bukadir{ > my $kelas = shift; > $rootdir="/home/patrikh/sementara/tes"; > opendir("dirku", "$rootdir"); > $statusp=chdir($rootdir); > if ($statusp){ > print "berhasil membuka direktori-->$rootdir\n"; > while ($entridir=readdir("dirku")){ > print "entridir: $entridir\n"; > $sqlku=kueri->baru; > $sqlku->konek; > if (($entridir ne '.') and ($entridir ne '..')){ > my $strsql = "insert into tblarsip (location) values > ('".$entridir."')"; > print "strsql: $strsql\n"; > $sth=$kueri::dbh->prepare($strsql); > $sth->execute; > $sth->finish; > } > } > } else{ > print "gagal membuka direktori yang diinginkan: $rootdir\n"; > exit 1; > } > } > > 1; > > -- > Patrik Hasibuan <[EMAIL PROTECTED]> > Junior Programmer > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > http://learn.perl.org/ > > > ** 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 PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
RE: Build module on one box and move to another box?
> Can this be done? Can I compile a module on one box and > somehow install the code on another? > Might this be as simple as copying over the contents of the > directories in @INC? As it turns out, this was fairly easy to do. I followed the advice from Chas Owens who suggested that I build them manually on another box and stop before the "make install". There were no pre-reqs that needed to be installed, so I guess I got lucky there. I tar'd up the build directories, moved them over, did another "make test" just to feel good about it, then the "make install". Further tests with our code went well. Thanks Chas, and to all who responded! richf -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: using a homemade perl module
On 06/28/2007 07:46 AM, Paul Johnson wrote: On Thu, Jun 28, 2007 at 06:58:36AM -0500, Mumia W. wrote: On 06/28/2007 03:00 AM, Mathew Snyder wrote: our @ISA = qw(Exporter); our @EXPORT = qw(startDate endDate searchStart searchEnd); our $VERSION = '1'; Those lines need to be within a BEGIN block. See perlmod: Are you sure? The package name should be Reports::Dates, not just Dates. Ah, yes you're right. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Formats for invoices.
Hi Folks I'm doing a single perl script to get data from a database (SQLlite), and want put some data in a format The output format is record in a simple temporary .txt file and send to lpr. the format is for a invoice displaying most important data. I have the next questions. 1.- How can put lines containing products descriptions ? 2.- Ho can justify or align a variable into format ? if any people can help me, it will be appreciated. Regards. -- Francisco Valladolid H. -- http://bsdguy.net - Perl, [Open-Net] BSD fan. --
Re: Formats for invoices.
On 6/28/07, Francisco Valladolid <[EMAIL PROTECTED]> wrote: 1.- How can put lines containing products descriptions ? 2.- Ho can justify or align a variable into format ? Have you seen the perlform manpage? http://perldoc.perl.org/perlform.html 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/
Re: Formats for invoices.
On 6/28/07, Tom Phoenix <[EMAIL PROTECTED]> wrote: On 6/28/07, Francisco Valladolid <[EMAIL PROTECTED]> wrote: > 1.- How can put lines containing products descriptions ? > 2.- Ho can justify or align a variable into format ? Have you seen the perlform manpage? http://perldoc.perl.org/perlform.html snip Or better yet, the new style forms from Perl 6 backported to Perl 5: http://search.cpan.org/~dconway/Perl6-Form-0.04/Form.pm -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Formats for invoices.
On Thu, 2007-06-28 at 17:24 -0700, Tom Phoenix wrote: > On 6/28/07, Francisco Valladolid <[EMAIL PROTECTED]> wrote: > > > 1.- How can put lines containing products descriptions ? > > 2.- Ho can justify or align a variable into format ? > > Have you seen the perlform manpage? > > http://perldoc.perl.org/perlform.html > > Hope this helps! Also check out PDF::ReportWriter: http://entropy.homelinux.org/axis -- 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 PROTECTED] website: http://www.nusconsulting.com.au -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: using a homemade perl module
Paul Johnson wrote: > On Thu, Jun 28, 2007 at 06:58:36AM -0500, Mumia W. wrote: > >> On 06/28/2007 03:00 AM, Mathew Snyder wrote: >>> our @ISA = qw(Exporter); >>> our @EXPORT = qw(startDate endDate searchStart searchEnd); >>> our $VERSION = '1'; >> Those lines need to be within a BEGIN block. See perlmod: > > Are you sure? > > The package name should be Reports::Dates, not just Dates. > Sweet! That was the problem. Thanks mucho Mathew Keep up with me and what I'm up to: http://theillien.blogspot.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: using a homemade perl module
Paul Johnson wrote: > On Thu, Jun 28, 2007 at 06:58:36AM -0500, Mumia W. wrote: > >> On 06/28/2007 03:00 AM, Mathew Snyder wrote: >>> our @ISA = qw(Exporter); >>> our @EXPORT = qw(startDate endDate searchStart searchEnd); >>> our $VERSION = '1'; >> Those lines need to be within a BEGIN block. See perlmod: > > Are you sure? > > The package name should be Reports::Dates, not just Dates. > I'm getting a strange bit of behaviour. I have everything set up right and my dates are getting made up properly however, one sub which creates the searchDate array isn't being called. I have to enter the full module path (Reports::Dates::searchDate) for it to work while all of the other dates are called without problem using just the sub name. Any thoughts on why this might be? Mathew Keep up with me and what I'm up to: http://theillien.blogspot.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
processing XL using Win32::OLE
Hi, Can anybody tell how to open an already existing xL file and then probabaly do some processing using Win32::OLE I found quite a few examples but none of them open an existing excel file. Or is there some better module for XL processing ? Here is my code .. use strict ; use warnings ; use Win32::OLE; use Win32::OLE::Const; my $file = "Matrix.xls" ; my $ex ; eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')}; die "Excel not installed" if $@; unless (defined $ex) { $ex = Win32::OLE->new($file, sub {$_[0]->Quit;}) or die "Oops, cannot start Excel"; } # get a new workbook my $book = $ex->Workbooks->Add; my $Sheet = $book->Worksheets(1); $Sheet->{Name} = 'Candle'; $book->Close; Thanks Alok. Never miss an email again! Yahoo! Toolbar alerts you the instant new Mail arrives. http://tools.search.yahoo.com/toolbar/features/mail/
Re: processing XL using Win32::OLE
Hi Alok, Hope the examples in the below links help your needs. For opening and reading an XL(Excel) use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; $Win32::OLE::Warn = 3;# die on errors... # get already active Excel application or open new my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); # open Excel file my $Book = $Excel->Workbooks->Open("test.xls"); my $Sheet = $Book->Worksheets(1); # select worksheet number 1 (you can also select a worksheet by name) my $Sheet = $Book->Worksheets(1); foreach my $row (1..4) { foreach my $col (1..3) { # skip empty cells next unless defined $Sheet->Cells($row,$col)->{'Value'}; # print out the contents of a cell printf "At ($row, $col) the value is %s and the formula is %s\n", $Sheet->Cells($row,$col)->{'Value'}, $Sheet->Cells($row,$col)->{'Formula'}; } } # clean up after ourselves $Book->Close; Refer the Below link...You can find More examples http://www.ibm.com/developerworks/library/l-pexcel/ Regards, Prabu.M.A alok nath <[EMAIL PROTECTED]> wrote: Hi, Can anybody tell how to open an already existing xL file and then probabaly do some processing using Win32::OLE I found quite a few examples but none of them open an existing excel file. Or is there some better module for XL processing ? Here is my code .. use strict ; use warnings ; use Win32::OLE; use Win32::OLE::Const; my $file = "Matrix.xls" ; my $ex ; eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')}; die "Excel not installed" if $@; unless (defined $ex) { $ex = Win32::OLE->new($file, sub {$_[0]->Quit;}) or die "Oops, cannot start Excel"; } # get a new workbook my $book = $ex->Workbooks->Add; my $Sheet = $book->Worksheets(1); $Sheet->{Name} = 'Candle'; $book->Close; Thanks Alok. Never miss an email again! Yahoo! Toolbar alerts you the instant new Mail arrives. http://tools.search.yahoo.com/toolbar/features/mail/ - Luggage? GPS? Comic books? Check out fitting gifts for grads at Yahoo! Search.
Re: processing XL using Win32::OLE
Found a good tutorial here : http://www.perlmonks.org/?node=153486 - Original Message From: alok nath <[EMAIL PROTECTED]> To: beginners@perl.org Sent: Friday, June 29, 2007 10:42:56 AM Subject: processing XL using Win32::OLE Hi, Can anybody tell how to open an already existing xL file and then probabaly do some processing using Win32::OLE I found quite a few examples but none of them open an existing excel file. Or is there some better module for XL processing ? Here is my code .. use strict ; use warnings ; use Win32::OLE; use Win32::OLE::Const; my $file = "Matrix.xls" ; my $ex ; eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')}; die "Excel not installed" if $@; unless (defined $ex) { $ex = Win32::OLE->new($file, sub {$_[0]->Quit;}) or die "Oops, cannot start Excel"; } # get a new workbook my $book = $ex->Workbooks->Add; my $Sheet = $book->Worksheets(1); $Sheet->{Name} = 'Candle'; $book->Close; Thanks Alok. Never miss an email again! Yahoo! Toolbar alerts you the instant new Mail arrives. http://tools.search.yahoo.com/toolbar/features/mail/ Need Mail bonding? Go to the Yahoo! Mail Q&A for great tips from Yahoo! Answers users. http://answers.yahoo.com/dir/?link=list&sid=396546091