RE: Split and concatenation
Thanks guys for your suggestion -Original Message- From: Shlomi Fish [mailto:shlo...@shlomifish.org] Sent: Saturday, December 17, 2011 2:48 PM To: vishnu.kuma...@wipro.com Cc: beginners@perl.org Subject: Re: Split and concatenation Hi Vishnu, On Sat, 17 Dec 2011 08:22:31 + vishnu.kuma...@wipro.com wrote: Hi, I am trying to convert the string abc.def.ghi.amm to abcdefghiamm using split and concatenation. I am missing something somewhere.. please help me to fix the code my $string = abc.def.ghi.amm; my @d = split(/\./,$string); No need for double-quotes around the string: my @d = split(/\./, $string); my $e = @d; for (my $i=0; $i $e; $i++) { This is better written as «for my $i (0 .. $#d)». print(Value of array element $i is $d[$i]\n); } #concatenation for (my $i=0; $i $e; $i++) { my $abc .= $d[$i]; } print(Value after concatenation is $abc\n); 1. You should declare $abc outside the loop and concatenate to it. 2. No need for double quotes around $d[$i]. 3. You can just use http://perldoc.perl.org/functions/join.html . So your program becomes: my $abc = join('', split(/\./, $string); In this case it can also be written using: my $abc = $string; $abc =~ s/\.//g; Or: my $abc = $string; $abc =~ tr/.//d; Regards, Shlomi Fish -- - Shlomi Fish http://www.shlomifish.org/ Star Trek: We, the Living Dead - http://shlom.in/st-wtld The number of items on an open source project’s to‐do list always grows or remains constant. Please reply to list if it's a mailing list post - http://shlom.in/reply . Please do not print this email unless it is absolutely necessary. The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments. WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email. www.wipro.com
Re: Guidance for a New Programmer/Perl User
Ken == Ken Peng short...@gmail.com writes: Ken I am 33 years old. Can I have the chance to see Perl6's official Ken stable release in my left life? Nobody is ever going to declare Perl 6 Stable. It will always be a moving target. Much the same way that Perl 5 is now... Perl 5 is hardly stable. :) Perhaps the word you're looking for is usable for *my* applications. But to answer that, you must first define your requirements. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 mer...@stonehenge.com URL:http://www.stonehenge.com/merlyn/ Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc. See http://methodsandmessages.posterous.com/ for Smalltalk discussion -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Split and concatenation
On Sat, Dec 17, 2011 at 08:22:31AM +, vishnu.kuma...@wipro.com wrote: Hi, Hello: I am trying to convert the string abc.def.ghi.amm to abcdefghiamm using split and concatenation. I am missing something somewhere.. please help me to fix the code my $string = abc.def.ghi.amm; my @d = split(/\./,$string); my $e = @d; for (my $i=0; $i $e; $i++) { print(Value of array element $i is $d[$i]\n); } #concatenation for (my $i=0; $i $e; $i++) { my $abc .= $d[$i]; } print(Value after concatenation is $abc\n); Output I am getting is Value after concatenation is amm Make sure you always use the 'strict' and 'warnings' pragmas. They should usually be the first couple of lines in every Perl script or module file you write (i.e., right after your shebang). use strict; use warnings; It looks like $abc in the file scope is not declared, and $abc within the for-loop is only visible within that loop. That explains your erroneous results. The 'strict' pragma would have made it an error to refer to $abc without declaring it. For example, executing your program with the strict and warnings pragmas yields the following output: Global symbol $abc requires explicit package name at - line 13. Execution of - aborted due to compilation errors. (Line 13 being your last line with the print statement on it) See how Perl detects the problem for you and refuses to execute until you fix it? Never write Perl code without these pragmas. :) They will save you a lot of trouble. That said, as somebody else pointed out, $abc would be undef in your print statement (if allowed to execute i.e., without the strict 'vars' pragma enabled) so your alleged output doesn't make sense given what you have shown us. The others have already shown you better ways to do this so I guess I won't repeat them. Please do not print this email unless it is absolutely necessary. /me prints this E-mail unnecessarily. :) Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: slicing array references
On 12/18/2011 08:53 PM, David Christensen wrote: beginners@perl.org: I'm working on some classes with attributes that are array and hash references, and am confused by what happens when I attempt to slice an array or hash reference. For example: 7. $ra-[0, 1, 2] evaluates to $ra-[2]. 8. $ra-[0 .. 2] produces two Use of uninitialized value in range (or flip) warnings and evaluates to $ra-[1]. you are missing a very important point in those two line. $ra-[] always accesses a single element of the array (same for a hash access). so it provides scalar context to the inside of the []. 0,1,2 in scalar context is 2 (right value of the , operator). i would be surprised not to see a warning of values in a void context or similar as the 0,1 are tossed away. the 0 .. 2 is just the scalar .. (flip flop op) inside the []. integers in that op are compares to $. (current line number from a file being read). since there is no file i/o here $. is unefined which does == to 0 so the boolean result of the .. op is 1. this is a very misguided way to get 1 from that. so remember, never use a list (or looking like a list) thing inside $ar-[] as it is always not what you expect. it is a scalar op in all cases. I see similar results with functions that return array references: 30. fra-[0, 1, 2] 31. fra-[0 .. 2] no reason to be different. the issue is what is inside the [] and not how the ref got there. And object methods: 42. $oa-ra-[0, 1, 2] 43. $oa-ra-[0, 1, 2] same issues. there is no need to try variations on how the ref got there. it is wasted coding and testing. your issue is knowing how to properly use -[] with a ref. as for slicing, it is actually a different story altogether and fairly easy. the perldoc perldata covers it some. this article i wrote a long time ago covers it in depth: http://sysarch.com/Perl/hash_slice.txt the rule is actually very simple. the basic rule for dereferencing applies. you can replace any name of a aggregate by a ref inside {} (i always use {} here). so a normal slice on an array is @foo{@keys} and the same on a ref is just @{$foo}{@keys}. done. no muss no fuss. as i said it is much cleaner and clearer to wrap the ref in {} whenever you do a full dereference (the - just accesses a single value and doesn't count here). similarly, when you just want a single item from an aggregate reference, always use the - notation. it is much cleaner than this form: $$ref{$key} # nasty to read ${$ref}{$key} # this is slightly better $ref-{$key} # this is best by far so in conclusion, slicing is not super common but can save some coding when used right. it makes no difference where the ref comes from. slicing always puts the [] or {} in list context. accessing a single element is best done with $ref- notation and it always puts the inside of []/{} in scalar context. slices are meant for a list of accesses (read OR write) and - is meant for a single access. uri -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
no regrets after doing this venture!!
pwhats up...brtruly i am presently on the path to all my most treasured goalsbra href=http://matematikdestekmerkezi.com/profile/59GeoffreyHarrison/;http://matematikdestekmerkezi.com/profile/59GeoffreyHarrison//abrtalk to you later./p
re: File Size Script Help
Hi Perl Pros This is my first call for help I am a totally new, self teaching, Perl hopeful If my approach to this script is simply wrong, please let me know as it will help my learning! The script aims to: 1) Read in a directory either from the command line, or from a default path 2) Produce a hash for future checksum 3) Write this (hex digest) to a separate file, in a sub directory of the parent, which has the same name and a .md5 extension 4) Check the original file for its size 5) Add this data to the newly created file on a new line (in bytes) I have a script that will do most of this, except for analysing the file size - I think that the file size being analysed may be the md5 object result as the same value is printed to each file I am running out of ideas and would appreciate any help you could give! I have tried using File::stat::OO and File::stat - but to no avail - I could be using them incorrectly! Many thanks in advance... Jon. Here are some details: System: Mac OSX 10.7.2 Perl version 5.12 Script: #!/usr/bin/perl # md5-test-3.plx use warnings; use strict; use Digest::MD5; my $filesize = 0; my $dir = shift || '/Users/jonharris/Documents/begperl'; opendir (DH, $dir) or die Couldn't open directory: $!; my $md5 = Digest::MD5-new; while ($_ = readdir(DH)) { $md5-add($_); $filesize = (stat(DH))[7]; Is it necessary to put the following into a new loop? foreach ($_) { open FH, /Users/jonharris/Documents/begperl/md5/$_.md5 or die $!; binmode(FH); print FH $md5-hexdigest, \n, $filesize; } close FH; } close DH; print \n$dir\n\n; ###
Re: File Size Script Help
On 12/19/11 Mon Dec 19, 2011 11:32 AM, Jonathan Harris jtnhar...@googlemail.com scribbled: Hi Perl Pros This is my first call for help I am a totally new, self teaching, Perl hopeful If my approach to this script is simply wrong, please let me know as it will help my learning! The script aims to: 1) Read in a directory either from the command line, or from a default path 2) Produce a hash for future checksum A hash of what? File names (directory contents) or file contents? 3) Write this (hex digest) to a separate file, in a sub directory of the parent, which has the same name and a .md5 extension Same name as the file or same name as the directory? 4) Check the original file for its size 5) Add this data to the newly created file on a new line (in bytes) Will this file contain information for one file or many files? I have a script that will do most of this, except for analysing the file size - I think that the file size being analysed may be the md5 object result as the same value is printed to each file Print out the file size returned by stat. Check if it is the same displayed by the ls command. I am running out of ideas and would appreciate any help you could give! I have tried using File::stat::OO and File::stat - but to no avail - I could be using them incorrectly! I am afraid I do not understand exactly what you are trying to accomplish. I can't tell from your program whether or not you will end up with one digest file for the entire directory, or one digest file for each file in the directory. Many thanks in advance... Jon. Here are some details: System: Mac OSX 10.7.2 Perl version 5.12 Script: #!/usr/bin/perl # md5-test-3.plx use warnings; use strict; use Digest::MD5; my $filesize = 0; You should declare variables where they are needed and not before. my $dir = shift || '/Users/jonharris/Documents/begperl'; opendir (DH, $dir) or die Couldn't open directory: $!; my $md5 = Digest::MD5-new; while ($_ = readdir(DH)) { You are better off using a scalar variable and not the default variable, which can get reused and overwritten: while( my $file = readdir(DH) ) { $md5-add($_); You are adding file names to a string to be digested. Is that what you want? Or do you want to calculate a digest for the contents of each file? I have not used Digest::MD5, but if you to calculate the digest for the contents of each file, you want to create a new digest object, open the file, and use the addfile() method, then hexdigest() for each file. $filesize = (stat(DH))[7]; You are applying stat to the directory read handle. You want to fetch data for the file (untested): my $filesize = (stat($dir/$file))[7]; Note that you must prefix the file name with its full path. Is it necessary to put the following into a new loop? No. It makes no sense to have a one-iteration loop. foreach ($_) { open FH, /Users/jonharris/Documents/begperl/md5/$_.md5 or die $!; You are appending lines to a file with a name that is based on an existing file. Why? binmode(FH); There is no need to set the mode of the output file to binary. Both hexdigest and the file size will be written in ascii characters, not binary data. print FH $md5-hexdigest, \n, $filesize; } close FH; } close DH; print \n$dir\n\n; ### -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: File Size Script Help
Hi Jonathan, some comments on your code - both positive and negative. On Mon, 19 Dec 2011 19:32:10 + Jonathan Harris jtnhar...@googlemail.com wrote: Hi Perl Pros This is my first call for help I am a totally new, self teaching, Perl hopeful If my approach to this script is simply wrong, please let me know as it will help my learning! The script aims to: 1) Read in a directory either from the command line, or from a default path 2) Produce a hash for future checksum 3) Write this (hex digest) to a separate file, in a sub directory of the parent, which has the same name and a .md5 extension 4) Check the original file for its size 5) Add this data to the newly created file on a new line (in bytes) I have a script that will do most of this, except for analysing the file size - I think that the file size being analysed may be the md5 object result as the same value is printed to each file I am running out of ideas and would appreciate any help you could give! I have tried using File::stat::OO and File::stat - but to no avail - I could be using them incorrectly! Many thanks in advance... Jon. Here are some details: System: Mac OSX 10.7.2 Perl version 5.12 Script: #!/usr/bin/perl # md5-test-3.plx use warnings; use strict; strict and warnings are good. use Digest::MD5; So is using a module. my $filesize = 0; You shouldn't predeclare your variables. my $dir = shift || '/Users/jonharris/Documents/begperl'; opendir (DH, $dir) or die Couldn't open directory: $!; Don't use bareword dir handles - use lexical ones. It's good that you're using the or die thing, though. my $md5 = Digest::MD5-new; Seems like you're using the same $md5 object times and again which will calculate cumulative MD5 sums instead of per-file ones. while ($_ = readdir(DH)) { 1. You're not skipping . and ... 2. You're not skipping other directories. 3. The $_ variable can be easily devastated. You should use a lexical one. $md5-add($_); According to http://metacpan.org/module/Digest::MD5 the add() methods adds data, and here it will only add the filename. You need to use addfile() with an opened file handle instead. $filesize = (stat(DH))[7]; You shouldn't stat the directory handle. Instead stat $dir/$filename (you can also use the core File::Spec module if you want to make it extra portable). Is it necessary to put the following into a new loop? foreach ($_) { Why the foreach ($_) here? It does nothing. You're already iterating on the files. open FH, /Users/jonharris/Documents/begperl/md5/$_.md5 or die $!; binmode(FH); print FH $md5-hexdigest, \n, $filesize; } close FH; Use lexical file handles here, use three-args open - «open my $fh, '', '/Users'» and don't open it time and again. Keep it outside the loop. For more information see: http://perl-begin.org/tutorials/bad-elements/ Regards, Shlomi Fish } close DH; print \n$dir\n\n; ### -- - Shlomi Fish http://www.shlomifish.org/ Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/ We don’t know his cellphone number, and even if we did, we would tell you that we didn’t know it. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: File Size Script Help
On Mon, Dec 19, 2011 at 8:08 PM, Jim Gibson jimsgib...@gmail.com wrote: On 12/19/11 Mon Dec 19, 2011 11:32 AM, Jonathan Harris jtnhar...@googlemail.com scribbled: Hi Perl Pros This is my first call for help I am a totally new, self teaching, Perl hopeful If my approach to this script is simply wrong, please let me know as it will help my learning! The script aims to: 1) Read in a directory either from the command line, or from a default path 2) Produce a hash for future checksum A hash of what? File names (directory contents) or file contents? 3) Write this (hex digest) to a separate file, in a sub directory of the parent, which has the same name and a .md5 extension Same name as the file or same name as the directory? 4) Check the original file for its size 5) Add this data to the newly created file on a new line (in bytes) Will this file contain information for one file or many files? I have a script that will do most of this, except for analysing the file size - I think that the file size being analysed may be the md5 object result as the same value is printed to each file Print out the file size returned by stat. Check if it is the same displayed by the ls command. I am running out of ideas and would appreciate any help you could give! I have tried using File::stat::OO and File::stat - but to no avail - I could be using them incorrectly! I am afraid I do not understand exactly what you are trying to accomplish. I can't tell from your program whether or not you will end up with one digest file for the entire directory, or one digest file for each file in the directory. Many thanks in advance... Jon. Here are some details: System: Mac OSX 10.7.2 Perl version 5.12 Script: #!/usr/bin/perl # md5-test-3.plx use warnings; use strict; use Digest::MD5; my $filesize = 0; You should declare variables where they are needed and not before. my $dir = shift || '/Users/jonharris/Documents/begperl'; opendir (DH, $dir) or die Couldn't open directory: $!; my $md5 = Digest::MD5-new; while ($_ = readdir(DH)) { You are better off using a scalar variable and not the default variable, which can get reused and overwritten: while( my $file = readdir(DH) ) { $md5-add($_); You are adding file names to a string to be digested. Is that what you want? Or do you want to calculate a digest for the contents of each file? I have not used Digest::MD5, but if you to calculate the digest for the contents of each file, you want to create a new digest object, open the file, and use the addfile() method, then hexdigest() for each file. $filesize = (stat(DH))[7]; You are applying stat to the directory read handle. You want to fetch data for the file (untested): my $filesize = (stat($dir/$file))[7]; Note that you must prefix the file name with its full path. Is it necessary to put the following into a new loop? No. It makes no sense to have a one-iteration loop. foreach ($_) { open FH, /Users/jonharris/Documents/begperl/md5/$_.md5 or die $!; You are appending lines to a file with a name that is based on an existing file. Why? binmode(FH); There is no need to set the mode of the output file to binary. Both hexdigest and the file size will be written in ascii characters, not binary data. print FH $md5-hexdigest, \n, $filesize; } close FH; } close DH; print \n$dir\n\n; ### -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ Hi Jim Thanks for responding Here are some answers to your questions: A hash of what? File names (directory contents) or file contents? The aim is to produce a digest for each file in the folder Same name as the file or same name as the directory? Each file created will have the same filename as the original, but with the extension '.md5' Will this file contain information for one file or many files? This file will contain information for only one file This is so that when looking at the treated directory, the end result will be: some_movie.mov some_movie.mov.md5 another_movie.mov another_movie.mov.md5 etc... Print out the file size returned by stat. Check if it is the same displayed by the ls command. I really have! However, this is where I am coming unstuck I am afraid I do not understand exactly what you are trying to accomplish. I can't tell from your program whether or not you will end up with one digest file for the entire directory, or one digest file for each file in the directory. The program creates one digest file for each file in the directory : ) my $filesize = 0; You should declare variables where they are needed and not before. Noted my $md5 = Digest::MD5-new; while ($_ = readdir(DH)) { You are better off using a scalar variable and not the default variable,
Re: File Size Script Help
On Mon, Dec 19, 2011 at 8:09 PM, Shlomi Fish shlo...@shlomifish.org wrote: Hi Jonathan, some comments on your code - both positive and negative. On Mon, 19 Dec 2011 19:32:10 + Jonathan Harris jtnhar...@googlemail.com wrote: Hi Perl Pros This is my first call for help I am a totally new, self teaching, Perl hopeful If my approach to this script is simply wrong, please let me know as it will help my learning! The script aims to: 1) Read in a directory either from the command line, or from a default path 2) Produce a hash for future checksum 3) Write this (hex digest) to a separate file, in a sub directory of the parent, which has the same name and a .md5 extension 4) Check the original file for its size 5) Add this data to the newly created file on a new line (in bytes) I have a script that will do most of this, except for analysing the file size - I think that the file size being analysed may be the md5 object result as the same value is printed to each file I am running out of ideas and would appreciate any help you could give! I have tried using File::stat::OO and File::stat - but to no avail - I could be using them incorrectly! Many thanks in advance... Jon. Here are some details: System: Mac OSX 10.7.2 Perl version 5.12 Script: #!/usr/bin/perl # md5-test-3.plx use warnings; use strict; strict and warnings are good. use Digest::MD5; So is using a module. my $filesize = 0; You shouldn't predeclare your variables. my $dir = shift || '/Users/jonharris/Documents/begperl'; opendir (DH, $dir) or die Couldn't open directory: $!; Don't use bareword dir handles - use lexical ones. It's good that you're using the or die thing, though. my $md5 = Digest::MD5-new; Seems like you're using the same $md5 object times and again which will calculate cumulative MD5 sums instead of per-file ones. while ($_ = readdir(DH)) { 1. You're not skipping . and ... 2. You're not skipping other directories. 3. The $_ variable can be easily devastated. You should use a lexical one. $md5-add($_); According to http://metacpan.org/module/Digest::MD5 the add() methods adds data, and here it will only add the filename. You need to use addfile() with an opened file handle instead. $filesize = (stat(DH))[7]; You shouldn't stat the directory handle. Instead stat $dir/$filename (you can also use the core File::Spec module if you want to make it extra portable). Is it necessary to put the following into a new loop? foreach ($_) { Why the foreach ($_) here? It does nothing. You're already iterating on the files. open FH, /Users/jonharris/Documents/begperl/md5/$_.md5 or die $!; binmode(FH); print FH $md5-hexdigest, \n, $filesize; } close FH; Use lexical file handles here, use three-args open - «open my $fh, '', '/Users'» and don't open it time and again. Keep it outside the loop. For more information see: http://perl-begin.org/tutorials/bad-elements/ Regards, Shlomi Fish } close DH; print \n$dir\n\n; ### -- - Shlomi Fish http://www.shlomifish.org/ Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/ We don’t know his cellphone number, and even if we did, we would tell you that we didn’t know it. Please reply to list if it's a mailing list post - http://shlom.in/reply . Hi Shlomi Thanks for your response To answer your questions: my $filesize = 0; You shouldn't predeclare your variables Noted, thanks my $dir = shift || '/Users/jonharris/Documents/begperl'; opendir (DH, $dir) or die Couldn't open directory: $!; Don't use bareword dir handles - use lexical ones. It's good that you're using the or die thing, though. Of course - will have to re-read the section on barewords... my $md5 = Digest::MD5-new; Seems like you're using the same $md5 object times and again which will calculate cumulative MD5 sums instead of per-file ones. In which case, I think that it will have to go in the loop so that a new instance is produced each time? while ($_ = readdir(DH)) { 1. You're not skipping . and ... 2. You're not skipping other directories. I'm sure I read somewhere, that the parent was automatically skipped Must be getting confused However, adding this will be ok: next if $_ eq . or $_ eq ..; 3. The $_ variable can be easily devastated. You should use a lexical one. I'll certainly use lexical in the future $md5-add($_); According to http://metacpan.org/module/Digest::MD5 the add() methods adds data, and here it will only add the filename. You need to use addfile() with an opened file handle instead. That makes sense $filesize = (stat(DH))[7]; You shouldn't stat the directory handle. Instead stat $dir/$filename (you can also use the core File::Spec module if you want to make