working with large integers

2011-12-29 Thread Jeswin
Hi,
I'm doing one of the Project Euler exercises which asks the sum of 100
fifty-digit numbers[1]. My code is 2 parts:

I open the text file containing the digits and put into an array:
BEGIN_CODE=
open (F, Fifty_hundred.txt) || die Could not open Fifty_hundred.txt: $!\n;

@numbers = F;
close F;
END_CODE===

Next I sum the values in the array:
BEGIN_CODE=
$sum = 0;
for (@numbers) {
$sum += $_;
}
END_CODE===

I get an answer but its wrong (according to Project Euler). Now I am
looking into the BigNum functions but I am not sure how they work and
don't really know how to use it.

Thanks



Reference:
[1] http://projecteuler.net/problem=13

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Hello a question about .+?

2011-12-29 Thread Xi Chen
Hello everyone,

I have a question about how to translate the meaning of .+?. Please
see the examples below:
SASI_Hs01_00205058  HUMAN   NM_005762   857 MISSION® siRNA 2   
 140.00
I want to get number857, I found the command below works:
perl -ne 'if (/(SASI\w+)(.+?)\s(\d+)\s/) { print $3\n; }'
but .+ or .*doesn't work. I don't know why ? is so important?

Thank you!

Xi

--
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 - Working Version

2011-12-29 Thread Jonathan Harris
On Thu, Dec 29, 2011 at 5:08 PM, Igor Dovgiy ivd.pri...@gmail.com wrote:

 Hi Jonathan,

 Let's review your script a bit, shall we? )
 It's definitely good for a starter, but still has some rough places.

  #!/usr/bin/perl
 # md5-test.plx
 use warnings;
 use strict;

 use File::Find;

 use Digest::MD5;
 use File::Spec;

 So far, so good. )


 my $dir = shift || '/Users/jonharris/Documents/begperl';

  Nice touch, setting up a default param. ) The name of variable might seem
 too generic to some, but then again, it's the only directory we deal with,
 so...


 my ($dircontents, $path, @directory, $fh, $wr_fh);

 Incoming!

 Well, it's usually better to declare your variables right before you'll
 really need them...
 Your script is short, so you'll hardly have a chance to forget what $fh
 and $wr_fh mean, though. )


 @directory = $dir;
 $path = File::Spec-catfile( @directory, $dircontents );

 Ahem. At least three 'wtf' moments for me. )
 First of all, File::Spec-catfile is really just a glorified join operator
 with some additional operations depending on which system you're using.
 So, second, it makes a little sense to convert $dir into @directory
 (documentation example is just that, an example) and to pass there
 undefined $dircontents as well.
 But the major one is why you'd ever have to pass your $dir through
 File::Spec? It's, you know, user input... )

 opendir (my $in, $path) or die Cannot open $dir: $!\n;

 So you're trying to open $path, but warn about failure to open $dir? )
 But then again, that's a minor quarrel, considering this:


 find (\wanted, $path);

 See, File::Find is convenient method which _emulates_ the whole
 opendir-readdir-closedir pattern for a given directory.
 The 'wanted' subroutine (passed by ref) will be called for each file found
 in $path.
 It's described really well in perldoc (perldoc File::Find).

 close $in;

 Opendir, but close - and not closedir? Now I'm confused. )

 opendir (my $totalin, $path) or die Cannot open $dir: $!\n;
 find (\cleanup, $path);
 close $totalin;

 You don't have to use different variable to store temporary file handle
 (which will be closed in three lines) - and that will save you a few
 moments spent working out a new (but rational) name for it. :)
 But then again, you don't need to open the same dir twice: you can call
 cleanup (with the same 'find (\cleanup)... ' syntax) whenever you want.
 And you don't really need cleanup... whoops, going too far too soon. :)

 print \n\nAll Done!\n\n;


 sub wanted {
 while ($dircontents = readdir($in)) {

 Did I say that you're using two alternative methods of doing the same
 thing? )
 But there's another big 'no-no' here: you're using external variable
 ($dircontents) when you really have perfectly zero reasons to do so.
 Of course, you don't need to push dirhandle ($in) from outer scape into
 this sub, when using find... ($File::Find::dir will do), but that's
 explainable at least. )

  if ($dircontents=~/^\./ || -d $dircontents) {
 next;
 }

 So now the script ignores all the files which names begin with '.', and
 you really wanted just to ignore '.' and '..' ... )

  my $bytes = -s $dircontents;

  print $dircontents, \n;
 print $bytes,  bytes, \tSo far so good!\n;

 Yes. )

  open $fh, , $dircontents or die $!;
 open $wr_fh, +, $path $dircontents.md5 or die $!;  ## was unable to
 concatenate here, hence sub cleanup to remove the ' '

 What was wrong with ...
 open my $wr_fh, '', File::Spec-catfile($path, $dircontents, '.md5') or
 die $!, $/
 ?


 my $hex = Digest::MD5-new-addfile($fh)-hex digest;
 print Hex Digest: , $hex, \n\n;
  print $wr_fh $hex, \n, $bytes, \n\n;

 All looks great for now: you're calculating md5 and size, and writing it
 into file with md5 extension...

 return($hex);

 ... but now you're abruptly jumping out of the while block, making the
 whole point of cycle, well, completely pointless. Not great.

 close $wr_fh;
 close $fh;

 }
 }




 # The following is mostly not original code - thanks to the author!

 sub cleanup {
 my @filelist = readdir($totalin);
  foreach my $oldname (@filelist) {
 next if -d $oldname;
 my $newname = $oldname;
  $newname =~ s/\ //;

 So you don't have spaces in your filenames. Great for you. )

  rename $oldname, $newname;
 }
 }


 # End #


 And here we finish.

 Computers are not smart. They're dumb. But they're fast. And obedient. )
 That's why they're really helpful in letting you do what you're trying to
 do... but only if you KNOW what you're trying to do.

 Imagine that you're - and not your computer - will be doing this task.
 Sit in one place - and write down your program as you (and not your
 computer) will be running it. Step by step. Bit by bit.

 Then convert your notes into some Perl form - and you'll instantly see the
 difference between now and then. )

 -- iD


Hi Igor

Many thanks for your response

I have started reviewing the things you said
There are some silly mistakes in there - eg not using closedir
It's a good 

Re: File Size Script Help - Working Version

2011-12-29 Thread Igor Dovgiy
Hi Jonathan,

Let's review your script a bit, shall we? )
It's definitely good for a starter, but still has some rough places.

#!/usr/bin/perl
 # md5-test.plx
 use warnings;
 use strict;

use File::Find;

use Digest::MD5;
 use File::Spec;

So far, so good. )


 my $dir = shift || '/Users/jonharris/Documents/begperl';

Nice touch, setting up a default param. ) The name of variable might seem
too generic to some, but then again, it's the only directory we deal with,
so...


 my ($dircontents, $path, @directory, $fh, $wr_fh);

Incoming!

Well, it's usually better to declare your variables right before you'll
really need them...
Your script is short, so you'll hardly have a chance to forget what $fh and
$wr_fh mean, though. )


 @directory = $dir;
 $path = File::Spec-catfile( @directory, $dircontents );

Ahem. At least three 'wtf' moments for me. )
First of all, File::Spec-catfile is really just a glorified join operator
with some additional operations depending on which system you're using.
So, second, it makes a little sense to convert $dir into @directory
(documentation example is just that, an example) and to pass there
undefined $dircontents as well.
But the major one is why you'd ever have to pass your $dir through
File::Spec? It's, you know, user input... )

opendir (my $in, $path) or die Cannot open $dir: $!\n;

So you're trying to open $path, but warn about failure to open $dir? )
But then again, that's a minor quarrel, considering this:


 find (\wanted, $path);

See, File::Find is convenient method which _emulates_ the whole
opendir-readdir-closedir pattern for a given directory.
The 'wanted' subroutine (passed by ref) will be called for each file found
in $path.
It's described really well in perldoc (perldoc File::Find).

close $in;

Opendir, but close - and not closedir? Now I'm confused. )

opendir (my $totalin, $path) or die Cannot open $dir: $!\n;
 find (\cleanup, $path);
 close $totalin;

You don't have to use different variable to store temporary file handle
(which will be closed in three lines) - and that will save you a few
moments spent working out a new (but rational) name for it. :)
But then again, you don't need to open the same dir twice: you can call
cleanup (with the same 'find (\cleanup)... ' syntax) whenever you want.
And you don't really need cleanup... whoops, going too far too soon. :)

print \n\nAll Done!\n\n;


 sub wanted {
 while ($dircontents = readdir($in)) {

Did I say that you're using two alternative methods of doing the same
thing? )
But there's another big 'no-no' here: you're using external variable
($dircontents) when you really have perfectly zero reasons to do so.
Of course, you don't need to push dirhandle ($in) from outer scape into
this sub, when using find... ($File::Find::dir will do), but that's
explainable at least. )

 if ($dircontents=~/^\./ || -d $dircontents) {
 next;
 }

So now the script ignores all the files which names begin with '.', and you
really wanted just to ignore '.' and '..' ... )

my $bytes = -s $dircontents;

print $dircontents, \n;
 print $bytes,  bytes, \tSo far so good!\n;

Yes. )

  open $fh, , $dircontents or die $!;
 open $wr_fh, +, $path $dircontents.md5 or die $!;  ## was unable to
 concatenate here, hence sub cleanup to remove the ' '

What was wrong with ...
open my $wr_fh, '', File::Spec-catfile($path, $dircontents, '.md5') or
die $!, $/
?


 my $hex = Digest::MD5-new-addfile($fh)-hex digest;
 print Hex Digest: , $hex, \n\n;
  print $wr_fh $hex, \n, $bytes, \n\n;

All looks great for now: you're calculating md5 and size, and writing it
into file with md5 extension...

 return($hex);

... but now you're abruptly jumping out of the while block, making the
whole point of cycle, well, completely pointless. Not great.

 close $wr_fh;
 close $fh;

}
 }




 # The following is mostly not original code - thanks to the author!

sub cleanup {
 my @filelist = readdir($totalin);
  foreach my $oldname (@filelist) {
 next if -d $oldname;
 my $newname = $oldname;
  $newname =~ s/\ //;

So you don't have spaces in your filenames. Great for you. )

 rename $oldname, $newname;
 }
 }


# End #


And here we finish.

Computers are not smart. They're dumb. But they're fast. And obedient. )
That's why they're really helpful in letting you do what you're trying to
do... but only if you KNOW what you're trying to do.

Imagine that you're - and not your computer - will be doing this task.
Sit in one place - and write down your program as you (and not your
computer) will be running it. Step by step. Bit by bit.

Then convert your notes into some Perl form - and you'll instantly see the
difference between now and then. )

-- iD


Re: Hello a question about .+?

2011-12-29 Thread Chris Stinemetz
On Thu, Dec 29, 2011 at 1:17 PM, Xi Chen cxde...@gmail.com wrote:
 Hello everyone,

 I have a question about how to translate the meaning of .+?. Please
 see the examples below:
 SASI_Hs01_00205058      HUMAN   NM_005762       857     MISSION¬Æ siRNA 2     
                    140.00
 I want to get number857, I found the command below works:
 perl -ne 'if (/(SASI\w+)(.+?)\s(\d+)\s/) { print $3\n; }'
 but .+ or .*doesn't work. I don't know why ? is so important?


I believe it makes the grouping optional.

Match 1 or 0 times

HTH,

Chris

--
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 - Working Version

2011-12-29 Thread Jonathan Harris
Hi All

Firstly, many thanks for your help previously (19/12/11) - it has led to
making a useable script
I don't think it's brilliantly written, it seems a little bodged together
to me... but works fine - not a bad result for a first script

If you are new to this problem and are interested in the previous thread, I
have attached it for you as a text file

I have done everything I can think of now to follow the previous advice
The script is portable, skips directories, creates digests of files, uses
better Perl practice (e.g., no more barewords, correct lexical file handles
etc)

I only have a couple of questions left - wondering if you can help :

- One thing that was recommended was to ensure that the File Handles are
opened outside of the loop
- I really can't figure out how to do this and keep the program working!
Doesn't it need to be inside the loop to be iterated over?

- Also, I had to open two file handles to get addfile()-hexdigest to work
so that the value could be passed - this can't be correct?!

- Writing back was messy - I was struggling with concatenating variables to
keep the script portable
- Is it possible to put values into a hash, and then print each hash entry
to a separate file?


There are clearly better ways to achieve this result - all suggestions are
gratefully received!

Thanks again

Jon


Here's the script:

#

# This program reads in files from a directory, produces a hex digest and
writes the hex, along with
# the file size into a newly created file with the same name and a '.md5'
extension, to the original directory

#!/usr/bin/perl
# md5-test.plx
use warnings;
use strict;

use File::Find;
use Digest::MD5;
use File::Spec;

my $dir = shift || '/Users/jonharris/Documents/begperl';
my ($dircontents, $path, @directory, $fh, $wr_fh);

@directory = $dir;
$path = File::Spec-catfile( @directory, $dircontents );

opendir (my $in, $path) or die Cannot open $dir: $!\n;
find (\wanted, $path);
close $in;

opendir (my $totalin, $path) or die Cannot open $dir: $!\n;
find (\cleanup, $path);
close $totalin;

print \n\nAll Done!\n\n;

sub wanted {
 while ($dircontents = readdir($in)) {
if ($dircontents=~/^\./ || -d $dircontents) {
next;
}
my $bytes = -s $dircontents;
print $dircontents, \n;
print $bytes,  bytes, \tSo far so good!\n;
 open $fh, , $dircontents or die $!;
open $wr_fh, +, $path $dircontents.md5 or die $!;  ## was unable to
concatenate here, hence sub cleanup to remove the ' '

my $hex = Digest::MD5-new-addfile($fh)-hex digest;
print Hex Digest: , $hex, \n\n;
print $wr_fh $hex, \n, $bytes, \n\n;
return($hex);

close $wr_fh;
close $fh;
}
}

# The following is mostly not original code - thanks to the author!
sub cleanup {
my @filelist = readdir($totalin);
foreach my $oldname (@filelist) {
next if -d $oldname;
my $newname = $oldname;
$newname =~ s/\ //;
rename $oldname, $newname;
}
}

# End #

Jonathan Harris
Dec 19 (10 days ago)

to beginners, me 
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;

###

Jim Gibson via perl.org 
Dec 19 (10 days ago)

to beginners 
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 

Re: working with large integers

2011-12-29 Thread Shlomi Fish
Hi Jeswin,

On Thu, 29 Dec 2011 11:40:09 -0500
Jeswin phillyj...@gmail.com wrote:

 Hi,
 I'm doing one of the Project Euler exercises which asks the sum of 100
 fifty-digit numbers[1]. My code is 2 parts:
 
 I open the text file containing the digits and put into an array:
 BEGIN_CODE=
 open (F, Fifty_hundred.txt) || die Could not open Fifty_hundred.txt: $!\n;
 

1. You should use lexical file handles.

2. You should use three arguments open.

open (my $fh, '', Fifty_hundred.txt) or die Could not open
Fifty_hundred.txt: $!.
 @numbers = F;

It would be a better idea to read it line by line, using while.

 close F;
 END_CODE===
 
 Next I sum the values in the array:
 BEGIN_CODE=
 $sum = 0;
 for (@numbers) {
 $sum += $_;
 }
 END_CODE===
 
 I get an answer but its wrong (according to Project Euler). Now I am
 looking into the BigNum functions but I am not sure how they work and
 don't really know how to use it.

See: https://metacpan.org/module/Math::BigInt - you might need some
introduction to object-oriented Perl. You can find it in the book Modern Perl
and in other resources -  http://perl-begin.org/topics/object-oriented/ .

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/

Whitespace in Python is not a problem: just lay out all the whitespace first,
then add the code around it.
— sizz on Freenode's #perl

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 - Working Version

2011-12-29 Thread John W. Krahn

Jonathan Harris wrote:


Hi Igor

Many thanks for your response

I have started reviewing the things you said
There are some silly mistakes in there - eg not using closedir
It's a good lesson in script vigilance

I found the part about opening the file handle particularly interesting
I had no idea that

open my $wr_fh, '', File::Spec-catfile($path, $dircontents, '.md5') or
die $!, $/

was possible

Now it's time to sit down and digest all this..and rewrite the script
to make it better!


Igor made a lot of good points.  Here are my two cents worth.  You are 
using the File::Find module to traverse the file system and add new 
files along the way.  This _may_ cause problems on some file systems. 
It would probably be better to get a list of applicable files first and 
then use that list to create your new files.  And you should have some 
way to handle the situation where a file exists that already has an 
'.md5' file or an '.md5' file exists with no corresponding plain file.




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Hello a question about .+?

2011-12-29 Thread Shawn H Corey

On 11-12-29 02:45 PM, Chris Stinemetz wrote:

On Thu, Dec 29, 2011 at 1:17 PM, Xi Chencxde...@gmail.com  wrote:

Hello everyone,

I have a question about how to translate the meaning of .+?. Please
see the examples below:
SASI_Hs01_00205058  HUMAN   NM_005762   857 MISSION® siRNA 2   
 140.00
I want to get number857, I found the command below works:
perl -ne 'if (/(SASI\w+)(.+?)\s(\d+)\s/) { print $3\n; }'
but .+ or .*doesn't work. I don't know why ? is so important?



I believe it makes the grouping optional.

Match 1 or 0 times


Sorry, no. It means a minimum span.

Given 'SASI_Hs01_00205058  HUMAN   NM_005762   857 MISSION® 
siRNA 2'


/(SASI\w+)(.+?)\s/ matches 'SASI_Hs01_00205058  '


/(SASI\w+)(.+)\s/ matches 'SASI_Hs01_00205058  HUMAN   NM_005762 
   857 MISSION® siRNA '


To simplify things, try: if( /(?:SASI).*?\b(\d+)\b/ ){ print $1\n; ]


--
Just my 0.0002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

Never give up your dreams.  Give up your goals, plans,
strategy, tactics, and anything that's not working but never
give up your dreams.

http://www.youtube.com/watch?v=cM5A1K6TxxM
Never, never, never give up.
  Winston Churchill

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Hello a question about .+?

2011-12-29 Thread Xi Chen
Thank you so much! This answer is very clear!!

On Thu, Dec 29, 2011 at 2:03 PM, Kronheim, David (Contr)
david.kronh...@ftr.com wrote:
 Actually, the ending ? makes the match non-greedy, in detail:
 Given:
 SASI_Hs01_00205058      HUMAN   NM_005762       857     MISSION¬Æ siRNA 2     
                    140.00
 if (/(SASI\w+)(.+?)\s(\d+)\s/) { print $3\n; }
 Match starts looking for the literal SASI followed by one or more \w's, which 
 are upper or lower case letters or digits or underscores, the charcater class:
      [a-zA-Z0-9_],
 So SASI is followed by letters, underscores or digits till the pattern finds 
 whitespace, in .+?, '.' is any character except a newline,
      plus means one or more characters that are not newlines, the '?' means 
 to match the minimal pattern/string possible,
      the maximal greedy string might grab every character up to the newline 
 (if there is one) at the end of the line.
 The minimal, non-greedy match has to allow the rest of the pattern to match 
 which is the one or more digits \d+ surronded by white space.

 Sincerely,
 David Kronheim
 Production Support Tier II
 Gateway Error Correction, VZ450 EDI, EDI Billing,  Metakey/LIA
 484-213-1315
 
 From: Chris Stinemetz [chrisstinem...@gmail.com]
 Sent: Thursday, December 29, 2011 2:45 PM
 To: Xi Chen
 Cc: beginners@perl.org
 Subject: Re: Hello a question about .+?

 On Thu, Dec 29, 2011 at 1:17 PM, Xi Chen cxde...@gmail.com wrote:
 Hello everyone,

 I have a question about how to translate the meaning of .+?. Please
 see the examples below:
 SASI_Hs01_00205058      HUMAN   NM_005762       857     MISSION¬Æ siRNA 2    
                     140.00
 I want to get number857, I found the command below works:
 perl -ne 'if (/(SASI\w+)(.+?)\s(\d+)\s/) { print $3\n; }'
 but .+ or .*doesn't work. I don't know why ? is so important?


 I believe it makes the grouping optional.

 Match 1 or 0 times

 HTH,

 Chris

 --
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/

 This communication is confidential.  Frontier only sends and receives email 
 on the basis of the terms set out at http://www.frontier.com/email_disclaimer.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




RE: Hello a question about .+?

2011-12-29 Thread Kronheim, David (Contr)
Actually, the ending ? makes the match non-greedy, in detail:
Given:
SASI_Hs01_00205058  HUMAN   NM_005762   857 MISSION® siRNA 2   
 140.00
if (/(SASI\w+)(.+?)\s(\d+)\s/) { print $3\n; }
Match starts looking for the literal SASI followed by one or more \w's, which 
are upper or lower case letters or digits or underscores, the charcater class:
  [a-zA-Z0-9_],
So SASI is followed by letters, underscores or digits till the pattern finds 
whitespace, in .+?, '.' is any character except a newline,
  plus means one or more characters that are not newlines, the '?' means to 
match the minimal pattern/string possible,
  the maximal greedy string might grab every character up to the newline 
(if there is one) at the end of the line.
The minimal, non-greedy match has to allow the rest of the pattern to match 
which is the one or more digits \d+ surronded by white space.

Sincerely,
David Kronheim
Production Support Tier II
Gateway Error Correction, VZ450 EDI, EDI Billing,  Metakey/LIA
484-213-1315

From: Chris Stinemetz [chrisstinem...@gmail.com]
Sent: Thursday, December 29, 2011 2:45 PM
To: Xi Chen
Cc: beginners@perl.org
Subject: Re: Hello a question about .+?

On Thu, Dec 29, 2011 at 1:17 PM, Xi Chen cxde...@gmail.com wrote:
 Hello everyone,

 I have a question about how to translate the meaning of .+?. Please
 see the examples below:
 SASI_Hs01_00205058  HUMAN   NM_005762   857 MISSION® siRNA 2 
140.00
 I want to get number857, I found the command below works:
 perl -ne 'if (/(SASI\w+)(.+?)\s(\d+)\s/) { print $3\n; }'
 but .+ or .*doesn't work. I don't know why ? is so important?


I believe it makes the grouping optional.

Match 1 or 0 times

HTH,

Chris

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/

This communication is confidential.  Frontier only sends and receives email on 
the basis of the terms set out at http://www.frontier.com/email_disclaimer.

--
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 - Working Version

2011-12-29 Thread Jonathan Harris
On Thu, Dec 29, 2011 at 6:39 PM, John W. Krahn jwkr...@shaw.ca wrote:

 Jonathan Harris wrote:


 Hi Igor

 Many thanks for your response

 I have started reviewing the things you said
 There are some silly mistakes in there - eg not using closedir
 It's a good lesson in script vigilance

 I found the part about opening the file handle particularly interesting
 I had no idea that

 open my $wr_fh, '', File::Spec-catfile($path, $dircontents, '.md5') or
 die $!, $/

 was possible

 Now it's time to sit down and digest all this..and rewrite the script
 to make it better!


 Igor made a lot of good points.  Here are my two cents worth.  You are
 using the File::Find module to traverse the file system and add new files
 along the way.  This _may_ cause problems on some file systems. It would
 probably be better to get a list of applicable files first and then use
 that list to create your new files.  And you should have some way to handle
 the situation where a file exists that already has an '.md5' file or an
 '.md5' file exists with no corresponding plain file.



 John
 --
 Any intelligent fool can make things bigger and
 more complex... It takes a touch of genius -
 and a lot of courage to move in the opposite
 direction.   -- Albert Einstein

 --
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/



Hi John

Thanks for your 2 cents

I hadn't considered that the module wouldn't be portable
If that is the case, then maybe it would be best to
ditch File::Find altogether?
Have you had experience with the module causing issues on certain systems?

It would be a shame, as I've just got it working! - Thanks to Igor, I no
longer use the unnecessary dir handles!

I agree that it may be worth examining the directory for existing .md5
files and skipping them
I'll look in to adding that in to the code

All the best and thanks for your help

Jonathan


Re: File Size Script Help - Working Version

2011-12-29 Thread Jonathan Harris
On Fri, Dec 30, 2011 at 12:33 AM, Jonathan Harris
jtnhar...@googlemail.comwrote:



 On Thu, Dec 29, 2011 at 6:39 PM, John W. Krahn jwkr...@shaw.ca wrote:

 Jonathan Harris wrote:


 Hi Igor

 Many thanks for your response

 I have started reviewing the things you said
 There are some silly mistakes in there - eg not using closedir
 It's a good lesson in script vigilance

 I found the part about opening the file handle particularly interesting
 I had no idea that

 open my $wr_fh, '', File::Spec-catfile($path, $dircontents, '.md5') or
 die $!, $/

 was possible

 Now it's time to sit down and digest all this..and rewrite the script
 to make it better!


 Igor made a lot of good points.  Here are my two cents worth.  You are
 using the File::Find module to traverse the file system and add new files
 along the way.  This _may_ cause problems on some file systems. It would
 probably be better to get a list of applicable files first and then use
 that list to create your new files.  And you should have some way to handle
 the situation where a file exists that already has an '.md5' file or an
 '.md5' file exists with no corresponding plain file.



 John
 --
 Any intelligent fool can make things bigger and
 more complex... It takes a touch of genius -
 and a lot of courage to move in the opposite
 direction.   -- Albert Einstein

 --
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/



 Hi John

 Thanks for your 2 cents

 I hadn't considered that the module wouldn't be portable
 If that is the case, then maybe it would be best to
 ditch File::Find altogether?
 Have you had experience with the module causing issues on certain systems?

 It would be a shame, as I've just got it working! - Thanks to Igor, I no
 longer use the unnecessary dir handles!

 I agree that it may be worth examining the directory for existing .md5
 files and skipping them
 I'll look in to adding that in to the code

 All the best and thanks for your help

 Jonathan


Hi All

Final question for Igor

I tried to use your suggestion

open my $wr_fh, '', File::Spec-catfile($path, $dircontents, '.md5') or
die $!, $/


but it returned an error on the command line:

'Not a directory'

At which point the program dies (which is what it is supposed to do!)

I used it inside the loop - sorry to bug you for clarification





if ($dircontents=~/^\./ || -d $dircontents) {

next;

}


This is also to avoid the file .DS_Store


#


FInally, I was advised by a C programmer to declare all variables at the
start of a program to avoid memory issues

Is this not necessary in Perl?


The rest of it is going really well - hope to post new and improved code
soon!


Re: File Size Script Help - Working Version

2011-12-29 Thread John W. Krahn

Jonathan Harris wrote:

On Thu, Dec 29, 2011 at 6:39 PM, John W. Krahnjwkr...@shaw.ca  wrote:


Igor made a lot of good points.  Here are my two cents worth.  You are
using the File::Find module to traverse the file system and add new files
along the way.  This _may_ cause problems on some file systems. It would
probably be better to get a list of applicable files first and then use
that list to create your new files.  And you should have some way to handle
the situation where a file exists that already has an '.md5' file or an
'.md5' file exists with no corresponding plain file.



Hi John

Thanks for your 2 cents

I hadn't considered that the module wouldn't be portable


That is not what I was implying.  I was saying that when you add new 
files to a directory that you are traversing you _may_ get irregular 
results.  It depends on how your operating system updates directory entries.




If that is the case, then maybe it would be best to
ditch File::Find altogether?
Have you had experience with the module causing issues on certain systems?

It would be a shame, as I've just got it working! - Thanks to Igor, I no
longer use the unnecessary dir handles!

I agree that it may be worth examining the directory for existing .md5
files and skipping them
I'll look in to adding that in to the code




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

--
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 - Working Version

2011-12-29 Thread John W. Krahn

Jonathan Harris wrote:


FInally, I was advised by a C programmer to declare all variables at the
start of a program to avoid memory issues

Is this not necessary in Perl?


It is not really necessary in C either.



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




What does = means?

2011-12-29 Thread Xi Chen
Hello everyone,

I saw a code below to get two same letters p in @a.

@a = qw (D D p O H p A O);
foreach $b (@a){
$n =~ /$b/i;
if($n = 2){
 $m = $b;
}
}

But I don't know what does = mean. Thank you!

Xi

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: What does = means?

2011-12-29 Thread Chris Stinemetz
On Thu, Dec 29, 2011 at 9:26 PM, Xi Chen cxde...@gmail.com wrote:
 Hello everyone,

 I saw a code below to get two same letters p in @a.

 @a = qw (D D p O H p A O);
 foreach $b (@a){
 $n =~ /$b/i;
 if($n = 2){
     $m = $b;
    }
 }

 But I don't know what does = mean. Thank you!


It is just geater than equal to inside an if condition

Chris

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: What does = means?

2011-12-29 Thread John W. Krahn

Xi Chen wrote:

Hello everyone,

I saw a code below to get two same letters p in @a.

@a = qw (D D p O H p A O);
foreach $b (@a){
$n =~ /$b/i;
if($n= 2){
  $m = $b;
 }
}

But I don't know what does = mean. Thank you!


It means greater than or equal to.  The expression $n = 2 is true 
if the value in $n is equal to 2 or is any value greater than 2, 6 for 
example.  If the value in $n is less than 2 then the expression is false.


Your algorithm looks weird though because you are testing $n for the 
presence of alphabetic characters (and then not using that information) 
and then using $n in a numerical context.




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/