comparing kit names
Hi, I have these type of kits in unix location. aaa.t.z aaa_d.t.z bbb.t.z bbb_d.t.z ccc.t.z ccc_d.t.z ddd.t.z eee.t.z (there will be more numbers). I have to come up with idea, so that , it compares for each *.t.z kit in that location, that it has the corresponding *_d.t.z. If any *.t.z doesn't have corresponding *_d.t.z, it will print out the list of that/those kit/kits. In this case, it should print out: ddd.t.z eee.t.z Currently I am putting the kit list in an array, still not able to compare them correctly. Any help suggestion will be appriciated. Thanks! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: how to simplify this script
Richard Lee wrote: It should include '1 2 3' because '1 2 3', '1 2 4', '1 2 7', '1 2 8', '1 2 9' = '1 2'(the common number from the list) + anynumber. as any of them contains 1 and 2 and I don't understand why '1 2 3' was picked. My interpretation: Because it's the first element of those with 1 and 2 in them. In other words, the order in which the elements appear in @datas is important for the result. Also can someone explain to me in detail what Gunnar Hjalmarsson's solution is doing? code of Gunnar's my $numbers_wanted = 2; my ( @datawanted, @numbers ); LOOP: foreach ( @datas ) { my @test = split; foreach my $num ( @numbers ) { next LOOP if grep( $num-{$_}, @test ) = $numbers_wanted; } push @datawanted, $_; push @numbers, { map { $_ = 1 } @test }; } print $_\n for @datawanted; It iterates over @datas and stores some of the elements in @datawanted based on (my interpretation of) the OP's criteria. There is nothing mysterious with the code; everything can be looked up in the Perl docs. @numbers is a help variable where the numbers in previously stored elements are made conveniently accessible for lookups. The expression map { $_ = 1 } @test creates a key/value list where the elements of @test are the keys (see perldoc -f map), and push @numbers, { map { $_ = 1 } @test }; makes the list an anonymous hash and adds a reference to that hash to @numbers. grep() is used in scalar context to compare the elements with previously stored elements and test against the OP's criteria. perldoc -f grep -- 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: comparing kit names
perl_learner wrote: Hi, I have these type of kits in unix location. aaa.t.z aaa_d.t.z bbb.t.z bbb_d.t.z ccc.t.z ccc_d.t.z ddd.t.z eee.t.z (there will be more numbers). I have to come up with idea, so that , it compares for each *.t.z kit in that location, that it has the corresponding *_d.t.z. If any *.t.z doesn't have corresponding *_d.t.z, it will print out the list of that/those kit/kits. In this case, it should print out: ddd.t.z eee.t.z Currently I am putting the kit list in an array, still not able to compare them correctly. One way: my @kits = qw( aaa.t.z aaa_d.t.z bbb.t.z bbb_d.t.z ccc.t.z ccc_d.t.z ddd.t.z eee.t.z ); my %hash = map { ( my $tmp = $_) =~ s/_d(\.t\.z)$/$1/; $tmp = 1 } grep /_d\.t\.z$/, @kits; foreach my $kit ( grep !/_d\.t\.z$/, @kits ) { print $kit\n unless $hash{$kit}; } -- 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: generating number ranges using rand function
John W. Krahn wrote: Aruna Goke wrote: I have worked on the code below and am able to generate the 15digit lenght required . However, what i wish to achieve is to make sure all the output comes out in 15 digits each. a sample of my output is as below. Can someone guide on how to make all come out in 15digits. print sprintf( '%.30f', rand ) =~ /\.(\d{15})/; This solution (as well as Gunnar's) relies on the available precision of the rand function, which is determined when Perl is built. The number of bits can be shown by executing perl -MConfig -e print $Config{randbits} and on ActiveState Perl, at least, it is only 15 bits, corresponding to 4.5 decimal digits. Expanding this limited amount of precision over 15 decimal digits will produce very poor random numbers, and it is far better to concatenate the results from five calls to rand(1000) unless the quality of randomness is of no consequence. The program below shows ten numbers produced using both methods, and even in this small sample the problem is clear. HTH, Rob use strict; use warnings; print Using single call to rand()\n; foreach (1...10) { my $n = rand15a(); print $n\n; } print \n\nUsing five calls to rand()\n; foreach (1...10) { my $n = rand15b(); print $n\n; } sub rand15a { my ($r15) = sprintf( '%.30f', rand ) =~ /\.(\d{15})/; $r15; } sub rand15b { my $r15; $r15 .= sprintf %03d, rand 1_000 for 1 .. 5; $r15; } **OUTPUT** Using single call to rand() 583312988281250 378173828125000 031066894531250 559173583984375 217193603515625 908050537109375 164520263671875 936859130859375 908111572265625 888763427734375 Using five calls to rand() 485911983221141 991568078471086 657810917897561 740256349243263 935703599145794 523194437953137 491906142761110 386269935142967 489434048410568 299184714819156 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Writing side of square
Hello, I need create a square using a single number, but I don't know how to create the sides. I have to create this: * * * * My code is below : print Enter with number:; chomp ($number = STDIN); my @array = (); $cont = 0; while ($numero $cont) { $array[$cont]=*; $cont++; } print @array; print \n; print @array; print \n; How I can to show the arrar like sides ? []'s Faria Abra sua conta no Yahoo! Mail, o único sem limite de espaço para armazenamento! http://br.mail.yahoo.com/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Writing side of square
The perl module Graphics::Simple might be able to help you. Found on CPAN. --jms On May 5, 2008, at 9:22 AM, Rodrigo Tavares wrote: Hello, I need create a square using a single number, but I don't know how to create the sides. I have to create this: * * * * My code is below : print Enter with number:; chomp ($number = STDIN); my @array = (); $cont = 0; while ($numero $cont) { $array[$cont]=*; $cont++; } print @array; print \n; print @array; print \n; How I can to show the arrar like sides ? []'s Faria Abra sua conta no Yahoo! Mail, o único sem limite de espaço para armazenamento! http://br.mail.yahoo.com/ -- 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: Writing side of square
Rodrigo Tavares [EMAIL PROTECTED] asked: I need create a square using a single number, but I don't know how to create the sides. #!/usr/bin/perl -w use strict; for( my $n = 1 ; $n = 10; $n++ ){ print \nn = $n\n\n; if( $n == 1 ){ print *\n; } else { print '*'x$n . \n; for( my $line = 2; $line $n; $line ++ ){ print '*' . ' 'x($n-2) . *\n; } print '*'x$n . \n; } } __END__ HTH, Thomas -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Comparing files with regular expressions
Given just the idea of the data, can you improve on that? I bet I could! It's interesting how my instinct, when trying to develop a programming solution, is to wrestle with the problem inside the context of the language. As a result, the solutions I come up with tend to be shaped by my limited understanding of that language. I think you're right that this is a case of fluency, that I am fluent in English and my best problem solving skills are most likely in that context. Trying to solve the problem in Perl, I'm likely not using my best skills and thus come up with a poor solution. I also take from your advice, whether you meant it or not, that I should approach my code as if it would be scalable. My solution is probably adequate for a small scale problem but its silliness would quickly be exposed as soon as the data scaled up. Thanks for the advice and inspiration. On Sat, May 3, 2008 at 8:08 PM, Rob Dixon [EMAIL PROTECTED] wrote: rubinsta wrote: Hello, I'm a Perl uber-novice and I'm trying to compare two files in order to exclude items listed on one file from the complete list on the other file. What I have so far prints out a third file listing everything that matches the exclude file from the complete file (which I'm hoping will be a duplicate of the exclude file) just so I can make sure that the comparison script is working. The files are lists of numbers separated by newlines. The exclude file has 333 numbers and the complete file has 9000 numbers. Here's what I have so far: #!/usr/bin/perl use strict; use warnings; open(ALL, all.txt) or die $!; open(EX, exclude.txt) or die $!; open(OUT,'exTest.txt') or die $!; my @ex_lines = EX; my @all_lines = ALL; foreach $all (@all_lines){ foreach $ex (@ex_lines){ if ($ex =~ /(^$all)/){ The lines you have read from the object files are unchomped (include the trailing newline character) and there is no allowance for leading or trailing whitespace. Are you sure of your input data? The regex has an unnecessary capture (parentheses) and isn't tied at the end of the string, although leaving the record separator at the end of $ex and $all has a similar effect. It should really be simply if ($ex eq $all) print OUT $1; The two strings are equal, so print OUT $all; } } } close(ALL); close(EX); Explicit closures are pointless unless the status is verified. All open filehandles will be closed by Perl when it finishes processing the script. (Even if an input file doesn't close cleanly, the damage has already been done when an earlier read failed. If a volume is dismounted while the program is running, for example, without explicit handling of read errors the file will simply appear to be shorter than its true length.) close(OUT); There's no need to close output files unless you're in a fragile environment, or if it is vital that the output information is complete. For instance it may be useful to write close $output or die $!; unlink 'input.txt'; so that the object data was discarded only if the target data was safely written and secured. I realize the nested foreach loops are ugly but I don't know enough to navigate the filehandles, which as I understand, can only be assigned to variables in their entirety as an array. Any thoughts on how this might be done? You should try to solve the problem instead of solving the data. Nearly all of your code is about opening, reading, and closing files. Your solution amounts to: if any of the lines in ALL match any of the lines in EX then print (it) Given just the idea of the data, can you improve on that? For instance, if one or both of the object files are sorted then you may not need to reassess all of the lines for each comparison. Or if the lines could occur more than once in either or both files, then it may be an idea to maintain a record of what comparisons had already been made. Those ideas are independent of Perl, or indeed of any programming language. After that, the line blurs. Programming languages are useful thinking tools for imagining programming solutions, just as natural languages are useful for life's challenges. An idea expressed in Latin can be impossible to recreate intact in French, just a solution in Forth can be inexpressible in C++. But despite its blurriness the line is narrow, so have courage and dash cross it into the implementation, where all languages have ways to open, close, read and write files; ways to handle numbers and strings; conveniences for arrays and constants and, God forbid, error handling. But I encourage you to start at the beginning, and if common sense is more familiar to you than Perl or any other programming language then use that. Your imagination is your best tool. If you were given two piles of line printer paper and were told to find the differences: - what questions would
Re: Comparing files with regular expressions
Aaron Rubinstein wrote: Given just the idea of the data, can you improve on that? I bet I could! I bet you could too :) It's interesting how my instinct, when trying to develop a programming solution, is to wrestle with the problem inside the context of the language. As a result, the solutions I come up with tend to be shaped by my limited understanding of that language. I think you're right that this is a case of fluency, that I am fluent in English and my best problem solving skills are most likely in that context. Trying to solve the problem in Perl, I'm likely not using my best skills and thus come up with a poor solution. It's a frequent assumption that when you working with a tool of any sort, whether it's a knife and fork or a golf club, that you should work with that tool until you are proficient. But unless those tools are prescribed by the rules of the game in play then you should consider alternatives. I often eat from a ladle or wooden spoon when I am cooking, but etiquette says that I may not do the same at table; and getting a ball into a hole half a mile away by hitting it with a stick is not a good solution by any standards. More often than not, a programming language restricts what you can do over what you can describe using English, and while you can always get more out of any language by becoming familiar with it, you are usually becoming familiar with what is impossible or difficult rather than getting used to new exciting possibilities. I also take from your advice, whether you meant it or not, that I should approach my code as if it would be scalable. My solution is probably adequate for a small scale problem but its silliness would quickly be exposed as soon as the data scaled up. Never write off your solution as silly. If it works then it is a solution, and final solutions are almost never the best ones possible. I meant quite the opposite about scalability. My intention was to emphasize that the amount of data changes what is a good solution. It is a useful exercise to imagine that the data is printed on sheets of paper and that you have to solve the problem manually given just an aircraft hangar full of filing cabinets. If you have only a couple of sheets of paper with a single line printed on each, then you can just sit at your desk and write the output. But if you have several stacks of paper then you might want to start using the filing system. Thanks for the advice and inspiration. You're more than welcome. Remember that the best way to solve a problem, whether it's a programming problem or any other sort, is to think about whether it's comparable to any situation you have already come across. It's called abstraction and it's your friend :) Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Writing side of square
Rodrigo Tavares wrote: Hello, I need create a square using a single number, but I don't know how to create the sides. I have to create this: * * * * My code is below : print Enter with number:; chomp ($number = STDIN); my @array = (); $cont = 0; while ($numero $cont) { $array[$cont]=*; $cont++; } print @array; print \n; print @array; print \n; my @square = ('*' x $number) x $number; substr $_, 1, $number-2, ' ' x ($number-2) for @square[1..$number-2]; print \n\n; print $_\n foreach @square; Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Writing side of square
On Monday 05 May 2008 09:22:51 Rodrigo Tavares wrote: Hello, I need create a square using a single number, but I don't know how to create the sides. I have to create this: * * * * My code is below : print Enter with number:; chomp ($number = STDIN); my @array = (); $cont = 0; while ($numero $cont) { $array[$cont]=*; $cont++; } print @array; print \n; print @array; print \n; How I can to show the arrar like sides ? []'s Faria Abra sua conta no Yahoo! Mail, o único sem limite de espaço para armazenamento! http://br.mail.yahoo.com/ for (1..$n) { if ($_ == 1 || $_ == $n) { print * x $n, \n; } else { print *, x ($n - 2), *\n; } } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: IDE for Perl in Linux
On Fri, 2008-05-02 at 08:04 -0700, Rodrigo Tavares wrote: Hello, Today I write my perls scripts with a simple editor. I found this link http://www.enginsite.com/Perl.htm, but it run only in Windows. This link http://www.solutionsoft.com/perl.htm, contain the for linux, but have to buy. Anybody knows a simple and good IDE Perl for Linux ? Eclipse with the EPIC plugin is an excellent editor debugger. It's also faster than Komodo, which is stunning considering it's written in Java. And it's free ... Dan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Writing side of square
On Mon, May 5, 2008 at 11:18 AM, Stephen Kratzer [EMAIL PROTECTED] wrote: snip for (1..$n) { if ($_ == 1 || $_ == $n) { print * x $n, \n; } else { print *, x ($n - 2), *\n; } } snip Its time to play TIMTOWTDI: #!/usr/bin/perl use strict; use warnings; my $n = 10; my $i = $n - 2; print * x $n, \n, (map *. x $i.*\n, 1 .. $i), * x $n, \n; print * x $n, \n, (*, x $i, *\n) x $i, * x $n, \n; -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: how to reun perl script on other pc without having to install perl
Yes, have you heard of a product called perltoexe by activestate? On Sun, 4 May 2008 [EMAIL PROTECTED] wrote: Hi, What must I do to my perl script so that my friends can run my perl script from their computer, using windows as the operating system, without having to install perl into their system. Technically, this conversion is it called compiling? Thanks -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: how to reun perl script on other pc without having to install perl
From: David Nicholas Kayal [EMAIL PROTECTED] Yes, have you heard of a product called perltoexe by activestate? On Sun, 4 May 2008 [EMAIL PROTECTED] wrote: Hi, What must I do to my perl script so that my friends can run my perl script from their computer, using windows as the operating system, without having to install perl into their system. Technically, this conversion is it called compiling? Thanks - Because it's all backwards. - Why is that? - It's hard to read. - Why? - Please, do not top post. I think you mean PerlApp (http://www.activestate.com/Products/perl_dev_kit/index.mhtml). Plus there is also Perl2exe from IndigoStar (http://www.perl2exe.com/) And then there is PAR (http://search.cpan.org/~smueller/PAR-Packer- 0.978/lib/pp.pm) 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: how to simplify this script
Gunnar Hjalmarsson wrote: Richard Lee wrote: It should include '1 2 3' because '1 2 3', '1 2 4', '1 2 7', '1 2 8', '1 2 9' = '1 2'(the common number from the list) + anynumber. as any of them contains 1 and 2 and I don't understand why '1 2 3' was picked. My interpretation: Because it's the first element of those with 1 and 2 in them. In other words, the order in which the elements appear in @datas is important for the result. Also can someone explain to me in detail what Gunnar Hjalmarsson's solution is doing? code of Gunnar's my $numbers_wanted = 2; my ( @datawanted, @numbers ); LOOP: foreach ( @datas ) { my @test = split; foreach my $num ( @numbers ) { next LOOP if grep( $num-{$_}, @test ) = $numbers_wanted; } push @datawanted, $_; push @numbers, { map { $_ = 1 } @test }; } print $_\n for @datawanted; It iterates over @datas and stores some of the elements in @datawanted based on (my interpretation of) the OP's criteria. There is nothing mysterious with the code; everything can be looked up in the Perl docs. @numbers is a help variable where the numbers in previously stored elements are made conveniently accessible for lookups. The expression map { $_ = 1 } @test creates a key/value list where the elements of @test are the keys (see perldoc -f map), and push @numbers, { map { $_ = 1 } @test }; makes the list an anonymous hash and adds a reference to that hash to @numbers. grep() is used in scalar context to compare the elements with previously stored elements and test against the OP's criteria. perldoc -f grep why does program go over empty array at the begining? foreach my $num ( @numbers ) { next LOOP if grep( $num-{$_}, @test ) = $numbers_wanted; } perl -d ./iii Loading DB routines from perl5db.pl version 1.25 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(./iii:9):my @datas = ( main::(./iii:10): '1 2 3', '1 2 4', '1 2 7', '1 2 8', '1 2 9', '1 6 7', main::(./iii:11): '1 7 12', '2 6 7', '4 5 10', '4 5 15' main::(./iii:12): ); DB1 s main::(./iii:14): my $numbers_wanted = 2; DB1 x @datas 0 '1 2 3' 1 '1 2 4' 2 '1 2 7' 3 '1 2 8' 4 '1 2 9' 5 '1 6 7' 6 '1 7 12' 7 '2 6 7' 8 '4 5 10' 9 '4 5 15' DB2 s main::(./iii:15): my ( @datawanted, @numbers ); DB2 main::(./iii:17): LOOP: foreach ( @datas ) { DB2 x $_ 0 undef DB3 s main::(./iii:18): my @test = split; DB3 x $x-^H^H Unrecognized character \x08 at (eval 8)[/usr/perl5/5.8.4/lib/perl5db.pl:619] line 2. eval '($@, $!, $^E, $,, $/, $\\, $^W) = @saved;package main; $^D = $^D | $DB::db_stop; $;- ;' called at /usr/perl5/5.8.4/lib/perl5db.pl line 619 DB::eval called at /usr/perl5/5.8.4/lib/perl5db.pl line 3349 DB::DB called at ./iii line 18 DB4 x $_ 0 '1 2 3' DB5 x @test empty array DB6 s main::(./iii:19): foreach my $num ( @numbers ) { DB6 x @test 0 1 1 2 2 3 DB7 x @numbers empty array DB8 s main::(./iii:22): push @datawanted, $_; DB8 x $num 0 undef DB9 s main::(./iii:23): push @numbers, { map { $_ = 1 } @test }; DB9 x @numbers empty array DB10 x @test 0 1 1 2 2 3 DB11 x @_ empty array DB12 x $_ 0 '1 2 3' DB13 s main::(./iii:23): push @numbers, { map { $_ = 1 } @test }; DB13 x $_ 0 1 DB14 s main::(./iii:23): push @numbers, { map { $_ = 1 } @test }; DB14 x $_ 0 2 DB15 x $_ 0 2 DB16 s main::(./iii:23): push @numbers, { map { $_ = 1 } @test }; DB16 x $_ 0 3 DB17 s main::(./iii:18): my @test = split; DB17 s main::(./iii:19): foreach my $num ( @numbers ) { DB17 x @numbers 0 HASH(0xc0b04) 1 = 1 2 = 1 3 = 1 DB18 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/