Re: Sandboxing while I am learning

2011-08-31 Thread Brian F. Yulga

Marc wrote:

Shawn,

   

if you use perlbrew and local::lib you
can test different perl versions and then different environments.
 

I haven't looked into local::lib yet.  What advantage does that give 
you over a plain perlbrew install?

Marc
   


I haven't used local::lib very much, because perlbrew seems to be the 
easiest catch-all solution for my purposes.  Once I discovered the 
--as installation parameter (e.g. perlbrew install 5.14.1 --as 
perl514_sandbox2, etc.), I had very little use for local::lib.  Since 
perlbrew sets @INC to line up with the --as in each installation, one 
can create multiple isolated installations of the same Perl version.  In 
my (limited) experience, cpan worked correctly under whichever 
installation was marked active with perlbrew use (or switch).


The downside of this approach is that I'm compiling the same version of 
Perl multiple times, for the sole purpose of different installation 
directories.  That takes time and hard drive space.  So I can see how 
local::lib may be better if those are critical issues.  But for me, 
adding another ~70MB copy of Perl to my home directory is 
inconsequential when weighed against the ease of using one tool to do 
the job.  Each to their own.


Besides saved disk space and install time, I'd be interested in knowing 
if I've overlooked some other advantage of local::lib over perlbrew.


Brian

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




Re: How to run a perl script in background on Windows?

2011-08-03 Thread Brian F. Yulga

Jose Marquez wrote:

Hi there
  
Sure I am the newbiest of all Perl newbies in this group 
  
Just have been reading some of the posts you all have sent since I subscribe to this list 
  
What I'm trying to learn these days is how I run a Perl script in background on Windows. Can anybody give a hint on it?
   

Hi Jose,

As far as I know, the Windows cmd.exe environment doesn't let you 
background a process the same way a unix shell does with ''.  However, 
I have used start to achieve a similar effect when launching a script:


C:\  start /b perl script.pl

Type start /? at a cmd prompt to read about the /B switch.  With 
this switch, you'll still have the cmd.exe window open, but the prompt 
will return immediately.


Also, I use this with Strawberry Perl; I don't know if ActiveState has 
other tricks you can use.


Brian

  
Appreciate very much all the help you guys can provide me
  
Cheers
  
JM


   


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




Re: using Perl, How to search for files in my computer

2011-06-12 Thread Brian F. Yulga

Sayth Renshaw wrote:

Try:

my @Music_files = ();

use File::Find;
find( \want_mp3, 'd:\\' );
print $_\n for sort @Music_files;

sub want_mp3 {
  push @Music_files, $File::Find::name if $_ =~ /\.mp3$/;
}


--
Just my 0.0002 million dollars worth,
  Shawn

Confusion is the first step of understanding.

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

The secret to great software:  Fail early  often.

Eliminate software piracy:  use only FLOSS.

 

That was good to see Shawn. I kept working on it but mine was far more
convoluted, and didn't like spaces in directory names.

#!\usr\bin\perl
use warnings;
use strict;
use diagnostics;
use File::List;
use File::Find;

my $mp3;
my @musicFiles;
my $dirList;
my $dir = (c:/);
my @dirList = find(sub {print if -d}, $dir);
# old file line $mp3 = new
File::List(C:/Users/RenshawFamily/maven/Music/Foo Fighters);
foreach $dirList (@dirList) {
@musicFiles = @{ $dirList-find(\.mp3\$) };
}
@musicFiles = sort(@musicFiles);
foreach (@musicFiles)
{
  print  $_ , \n;
}

Sayth

   

Hi Sayth, et. al.,

At work I'm stuck on Winblows, and this is one of the ways Strawberry 
Perl (portable install) saves me lots of time.  There's nothing wrong 
with the other solutions presented; I'm just adding to the variety...  I 
use these one-liners if I need a quick search while playing in DOS 
(doesn't sort, just prints as it gets 'em):


perl -MFile::Find -wle find( sub{ /\.mp3$/i and print;}, q(.));

perl -MFile::Find -wle find( sub{ /\.mp3$/i and print 
$File::Find::name;}, q(.));


The first I tend to use when I just want an  existence check and I 
don't need the directory info.


\Brian

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




Re: using Perl, How to search for files in my computer

2011-06-12 Thread Brian F. Yulga

shawn wilson wrote:

On Sun, Jun 12, 2011 at 14:38, Brian F. Yulgabyu...@langly.dyndns.org  wrote:
   


perl -MFile::Find -wle find( sub{ /\.mp3$/i and print;}, q(.));

 

just curious, what's the difference (in speed or results) between this
and 'dis/a/s/b *.mp3'?
obviously File::Find and perl are more powerful, but that doesn't seem
like the right use case for it?

   

Hi Shawn,

Good point... For a simple printing of results, 'dir' is noticeably 
faster and gives the same results.  Actually, before I learned how to 
use File::Find, I used to pipe the output from 'dir' to perl to filter 
results with a regex:


dir /s /b | perl -wnle /regex/ and print;

...essentially using perl as 'grep', and it's faster than the File::Find 
solution (well, I don't have benchmarks to prove it, but File::Find 
seemed slower to me).  Although in my rather basic uses, the performance 
difference is inconsequential, and either way beats using the Windows 
Search Companion.


\Brian

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




Re: how do i push a hashref into another hashref

2011-06-07 Thread Brian F. Yulga

Leo Lapworth wrote:

Hi Agnello,

On 7 June 2011 07:47, Agnello Georgeagnello.dso...@gmail.com  wrote:
   

I got the following hashref
 

snip
   

now i need to push $select_all_website into $selet_domU_data
 

I think this is what you want...

use strict;
use warnings;

my $hash_ref_1 = {
 a =  1,
 b =  2,
};

my $hash_ref_2 = {
 a =  3,
 c =  4,
};

my $new_hash_ref = {
 %{$hash_ref_1},
 %{$hash_ref_2}
};

use Data::Dumper;
print Dumper($new_hash_ref);

Which produces:

$VAR1 = {
   'c' =  4,
   'a' =  3,
   'b' =  2
 };

   

Hi Leo,

You are probably close to what the OP wanted; it is hard to read minds 
since pushing onto hashes makes no sense.  I would call it merging 
two hashes.  For the OP's data maybe this isn't an issue (and hence why 
you didn't state it), but in your sample data, the value for key 'a' in 
hash_ref_1 is overwritten by the value in hash_ref_2 during the merge.  
Even though your way is short and concise, perhaps it's safer to check 
each key before assignment?  I suppose the decision to check depends on 
the predictability of the source data...


my $combined_hash = { %$hash_ref_1 };

foreach my $key_in_hsh2 ( keys %$hash_ref_2 ) {

if (exists $hash_ref_1-{$key_in_hsh2} ) {
# some algorithm to deal with duplicate keys or alert user
warn found duplicate key '$key_in_hsh2', keeping original 
value\n;


} else {
$combined_hash-{$key_in_hsh2} = $hash_ref_2-{$key_in_hsh2}
}
}




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




Re: Real Beginner

2011-04-21 Thread Brian F. Yulga

Tiago Hori wrote:

Hey Guys,

I am a real beginner so at the risk of being slammed by some, I wanted to
get some input.
   
I'm a beginner, too.  I've worked through this chapter somewhat 
recently, so my advice shouldn't be too hard to understand :-)  
Hopefully _I_ don't get slammed!

I was trying to do exercise 5 of the 5th edition of learning perl:

   
You forgot to include Chapter 4 but that's ok, it wasn't hard to find 
based on the context since I had the book right here :-)


I'll snip most of your code to the part that I think may be what's 
slowing you down.





my @peoplec = @people;

my $lastperson = shift @peoplec;

my $beforelast = shift @peoplec;


   
I think I see what you did, but you can probably make it easier by 
taking advantage of how 'print' handles arrays as a whole.  An example:


#!/usr/bin/perl -wl
use strict;

my @designations = ( 'red', 'two', 'rogue', 'leader' );
print my array contains: @designations;

#  displays the following
#  my array contains: red two rogue leader

Then you shouldn't have to iterate through each item of the array, or 
'shift' off items from a duplicated array.  The problem didn't include 
commas in the output, which (for me), signaled that I could probably 
just 'print' the entire array at once.




ground. I have to say this list is a bit daunting  and does not look like a
beginners list at all. I think it, as Cat Stevens would say, a wild world
out there, but some of us are just trying to get by.

That's my view from outside, really outside.

   

I hear ya!  Keep trying :-)

Brian


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




Re: Nature of this list

2011-04-19 Thread Brian F. Yulga


Randal L. Schwartz wrote:


If they don't have a thick skin already, they'd better get one soon, or
they'll be useless as an advanced programmer.  Without thick skin, they
won't be able to submit their code for review regardless of how much
their ego is invested in it.

   
I've been following this list for a few months now, and I've posted a 
handful of times (both for questions and possible solutions).  The first 
time I ventured some guesses in unknown territory, I got struck down 
pretty fast.  It does kind of suck to spend some time thinking about how 
things work, post an idea, and then read a you're completely 
wrong-style, blunt reply.


I can see how some newbies could get scared away by this atmosphere, and 
indeed I have thought twice before posting questions or replying too 
quickly.  But, I have accepted it almost like a Perl initiation 
trial.  I figure, if I stick with it long enough, I can be helped past 
the newbie level of Perl understanding.


Indeed, this list has been an amazing resource for me.  Since I'm 
learning Perl on my own, this list is like a virtual classroom.  I enjoy 
seeing variations on a theme (good and bad), even after a question has 
been answered, because it gives me insight on how to think about these 
problems in different ways.  If I have to thicken my skin a bit to 
accept criticism of my code, it's worth it for the knowledge and 
understanding gained.


As a side note, I must confess a slight sense of awe (and much 
appreciation), that the authors of modules I've used, and even author of 
two BOOKS I am learning from, are willing to take any amount of time to 
answer our beginner questions.  When I first decided to tackle Perl, I 
did not expect to have such live expert advice readily available.


Since this is a community effort, and the experts aren't getting 
monetarily compensated for their contributions here, I can understand 
the tendency toward blunt responses.  But it is commendable that some 
contributors (Shlomi, et.al.) are trying to make the community a little 
more friendly to newcomers.


To be perfectly honest, I'm glad that both camps exist here.  It helps 
in my Perl education, but also prepares me for the real world.  I'd 
much rather have my code ripped apart here on this list, rather than 
later when it is my entry-level IT job on the line during a code 
review.  I may as well be humbled early on!


On the flip side, for the hobbyist programmer, without some 
hand-holding, they might bail and switch to an easier (or more trendy) 
language to learn (I don't dare give names ;-)


I guess this community has to decide if it wants the list to cater to 
beginning IT professionals, amateur programming hobbyists, or both.  
From my newbie perspective, I think this list does a pretty good job 
with both overall.  Even in the context of the current debate, I feel 
the viewpoints people have contributed to the list are all insightful, 
and not excessively negative in tone.  Shall we all say bygones and 
move on?  ;-)


Brian


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




Re: How to test for 2 words from a list in a form field?

2011-04-09 Thread Brian F. Yulga

shawn wilson wrote:

On Sat, Apr 9, 2011 at 3:29 PM,sono...@fannullone.us  wrote:
   



my $address_count = () = $str =~ /box|street|avenue|lane|apo/ig;
   

what does print $var = () = $str =~ /regex/; do?
particularly the '= () ='?
   


According to Effective Perl Programming (2nd Ed.), Item 9 (Know the 
difference between lists and arrays)...
(I couldn't find the text online, so my apologies for poorly 
paraphrasing the book)
Since the assignment operator is right associative, the '() = ...' 
happens first, which forces the assignment operator to act in list 
context on the regex match on the right.  Then the '$var =' to the left 
is the assignment operator in scalar context, which returns the number 
of elements from the list on the right.


This code can be used to count the number of matches from your regex, if 
you don't care to do anything with the actual matches.


my $count =()= m/(.)/g;

Apparently this is called the goatse operator.  no kidding; that's 
what the book says.


per the question, maybe something like this:
my @match = [ qr/one/i, qr/two/i, qr/etc/i ];
my @words = split /[^\s]+/, $fields;
my $count = 0;
foreach my $word (@words) {
  $count++ if( $word ~~ @match );
}
print cool\n if $count= 2;

   
How does this code work?  For one thing, you have @match = [ ]; which is 
assigning an array reference to an array?  Maybe I'm not understanding 
something...  I tweaked your code to the following, since I had problems 
compiling it as you wrote it:


my $match = [ qr/one/i, qr/two/i, qr/etc/i ];
my @words = split /\s/, $fields;
my $count = 0;
foreach my $word (@words) {
foreach (@$match) {
if ( $word =~ $_ ) {
$count++;
last;
}
}
}
print cool\n if $count = 2;

It's certainly not as short as your code.  Is this basically the same 
logic you were going for?


Here's a slightly different take I thought a 'map' or 'grep' might 
be a cleaner way to go.


my @mail_types = qw( avenue road box lane );
my @words = split /\s/, $fields;
my $count = grep {
my $found;
foreach my $street ( @mail_types ) {
if ( /\b$street\b/i ) {
$found++;
last;
}
}
$found;
} @words;
print qq(found $count address types\n) if ( $count  1);


I think I was wrong; this is even longer, and too convoluted I'm 
sure there's a better way...


Brian

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




Re: How to test for 2 words from a list in a form field?

2011-04-09 Thread Brian F. Yulga

Brian F. Yulga wrote:


my @mail_types = qw( avenue road box lane );
my @words = split /\s/, $fields;
my $count = grep {
my $found;
foreach my $street ( @mail_types ) {
if ( /\b$street\b/i ) {
$found++;
last;
}
}
$found;
} @words;
print qq(found $count address types\n) if ( $count  1);


I think I was wrong; this is even longer, and too convoluted I'm 
sure there's a better way...



Of course, two minutes after I send to the list, I get a much better 
idea.  My apologies for spamming all of you  How about this:



my @mail_types = qw( street road box lane );

my $regex;
$regex .= q(\b) . $_ . q(\b|) foreach ( @mail_types );
$regex = substr $regex, 0, -1;

my $count = grep /$regex/i, @words;

print qq(found $count\n) if ( $count  1);


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




Re: Filling in an online form.

2011-03-28 Thread Brian F. Yulga

Dennis Wicks wrote:

Greetings;

I want to setup a form on a web page that a visitor can fill in then 
send it back to me by email so I can browse it online and print it out 
locally in the correct format. I have the way to define the input form 
figured out, but the transmission and browsing and printing the form 
are giving me trouble, to say the least!


My public web server (Apache) is not local. I have a local Apache2 web 
server that I use for testing.


Can anybody point me to a tutorial or example of how to do this? 
(Using perl, of course!)


Many TIA!
Dennis

I'm pretty new to Perl, but I'll try to tackle this one with what I know 
so far


First of all, I'm not sure about what you mean by browse it online; do 
you want to store the content in a database?  From my perspective your 
question seems vague.  You'll have to define that more precisely...


For the first half of your problem that I think I understand, I'll 
assume you need information on using CGI coupled with sending e-mail.  I 
recently learned some of the basics of using Perl with the web through 
Chapter 14 - Introduction to CGI, of Beginning Perl, (3rd Ed.) by 
James Lee.  It's not complete (for example, lacks security 
considerations), but the chapter reads like a tutorial and I found it 
easy to follow along and build the example script at the end of the 
chapter (webchess.pl).  The book also refers the reader to Official 
Guide to Programming with CGI.pm by Lincoln Stein (the module's author).


For official PerlDoc online documentation with an example, check out:
http://perldoc.perl.org/CGI.html#A-COMPLETE-EXAMPLE-OF-A-SIMPLE-FORM-BASED-SCRIPT

Working with the example above, the do_work() subroutine processes the 
form parameters.  You should be able to apply the same concept to your 
problem, and have do_work() grab the posted data ( via the keys in the 
param() method ) and output to an e-mail, using the Mail::Mailer module:

http://perldoc.perl.org/perlfaq9.html#How-do-I-send-mail?
http://search.cpan.org/~markov/MailTools-2.07/lib/Mail/Mailer.pod

Hope that gets you a start.

Brian


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




Re: Using regex special characters from a string

2011-03-09 Thread Brian F. Yulga


Uri Guttman wrote:
 BL == Ben Lavery ben.lav...@gmail.com writes:


   BL #Here, using a hash looks much cleaner than 
iterating through an array


 hashes are almost always better for token lookups than scanning
 arrays. don't doubt yourself in this area.




Jim Gibson wrote:
 On 3/8/11 Tue  Mar 8, 2011  2:51 PM, Ben Lavery ben.lav...@gmail.com
 scribbled:


 I'm wondering if I need to convert my hash of valid words into an 
array, then

 iterate through each entry with something like:
 if($word_list[i] =~ m/$temp_word/){
 push(@all_combinations, $temp_word);
 }

 You can use the built-in grep function to compare each of the words 
in your

 word list to your specified pattern, used as a regular expression, and
 return all that match. In that case, the period is fine, as it will match
 any character:

 my @matches = grep( /^$temp_word$/i, @word_list );
 push( @all_combinations, @matches );




Uri and Jim have hit upon one of my major stumbling blocks with learning 
Perl.  There seems to be a difference of opinion on the proper times to 
use hashes vs. arrays/lists...and how best to use them.  For those that 
have heard of this book, I'm currently digesting this piece of information:


Effective Perl Programming, 2nd Ed., Item 9: Know the difference 
between lists and arrays.


I have this suspicion that it relates to how C/C++ programmers write C 
programs in Perl, rather than Perl programs.  For instance, someone in 
the C mindset might be tempted to write the above 'grep' as (following 
code is a bad idea, I know...):


foreach ( @word_list ) {
if ( /^$temp_word$/i ) {
push( @all_combinations, ( $_ ));
}
}

I understand that this is very inefficient in Perl, and (please correct 
me if I'm wrong) that Perl is optimized to search and manipulate hashes 
and lists as a whole, rather than executing code over each item 
one-at-a-time.  So, I get the idea that I should treat a list of items 
as a LIST (as a whole), rather than the conventional thinking of 
enumerating each item of a list into an array.  Please tell me, gurus, 
if I'm on the right track!  ;-)


So, my question becomes, why is hash manipulation more efficient than 
list manipulation??  Uri states that hashes are better for for token 
lookups than scanning arrays.  But each key in the hash has an unused 
associated value (1) in this case, which increases the amount of data 
stored in memory.  Maybe that's inconsequential.  But, if we grep a 
list (as Jim suggests) instead of scan an array, shouldn't the list 
manipulation be more efficient (or at least equivalently efficient) to a 
hash lookup?


Thanks in advance for your sharing your thoughts...

Brian



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




Re: Using regex special characters from a string

2011-03-09 Thread Brian F. Yulga

Jim Gibson wrote:

 On 3/9/11 Wed  Mar 9, 2011  9:22 AM, Brian F. Yulga
 byu...@langly.dyndns.org scribbled:

 foreach ( @word_list ) { if ( /^$temp_word$/i ) { push(
 @all_combinations, ( $_ )); } }

 That is pretty much what the grep function is doing. It has to
 iterate over the entire array and evaluate its code block for each
 element and form a list of the array elements that evaluate to true.
 There is no way to eliminate the requirement to scan the entire
 array.



Ah, my (poor) assumption was that 'grep' was more magical in its 
internal mechanisms for searching.  Since it still acts on each member 
of the list, the rest of my argument is defunct.




 I understand that this is very inefficient in Perl, and (please
 correct me if I'm wrong)

 You are wrong.

 each item of a list into an array.  Please tell me, gurus, if I'm
 on the right track!  ;-)

 You are not.



Ouch.  Well, I'm here to learn.



 So, my question becomes, why is hash manipulation more efficient
 than list manipulation??

 Because to find an element in a hash does not require iterating over
 the entire hash. Perl can find a (key,value) pair by evaluating the
 hash of the key, then going right to where the value is stored. It is
 almost like finding the element of an array knowing the index value,
 rather than having to scan the entire array looking for a certain
 value.



so... key converts to hash in one step, then jumps directly to that 
position (if hash for that key is defined).  And if you're only looking 
up the existence of the key in the table, no additional resources are 
used to read the value from memory.  I can see how that's way more 
efficient than scanning all array indexes and reading each value.  
Especially for large lists.  But, there must be a slight trade-off... 
the processing required to initialize the hash table with it's keys and 
values is probably more intensive than defining an array with its 
respective values?  Unless, internally, Perl stores arrays as hashes, 
with the indexes as the keys.




 But, if we grep a list (as Jim suggests) instead of scan an
 array, shouldn't the list manipulation be more efficient (or at
 least equivalently efficient) to a hash lookup?

 No, grep has to scan the entire array, one element at a time. Using
 grep instead of a for loop might be more efficient, because of the
 internal mechanisms of Perl, but the time required is still
 proportional to the number of elements in the array. There is no way
 around that.

 Not much can be done with the list as a whole. You can take a
 reference to it and find out how many elements, but anything else
 productive will almost surely involve scanning the array and
 accessing the elements, one at a time.



Thanks much for the explanation...now I understand Uri's comments!  and 
I can appreciate that hashes are more powerful and flexible than I 
originally gave them credit for (I always thought of hashes as just 
fancy arrays with strings instead of numbers for indexes).


It also makes me wonder if the time spent learning linked lists in C 
programming class would have been better spent learning useful 
programming theory with Perl...




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




Re: Using regex special characters from a string

2011-03-09 Thread Brian F. Yulga

Ben Lavery wrote:

 there must be a slight trade-off... the processing required to
 initialize the hash table with it's keys and values is probably
 more intensive than defining an array with its respective values?
 Unless, internally, Perl stores arrays as hashes, with the indexes
 as the keys.

 I would have thought there would be more of a hit for initialising
 hashes over arrays, but the gains in efficiency later would outweigh
 that hit I'd have though?

Initially I was going to ask: Comparing hashes to arrays, at what scale 
is the extra time required for hash initialization offset by the 
decreased time in searching? 10 items? 100? 1000?   But, I realized 
that the problem isn't that simple, because it depends on the operations 
you perform on the hash after you initialize it.


I get the impression (from reading this mailing list and other sources) 
that in most cases, the performance hit of initialization is relatively 
minor, compared to the performance gains later on.  In other words, if 
there is a relatively direct (or natural) way to put your data into a 
hash instead of an array, it's worth it.  And for some situations, if 
the values are unique, to define a mirrored hash (value = key) so that 
it's faster to search for values as key lookups instead.



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




Re: Using regex special characters from a string

2011-03-09 Thread Brian F. Yulga

Thanks for the reading suggestions!

Brian Fraser wrote:
On Wed, Mar 9, 2011 at 2:22 PM, Brian F. Yulga 
byu...@langly.dyndns.org mailto:byu...@langly.dyndns.org wrote:


Uri and Jim have hit upon one of my major stumbling blocks with
learning Perl.  There seems to be a difference of opinion on the
proper times to use hashes vs. arrays/lists...and how best to use
them.


http://perldoc.perl.org/perlfaq4.html#How-can-I-remove-duplicate-elements-from-a-list-or-array%3f 

Use a hash. When you think the words unique or duplicated, think 
hash keys.
Very informative!  Also in the following section, How can I tell 
whether a certain element is contained in a list or array? Hearing the 
word in is an indication that you probably should have used a hash, 
not a list or array, to store your data.
Also the Hashes chapter of Learning Perl has a section called 
something like Why use Hashes, which seems fitting enough.
counting words is the fourth example :-)  That section mentions that 
hashes are useful for related data, or database-like data, but wasn't 
entirely convincing for me because it didn't mention performance.
Same with the Hashes part of Modern Perl ( 
http://www.onyxneon.com/books/modern_perl/index.html ), in the The 
Perl Language chapter.


For those that have heard of this book, I'm currently digesting
this piece of information: 


Effective Perl Programming, 2nd Ed., Item 9: Know the difference
between lists and arrays.


See Item 13: Use arrays or hashes to group data. Also, 
www.effectiveperlprogramming.com 
http://www.effectiveperlprogramming.com.
A useful read, but similar to Learning Perl, it doesn't mention the 
performance aspect.  In this situation, I think the perlfaq provides the 
most convincing argument.


I don't know if others had this problem when learning Perl, but the 
TMTOWTDI philosophy sometimes makes it difficult for me to discover 
better ways of doing things after I find one clunky way that works 
(hence my recent purchase of Effective Perl Programming, with the 
intention of acquiring Perl Best Practices in the future).


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




Re: Auto correction of typos

2011-03-07 Thread Brian F. Yulga


shawn wilson wrote:

 On Mar 7, 2011 11:37 AM, Brandon McCaig bamcc...@gmail.com
 wrote:

 On Mon, Mar 7, 2011 at 5:22 AM, Ramprasad Prasad
 ramprasad...@gmail.com
 wrote:
 1) Create a hash of aliases for frequently used domains and their
 typos For eg gmaill.com = gmail.com hotmal.com =  hotmail.com
 etc

 when I get the email id  with these typos , I will prompt the
 user for correction  , If accepted then thats fine

 It's difficult to predict every possible typo that a user is going
 to make. They could have their hands off-by-one and type something
 completely different. Perhaps you could instead just store a list
 of common mail domains and warn the user to double-check if their
 E-mail address doesn't match any of them.


 Yeah. However if he's going to go that far, he might as well just
 check for an mx record for the domain.

 Also, that hash is going to get unwieldy pretty quick. If there's no
 db already, might want to use sqlite.

 Heh, too bad he didn't like my capcha of the email address for
 confirmation. I kinda think that was one of my better ideas. :)

I agree -- I think you would spend considerable time attempting to 
predict typos, when a simpler validation model could be written and 
implemented relatively quickly.  For instance, I would tackle this 
problem with some straightforward client-side JavaScript.  Before 
passing the form results back to the server for processing (and further 
validation if desired), you could use the prompt() or confirm() methods 
of the Window object.  With prompt(), you could require the user to 
enter the address a second time.  With confirm(), you could force them 
to inspect the address before continuing on with submission.  I'm not 
sure if web bots could work through this logic and still spam you...


I'm not a big fan of my own suggestion, since encountering dialog boxes 
while browsing can be annoying to the user.  But, I think most types of 
entry validation annoy users, even if it's for their own good.  IMO, the 
Capcha idea is probably the best bang for the buck; requires only 
moderate coding (some are just copy-paste code snippets) and gives you a 
high level of human authentication.  The downside is that the capcha 
phrases are sometimes ambiguous, and the user might have to try more 
than once to submit.


A quick Google search revealed an article you may find useful, if you'd 
still like to tackle the problem with your original idea:

http://www.infoq.com/articles/lucene-did-you-mean
Granted, it's not a Perl solution, but it is the same programming logic 
you would need.  Notice that it requires a suitable dictionary of 
reference words (e-mail domains for your case) so you would still have a 
lot of work to do...


Good Luck!
Brian


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




Re: multidimensional array check

2011-03-04 Thread Brian F. Yulga


Rob Coops wrote:



 So you should now be able to retrieve the whole result set as a
 reference to an array containing references to arrays. And I hope
 that my ramblings explained a bit how you can use that reference to
 get to the underlying values. If you combine all of that I think
 you'll be able to work out the way to get your program to work the
 way you want it to.



FWIW, I found your ramblings quite helpful, since I am working to get 
more comfortable with variables, arrays, and references, on my way to 
learning objects.


Coming from a basic understanding of C, the reference variable concept 
sounds a lot like pointers in C.  But, my Learning Perl and 
Intermediate Perl books do not use that terminology.  Am I on a 
correct path to understanding by making this analogy?


Thanks,
Brian


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




Re: multidimensional array check

2011-03-04 Thread Brian F. Yulga

Thanks for the link.  That's exactly the explanation I was looking for...

I progressed quickly through the first Oreilly book (Learning Perl) but 
slowed down considerably in the 2nd (Intermediate Perl).  I found the 
chapter on Subroutine References (7) more difficult than earlier 
material (my brain nearly exploded when reading the section returning a 
subroutine from a subroutine) so I decided to stop and go back to 
basics until I achieve a better understanding.  It didn't occur to me to 
open up the Advanced Perl Programming book (what I intended to read as 
my 3rd book)!


Rob Coops wrote:

 You are very right a really nice explanation of this you can find in
 the Advanced Perl Programming book by Oreilly see the link below

 http://oreilly.com/catalog/advperl/excerpt/ch01.html

 If you already went trough the other two then this is definitely a
 good one to read as it goes that extra step and provides you with a
 good insight of the underlying logic.

 On Fri, Mar 4, 2011 at 7:30 PM, Brian F. Yulga
 byu...@langly.dyndns.org mailto:byu...@langly.dyndns.org wrote:


 Rob Coops wrote:


 So you should now be able to retrieve the whole result set as a
 reference to an array containing references to arrays. And I hope
 that my ramblings explained a bit how you can use that reference to
 get to the underlying values. If you combine all of that I think
 you'll be able to work out the way to get your program to work the
 way you want it to.


 FWIW, I found your ramblings quite helpful, since I am working to get
 more comfortable with variables, arrays, and references, on my way to
 learning objects.

 Coming from a basic understanding of C, the reference variable
 concept sounds a lot like pointers in C.  But, my Learning Perl and
 Intermediate Perl books do not use that terminology.  Am I on a
 correct path to understanding by making this analogy?

 Thanks, Brian





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




Re: multidimensional array check

2011-03-04 Thread Brian F. Yulga


Shawn H Corey wrote:

 On 11-03-04 02:30 PM, Brian F. Yulga wrote:
 my brain nearly exploded when reading the section returning a
 subroutine from a subroutine

 Just wait until you get to the part where you can treat an anonymous
 subroutine as an object.  :D

 For details, see `perldoc -f bless`.


bless REF
   This function tells the thingy referenced by REF that it 
is now

   an object in the CLASSNAME package.  ...

'thingy' is a great technical term. ;-)
When I learned rudimentary C/C++ in school, I was taught pointers and 
structs/classes initially as separate concepts, and later the two were 
connected.  It's becoming apparent to me that learning Perl objects 
_requires_ a solid understanding of references first...





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




Re: Testing File Contents

2011-03-02 Thread Brian F. Yulga

On Wed, 2 Mar 2011, Matt wrote:

  The easiest way in my opinion is to use the 'grep' function like this:
 
  my $searchstring=whatever;
  open CFG, '', $_file || die(could not open file: $_file!);
  my @data=CFG;
  close CFG;
  if ( grep /$searchstring/i, @data ) {
   print $searchstring found\n;
  }
 
 
 This sorta worked.  Needed a minor change.
 
  unless ( grep /$searchstring/i, @data ) {
   print $searchstring not found\n;
 
 Thanks.
 

My apologies if I'm beating a dead horse here, but I'm new to Perl and 
thought of a slightly different approach:


my $searchrx = qr/whatever/;  # or q/whatever/ if you don't need regexp
@ARGV or die qq/you didn't specify a filename\n/;
open FH, q//, shift @ARGV or die qq/file open error: $!/;
$_ = join q//, FH;
close FH;
if ( ! m/$searchrx/s ) {
print qq/pattern not found\n/;
# do something
}


I'm not sure which is more Perlish, but I hear TMTOWTDI is supposed to 
be valued, too.  I welcome any criticism.

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


Re: Testing File Contents

2011-03-02 Thread Brian F. Yulga



Ken Slater wrote:

 From: Brian F. Yulga [mailto:byu...@langly.dyndns.org]

 On Wed, 2 Mar 2011, Matt wrote:

 The easiest way in my opinion is to use the 'grep' function like
 this:

 my $searchstring=whatever; open CFG, '', $_file || die(could
 not open file: $_file!); my @data=CFG; close CFG; if ( grep
 /$searchstring/i, @data ) { print $searchstring found\n; }


 This sorta worked.  Needed a minor change.

 unless ( grep /$searchstring/i, @data ) {  print $searchstring
 not found\n;

 Thanks.


 My apologies if I'm beating a dead horse here, but I'm new to Perl
 and thought of a slightly different approach:


 my $searchrx = qr/whatever/;  # or q/whatever/ if you don't need
 regexp

 This is probably personal preference, but I prefer to provide a more
 meaningful name rather than using @ARGV. Plus you have lost the
 filename once you 'shifted' @ARGV in the open statement. May want to
 use the file name in the open error statement or later.


I agree with you, saving the filename to a variable is probably a better 
practice, in general.  In this context it wasn't specified that it was 
needed later, so I opted to just 'shift' it to use once.



 Also, I would not use the '/' as your quote delimiter - that's too
 recognizable as being used as the pattern match delimiter. Use actual
 double quotes () unless there are double quotes in the string - also
 less typing. Instead of '/' may want to use '{' and '}' if using qq
 (see perldoc perlop, Quote and Quote-like Operators).


Good point.  I started using generic quotes almost exclusively when I 
discovered that they avoided some of the possible shell-interpolation 
issues that arise when executing perl one-liners.  I've probably taken 
it too far.  I can see that '/' is not a great idea.  Besides '{' and 
'}' as quote delimiters, do you think it's generally tolerated to use 
'(', ')', '[', ']'  ?




 my $fileName = shift @ARGV or die You did not specify a
 filename\n;
 @ARGV or die qq/you didn't specify a filename\n/;

 Use lexical variable for filehandle. Makes things easier - such as
 passing to a function.

 open my $FH,'', $fileName or die Error opening $fileName: $!\n;
 open FH, q//, shift @ARGV or die qq/file open error: $!/;


Okay, I'll work on breaking that habit.  My first lessons in Perl used 
the open FH convention, and I just got used to it.



 The join is unnecessary, set the input_record_separator ($/) to undef
 or use a local copy of $/ (see perldoc perlvar). If this is
 undefined, the entire file will be read into a variable.



Oh, I totally forgot about the input_record_separator ( $/ )...
That's WAY better (actually when I wrote the 'join', I was thinking that 
I shouldn't need to do it that way!)


Thanks much for the suggestions,

Brian


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




Re: Testing File Contents

2011-03-02 Thread Brian F. Yulga

Uri Guttman wrote:

 BFY == Brian F Yulga byu...@langly.dyndns.org writes:

 BFY My apologies if I'm beating a dead horse here, but I'm new to
 Perl and BFY thought of a slightly different approach:


 BFY my $searchrx = qr/whatever/;  # or q/whatever/ if you don't need
 regexp BFY @ARGV or die qq/you didn't specify a filename\n/; BFY
 open FH, q//, shift @ARGV or die qq/file open error: $!/; BFY $_ =
 join q//, FH;

 that is very slow and clunky. perl could slurp the file for you if
 you set $/ to undef. or better yet, use File::Slurp to do it


Yes, definitely better to set $/ to undef.

I'm still trying to get a handle on the libraries at my disposal -- 
There's so much in CPAN it's hard to know where to start.  Is it 
equivalently efficient to use IO::All or IO::Simple, or is File::Slurp 
truly the best for this purpose?


I ask because I played with IO:All to read files, enjoyed the simple, 
intuitive syntax   my $filecontents  io('filename'); , but (perhaps 
naively) assumed the native open my $fh to be faster since it doesn't 
require a use to load a library.




 BFY close FH; BFY if ( ! m/$searchrx/s ) {

 why are you using m//? the m isn't needed if you use // for
 delimiters


Yeah, I know, I don't need it... being verbose (sometimes) keeps a 
newbie like me from making silly mistakes :-)


My initial experimentation with Perl has been mostly in a line-by-line 
mind-set because I have a tendency to write scripts that are used like:

some-unix-command | perl -wne 'do some processing'  myresults.txt

I'm trying to expand my horizons, thanks for help,

Brian


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