Hi

2009-07-10 Thread John Somoza
I have a general perl question.

I'm on OSX running a program from the command line. That program asks a
series of questions, which I interactively answer.

I would like to use perl to run the program (this part is not the problem)
and answer the questions (this is the part I need help with). I have seen
this type of thing done with a shell script (I think you can use the ECHO
command in the bourne shell) but how would I do this in perl?

Thanks,

John


Re: Hi

2009-07-10 Thread Jenn G.
Hi,

You may want Expect:
http://search.cpan.org/~rgiersig/Expect-1.21/Expect.pod


On Fri, Jul 10, 2009 at 3:00 PM, John Somoza wrote:
> I have a general perl question.
>
> I'm on OSX running a program from the command line. That program asks a
> series of questions, which I interactively answer.
>
> I would like to use perl to run the program (this part is not the problem)
> and answer the questions (this is the part I need help with). I have seen
> this type of thing done with a shell script (I think you can use the ECHO
> command in the bourne shell) but how would I do this in perl?
>
> Thanks,
>
> John
>

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




Understanding recursion

2009-07-10 Thread Dermot
Hi,

I was reading Higher Order Perl[1] last night and, dishearteningly,
got stuck on chapter one.

Mark Dominus offers the following as a  binary string conversion
example of how recursion can work.


use strict;
use warnings;

my $bstr = binary(37);
print "$bstr\n";                 # prints 100101

sub binary {
 my ($n) = @_;
 return $n if $n == 0 || $n == 1;
 my $k = int($n/2);
 my $b = $n % 2;
 my $E = binary($k);
 return $E . $b;
}


The algorithm works perfectly but my understanding of it's workings is amiss.

When I look at this I see $E initialised and then concatenate with the
the modulus of
37 % 2 =1
18 % 2 = 0
9 % 2 = 1
4 % 2 = 0
2 % 2 = 0

That by my reckoning is 10100. Almost the reverse of the answer but I
am obviously wrong and I can't see how the final expressions:

my $E = binary($k);
return $E . $b;

work to give the answer.

Can someone more enlightened than me give me some guidence.
Thanx,
Dp.


PS: Has anybody heard from Rob Dixon, he hasn't been on the list for ages.

1.  http://hop.perl.plover.com/book

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




Re: Understanding recursion

2009-07-10 Thread Shawn H. Corey
On Fri, 2009-07-10 at 09:26 +0100, Dermot wrote:
> The algorithm works perfectly but my understanding of it's workings is amiss.
> 
> When I look at this I see $E initialised and then concatenate with the
> the modulus of
> 37 % 2 =1
> 18 % 2 = 0
> 9 % 2 = 1
> 4 % 2 = 0
> 2 % 2 = 0
> 
> That by my reckoning is 10100. Almost the reverse of the answer but I
> am obviously wrong and I can't see how the final expressions:
> 
> my $E = binary($k);
> return $E . $b;
> 
> work to give the answer.
> 
> Can someone more enlightened than me give me some guidence.

Perhaps it would be easier to understand if we look at the counterpart
to this.

#!/usr/bin/perl

my $dstr = decimal( 123456789 );
print "$dstr\n";

sub decimal {
 my ($n) = @_;
 return $n if $n < 10;
 my $k = int($n/10);
 my $b = $n % 10;
 my $E = decimal($k);
 return $E . $b;
}
__END__

When dealing with the number 123456789, the first time through, stopping
just before the sub is called a second time, we have:

$b = 123456789 % 10 = 9;
$k = int( 123456789 / 10 ) = 12345678;

$E will be assigned the string that represents $k.  Clearly, this must
be concatenated before $b in the returned string.  The binary version
works the same way.


-- 
Just my 0.0002 million dollars worth,
  Shawn

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



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




Re: Need some module help

2009-07-10 Thread Shawn H. Corey
On Thu, 2009-07-09 at 21:58 -0700, Travis Thornhill wrote:
> [File: ./uses_Mod.pl]
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> use lib 'Mod';

Not needed.  When perl searches for a module, it does so by looking in
the directories in @INC (See `perldoc perlvar`).  The very last item in
@INC is '.', the current directory.

However, you may want to add this to all your scripts:

use FindBin qw( $Bin );
use lib $Bin;

The module, FindBin, is a standard module and comes bundled with perl.
What it does is finds the directory where the script is and stores it in
$Bin.  If your modules are in the same directory, adding $Bin to lib is
necessary for your script to work when you run it outside of its home
directory.

> 
> use Mod::myMod;

This means attach the string 'Mod/myMod.pm' to each of the directories
in @INC and see if it's there.  perl considers 'Mod' to be a
sub-directory and 'myMod.pm' the file.

BTW, by convention, pragmatics should start with lowercase letters and
modules with uppercase.  You should name your modules with names
starting in uppercase: Mod/MyMod.pm

> 
> 
> my $obj = myMod->new(); # <- THIS IS THE LINE THAT ERRORS
> 
> $obj->myModFunc();
> [end uses_Mod.pl]
> 
> 
> [File: ./Mod/myMod.pm]
> package Mod::myMod;
> 
> use strict;
> use warnings;
> 
> require Exporter;
> 
> our @ISA = qw(Exporter);
> our @EXPORT = qw(new myModFunc);

Exporter is not required.  Exporter is used with modules, that is, a
file containing a collection of subroutines.  It is not needed for
objects (unless you are creating a bastardized version that does both,
like CGI).

> 
> sub new
> {
> my $class = shift;
> my $self = {};
> 
> # Just so the constructor does something I can see and verify.
> $self->{'time_created'} = time();
> print STDOUT "Object created at " . $self->{'time_created'} .
> "\n";
> 
> bless ( $self, $class );
> return $self;
> }
> 
> sub myModFunc
> {
> my $self = shift;
> print "myModFunc called\n";
> }
> 
> 1;
> [end myMod.pm]
-- 
Just my 0.0002 million dollars worth,
  Shawn

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



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




Re: Understanding recursion

2009-07-10 Thread Dermot
2009/7/10 Shawn H. Corey :
> On Fri, 2009-07-10 at 09:26 +0100, Dermot wrote:
>> The algorithm works perfectly but my understanding of it's workings is amiss.
>>
>> When I look at this I see $E initialised and then concatenate with the
>> the modulus of
>> 37 % 2 =1
>> 18 % 2 = 0
>> 9 % 2 = 1
>> 4 % 2 = 0
>> 2 % 2 = 0
>>
>> That by my reckoning is 10100. Almost the reverse of the answer but I
>> am obviously wrong and I can't see how the final expressions:
>>
>> my $E = binary($k);
>> return $E . $b;
>>
>> work to give the answer.
>>
>> Can someone more enlightened than me give me some guidence.
>
> Perhaps it would be easier to understand if we look at the counterpart
> to this.

I appreciate what your saying but I can't say I find the counterpart
to be more helpful  so I am going to stick to the binary if it's all
the same.

> #!/usr/bin/perl
>
> my $dstr = decimal( 123456789 );
> print "$dstr\n";
>
> sub decimal {
>  my ($n) = @_;
>  return $n if $n < 10;
>  my $k = int($n/10);
>  my $b = $n % 10;
>  my $E = decimal($k);
>  return $E . $b;
> }
> __END__
>
> When dealing with the number 123456789, the first time through, stopping
> just before the sub is called a second time, we have:
>
> $b = 123456789 % 10 = 9;
> $k = int( 123456789 / 10 ) = 12345678;
>
> $E will be assigned the string that represents $k.

Why is $E getting assigned the value from $k? $E is initialised and
then assigned the return value of the binary(18) during the first
invocation (0). Does the subroutine continue and concatenate $b to $E
and then return (1) ? or does it wait until binary exhausts $n?

In my own groping/nonscientific sort of way, what I see emerging is a
pattern where my result (10100) is nearly the reverse of the correct
answer (100101) minus the leading 1. If that is correct I don't know
why the string is reversed,

Clearly, this must
> be concatenated before $b in the returned string.  The binary version
> works the same way.

Lost you a bit there because I can't see $k being concatenated to $E.
Dp.

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




Re: Understanding recursion

2009-07-10 Thread Shawn H. Corey
On Fri, 2009-07-10 at 13:42 +0100, Dermot wrote:
> Why is $E getting assigned the value from $k? 

my $E = binary($k);

I said $E will be assigned the string that represents $k, not it's
value.

> $E is initialised and
> then assigned the return value of the binary(18) during the first
> invocation (0). Does the subroutine continue and concatenate $b to $E
> and then return (1) ? or does it wait until binary exhausts $n?

It "waits" until: $n == 0 || $n == 1;

> 
> In my own groping/nonscientific sort of way, what I see emerging is a
> pattern where my result (10100) is nearly the reverse of the correct
> answer (100101) minus the leading 1. If that is correct I don't know
> why the string is reversed,

Here's a breakdown of what's happening in binary(37).  Note that I'm
mixing arithmetic and string manipulations here.

37
= int(37/2)*2 + 37%2
= (18)*2 + 1
= ( int(18/2)*2 + 18%2 )*2 + 1
= ( (9)*2 + 0 ) + 1
= ( ( int(9/2)*2 + 9%2 )*2 + 0 )*2 + 1
= ( ( (4)*2 + 1 )*2 + 0 )*2 + 1
= ( ( ( int(4/2)*2 + 4%2 )*2 + 1 )*2 + 0 )*2 + 1
= ( ( ( (2)*2 + 0 )*2 + 1 )*2 + 0 )*2 + 1
= ( ( ( ( int(2/2)*2 + 2%2 )*2 + 0 )*2 + 1 )*2 + 0 )*2 + 1
= ( ( ( ( (1)*2 + 0 )*2 + 0 )*2 + 1 )*2 + 0 )*2 + 1
= ( ( ( ( "1" . "0" ) . "0" ) . "1" ) . "0" ) . "1"
= ( ( ( ( "10" ) . "0" ) . "1" ) . "0" ) . "1"
= ( ( ( "100" ) . "1" ) . "0" ) . "1"
= ( ( "1001" ) . "0" ) . "1"
= ( "10010" ) . "1"
= "100101"

In the first part:
$n = int($n/2)* + $n%2
but $k = int($n/2)
and $b = $n%2
so $n = int($n/2)*2 + $n%2 = $k*2 + $b

Addition is commutative, so this could be written as: $n = $b + $k*2
but string concatenation is not.  The return string must be $E, the
string representing $k; multiplied by 2 by concatenating "0" at it's
end; and then that "0" being replaced by "$b".

In decimal, to multiply a number by 10, add an zero to it's end.  (It's
true.  Ask anyone on the streets.  When asked, "How do you multiply a
number by ten?" they'll reply, "Stick a zero at its end.")

In binary, to multiply a number by two, stick an zero on its end.  If $e
is the string representing $k, to reconstruct $n, we have to first
multiply $E by 2:

my $return_string = $E . "0";

Now we have to add $b to this by replacing the last character in the
return string with "$b":

$return_string = substr( $return_string, 0, -1 ) . $b;

It's just simpler to do it all in one step:

my $return_string = $E . $b;


-- 
Just my 0.0002 million dollars worth,
  Shawn

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



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




Re: Understanding recursion

2009-07-10 Thread Telemachus
On Fri Jul 10 2009 @  9:26, Dermot wrote:
> The algorithm works perfectly but my understanding of it's workings is amiss.
> 
> When I look at this I see $E initialised and then concatenate with the
> the modulus of
> 37 % 2 =1
> 18 % 2 = 0
> 9 % 2 = 1
> 4 % 2 = 0
> 2 % 2 = 0
> 
> That by my reckoning is 10100. Almost the reverse of the answer but I
> am obviously wrong and I can't see how the final expressions:
> 
> my $E = binary($k);
> return $E . $b;
> 
> work to give the answer.

It helps for me to walk through it visually, indenting once each time the
script needs to call the binary sub-routine. Notice that it keeps going down
and inward, until it "bottoms out" on the base case. At that point, the answers
ripple back up to fit into the calls to binary($E) that were left hanging 
(because in those cases $E wasn't 1 or 0). Maybe this will help you as well.


Does 37 == 0 || 1? No; continue...
$k = 18; $b = 1; $E = binary(18) -> go do that...
Does 18 == 0 || 1? No; continue...
$k = 9; $b = 0; $E = binary(9) -> go do that...
Does 9 == 0 || 1? No; continue...
$k = 4; $b = 1; $E = binary(4) -> go do that...
Does 4 == 0 || 1? No; continue...
$k = 2; $b = 0; $E = binary(2) -> go do that...
Does 2 == 0 || 1? No; continue...
$k = 1; $b = 0; $E = binary(1) -> go do that...
Does 1 == 0 || 1? Yes; return 1
$E = 1; return 1 . 0
$E = 10; return 10 . 0
$E = 100; return 100 . 1
$E = 1001; return 1001 . 0
$E = 10010; return 10010 . 1

Hope this helps, T

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




Re: Help Me

2009-07-10 Thread Steve Bertrand
Umar Draz wrote:
> Hello Steve
> 
> Thanks for your help.
> 
> Would you please help me one thing more
> 
> I have string e.g
> 
> $str = "Hello this is my string (1020p0404), this string is not complete
> (1 034 400 3). now the string complte";
> 
> I want to remove spaces form within ( ) not whole string. Please help me
> how I can do that.

Undoubtedly, there are far better ways to do this, but this is what I
came up with quickly:

#!/usr/bin/perl

use warnings;
use strict;

my $str = "" .
"Hello this is my string (1020p0404), this string " .
"is not complete (1 034 400 3). now the string complte";

while ($str =~ /\((.*?)\)/g) {

next unless $1;
my $num = $1;
$num =~ s/\s+//g;

print "$num\n";
}

print "$str\n";

__END__

Output:

1020p0404
10344003
Hello this is my string (1020p0404), this string is not complete (1 034
400 3). now the string complte

Steve


smime.p7s
Description: S/MIME Cryptographic Signature


Re: Understanding recursion

2009-07-10 Thread Dermot
2009/7/10 Telemachus :
> On Fri Jul 10 2009 @  9:26, Dermot wrote:
>> The algorithm works perfectly but my understanding of it's workings is amiss.
>>
>> When I look at this I see $E initialised and then concatenate with the
>> the modulus of
>
> It helps for me to walk through it visually, indenting once each time the
> script needs to call the binary sub-routine. Notice that it keeps going down
> and inward, until it "bottoms out" on the base case. At that point, the 
> answers
> ripple back up to fit into the calls to binary($E) that were left hanging
> (because in those cases $E wasn't 1 or 0). Maybe this will help you as well.
>
>
> Does 37 == 0 || 1? No; continue...
> $k = 18; $b = 1; $E = binary(18) -> go do that...
>    Does 18 == 0 || 1? No; continue...
>    $k = 9; $b = 0; $E = binary(9) -> go do that...
>        Does 9 == 0 || 1? No; continue...
>        $k = 4; $b = 1; $E = binary(4) -> go do that...
>            Does 4 == 0 || 1? No; continue...
>            $k = 2; $b = 0; $E = binary(2) -> go do that...
>                Does 2 == 0 || 1? No; continue...
>                $k = 1; $b = 0; $E = binary(1) -> go do that...
>                    Does 1 == 0 || 1? Yes; return 1
>                $E = 1; return 1 . 0
>            $E = 10; return 10 . 0
>        $E = 100; return 100 . 1
>    $E = 1001; return 1001 . 0
> $E = 10010; return 10010 . 1
>

Yes this does help. It makes a lot more sense when I get to view the
whole execution visually like this. So the final return statement (as
Shawn pointed out) isn't used until $n is either 0 or 1. In effect the
whole thing loops from:

my ($n) = @_;
return  return $n if $n == 0 || $n == 1;
 my $k = int($n/2);
 my $b = $n % 2;
 my $E = binary($k);

Then un-winds itself, returning in reverse order.

I want to thank you both for the time and energy you've put in.
There's been a lot of typing there and it's much appreciated.
Dp.

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




Re: Understanding recursion

2009-07-10 Thread Ken Slater

Dermot wrote:
>Hi,
>
>I was reading Higher Order Perl[1] last night and, dishearteningly,
>got stuck on chapter one.
>
>Mark Dominus offers the following as a  binary string conversion
>example of how recursion can work.
>
>
>use strict;
>use warnings;
>
>my $bstr = binary(37);
>print "$bstr\n"; # prints 100101
>
>sub binary {
>  my ($n) = @_;
>  return $n if $n == 0 || $n == 1;
>  my $k = int($n/2);
>  my $b = $n % 2;
>  my $E = binary($k);
>  return $E . $b;
>}
>
>
>The algorithm works perfectly but my understanding of it's workings is amiss.
>
>When I look at this I see $E initialised and then concatenate with the
>the modulus of
>37 % 2 =1
>18 % 2 = 0
>9 % 2 = 1
>4 % 2 = 0
>2 % 2 = 0
>
>That by my reckoning is 10100. Almost the reverse of the answer but I
>am obviously wrong and I can't see how the final expressions:
>
>my $E = binary($k);
>return $E . $b;
>
>work to give the answer.
>
>Can someone more enlightened than me give me some guidence.
>Thanx,
>Dp.

Maybe this will make it easier to understand.

On each call 'binary' $b is set as follows (but does not start returning values 
until the 6th call):

1. $b = 1  (37 % 2)
2. $b = 0  (18 % 2)
3. $b = 1 (9 % 2)
4. $b = 0 (4 % 0)
5. $b = 0 (2 % 0)
6. returns 1 immediately

Now the stack starts unwinding and appending the $b value to the return value:

5. returns '10' (1 from 6th call and 0 from b value on 5th call).
4. returns '100'
3. returns '1001'
2. returns '10010'
1. returns '100101'

Ken


 
_
Windows Live™: Keep your life in sync. 
http://windowslive.com/explore?ocid=TXT_TAGLM_WL_BR_life_in_synch_062009

Best way to mix two lists

2009-07-10 Thread Matteo Riva
Hello everybody, I have two lists and I want to merge them like this:

element 1 of list A, element 1 of list B, A2, B2, A3, B3, etc.

I need this to create a sequence of key/value for an anonymous hash.

I'm using this code:

# @fields contains the names of the hash keys
# $val is a string containing a colon-separated sequence of vales to
# be associated with the keys

my $count = 0;
$data{$key} = { map { $fields[$count++] => $_ } split (/:/, $val) };

which uses a counter variable.  I was wondering it there's a more
elegant (AKA perlish) way to mix two lists in this way.

Thanks!

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




Re: Best way to mix two lists

2009-07-10 Thread Shawn H. Corey
On Fri, 2009-07-10 at 20:32 +0200, Matteo Riva wrote:
> Hello everybody, I have two lists and I want to merge them like this:
> 
> element 1 of list A, element 1 of list B, A2, B2, A3, B3, etc.
> 
> I need this to create a sequence of key/value for an anonymous hash.
> 
> I'm using this code:
> 
> # @fields contains the names of the hash keys
> # $val is a string containing a colon-separated sequence of vales to
> # be associated with the keys
> 
> my $count = 0;
> $data{$key} = { map { $fields[$count++] => $_ } split (/:/, $val) };
> 
> which uses a counter variable.  I was wondering it there's a more
> elegant (AKA perlish) way to mix two lists in this way.
> 
> Thanks!
> 

Download List::MoreUtils from CPAN
http://search.cpan.org/~vparseval/List-MoreUtils-0.22/lib/List/MoreUtils.pm

and use mesh().


-- 
Just my 0.0002 million dollars worth,
  Shawn

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



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




Re: Best way to mix two lists

2009-07-10 Thread Gunnar Hjalmarsson

Matteo Riva wrote:

Hello everybody, I have two lists and I want to merge them like this:

element 1 of list A, element 1 of list B, A2, B2, A3, B3, etc.

I need this to create a sequence of key/value for an anonymous hash.

I'm using this code:

# @fields contains the names of the hash keys
# $val is a string containing a colon-separated sequence of vales to
# be associated with the keys

my $count = 0;
$data{$key} = { map { $fields[$count++] => $_ } split (/:/, $val) };

which uses a counter variable.  I was wondering it there's a more
elegant (AKA perlish) way to mix two lists in this way.


You can use a hash slice.

@{ $data{$key} }{ @fields } = split /:/, $val;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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




Re: Best way to mix two lists

2009-07-10 Thread Jim Gibson
On 7/10/09 Fri  Jul 10, 2009  11:32 AM, "Matteo Riva" 
scribbled:

> Hello everybody, I have two lists and I want to merge them like this:
> 
> element 1 of list A, element 1 of list B, A2, B2, A3, B3, etc.
> 
> I need this to create a sequence of key/value for an anonymous hash.
> 
> I'm using this code:
> 
> # @fields contains the names of the hash keys
> # $val is a string containing a colon-separated sequence of vales to
> # be associated with the keys
> 
> my $count = 0;
> $data{$key} = { map { $fields[$count++] => $_ } split (/:/, $val) };
> 
> which uses a counter variable.  I was wondering it there's a more
> elegant (AKA perlish) way to mix two lists in this way.

Use a hash slice:

my %data;
@da...@fields} = split(/:/,$val);




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




Index a list

2009-07-10 Thread Steve
Hi list memebers. I'm new here and I'm interested in learning about
Perl. I've managed to get some programs together but have a lot to
learn.

May I put a question to the experts?

Suppose I have a text file that has a whopping amount of lines
(20-50,000). What would be the best way to condense or index this with
Perl so I could query it and get a yes/no on it as fast as possible with
minimum overhead? I could maintain a database to do it, but for one line
entries this would probably be overkill. It's easy to swap a text file
to update it and manage the list concerned.

What would be the best approach?

Warm regards
Steve


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




Re: Help Me

2009-07-10 Thread Christer Ekholm
Steve Bertrand  writes:

> Umar Draz wrote:
>> Hello Steve
>> 
>> Thanks for your help.
>> 
>> Would you please help me one thing more
>> 
>> I have string e.g
>> 
>> $str = "Hello this is my string (1020p0404), this string is not complete
>> (1 034 400 3). now the string complte";
>> 
>> I want to remove spaces form within ( ) not whole string. Please help me
>> how I can do that.
>
> Undoubtedly, there are far better ways to do this, but this is what I
> came up with quickly:
>
>

I found a way using /e and join+split, probably not better. But more fun :)

$str =~ s/\(([^)]+?)\)/(join('',split(' ',$1)))/ge;


--
 Christer


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




Re: Index a list

2009-07-10 Thread Shawn H. Corey
On Fri, 2009-07-10 at 20:37 +0100, Steve wrote:
> Hi list memebers. I'm new here and I'm interested in learning about
> Perl. I've managed to get some programs together but have a lot to
> learn.
> 
> May I put a question to the experts?
> 
> Suppose I have a text file that has a whopping amount of lines
> (20-50,000). What would be the best way to condense or index this with
> Perl so I could query it and get a yes/no on it as fast as possible with
> minimum overhead? I could maintain a database to do it, but for one line
> entries this would probably be overkill. It's easy to swap a text file
> to update it and manage the list concerned.
> 
> What would be the best approach?
> 
> Warm regards
> Steve
> 
> 

Put it in a database that has search-index building capacity.  Don't
re-invent the wheel.


-- 
Just my 0.0002 million dollars worth,
  Shawn

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



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




Re: Help Me

2009-07-10 Thread Steve Bertrand
Christer Ekholm wrote:
> Steve Bertrand  writes:
> 
>> Umar Draz wrote:
>>> Hello Steve
>>>
>>> Thanks for your help.
>>>
>>> Would you please help me one thing more
>>>
>>> I have string e.g
>>>
>>> $str = "Hello this is my string (1020p0404), this string is not complete
>>> (1 034 400 3). now the string complte";
>>>
>>> I want to remove spaces form within ( ) not whole string. Please help me
>>> how I can do that.
>> Undoubtedly, there are far better ways to do this, but this is what I
>> came up with quickly:
>>
>>
> 
> I found a way using /e and join+split, probably not better. But more fun :)
> 
> $str =~ s/\(([^)]+?)\)/(join('',split(' ',$1)))/ge;

Ohhh spiffy! That's a lot more compact than what I came up with!

my @parts = $str =~ /^(.*?)(\(.*?\))(.*?)(\(.*?\))(.*)$/g;
$parts[3] =~ s/\s+//g;
$str = join('', @parts);

Steve



smime.p7s
Description: S/MIME Cryptographic Signature


Re: Index a list

2009-07-10 Thread Jim Gibson
On 7/10/09 Fri  Jul 10, 2009  12:37 PM, "Steve"
 scribbled:

> Hi list memebers. I'm new here and I'm interested in learning about
> Perl. I've managed to get some programs together but have a lot to
> learn.
> 
> May I put a question to the experts?
> 
> Suppose I have a text file that has a whopping amount of lines
> (20-50,000). What would be the best way to condense or index this with
> Perl so I could query it and get a yes/no on it as fast as possible with
> minimum overhead? I could maintain a database to do it, but for one line
> entries this would probably be overkill. It's easy to swap a text file
> to update it and manage the list concerned.
> 
> What would be the best approach?

That depends upon the query that you want to do. If it is just to see if a
line exists in the file, then a hash would be best:

my %hash;
while(<>) {
chomp;
$hash{$_} = 1;
}

...

if( exists $hash{$someline} ) {
print "Line <$someline> exists\n";
}else{
print "Line <$someline> not found\n";
}

If it is something more complex, then reading in the file to an array might
work efficiently.

We need a little more information to help you further.



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




html template loops not showing data

2009-07-10 Thread Scott

Hello,
I almost have this but cannot figure out why its not showing the data, 
right now it knows how many entries im putting in because it spits out 
that number of rows.


I am getting my data from dbd::mysql, it works because i have tested in 
in a normal test.pl and it prints out the data but not using an array or 
hash.
I tried to follow this example: 
http://search.cpan.org/~samtregar/HTML-Template-2.9/Template.pm#TMPL_LOOP

I think its the second loop example on there.

Here is my code on the perl side:

my @wall_data = ();
while(my $wallref = $wallpostquery->fetchrow_hashref())
{
my %walldata;

$walldata{WALL_SUBJECT} = $wallref{'SUBJECT'};
$walldata{WALL_DATE} = $wallref{'DATE'};
$walldata{WALL_POSTID} = $wallref{'POSTID'};
$walldata{WALL_MESSAGE} = $wallref{'MESSAGE'};

push (@wall_data, \%walldata);
}   
$template->param(WALL_LOOP => \...@wall_data);

Web side:


		 - Posted by NAME="WALL_POSTID"> on 






thanks for those that help, sometimes handling data types is hard for me.

-Scott


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




Re: html template loops not showing data

2009-07-10 Thread Shawn H. Corey
On Fri, 2009-07-10 at 15:19 -0600, Scott wrote:
> Hello,
> I almost have this but cannot figure out why its not showing the data, 
> right now it knows how many entries im putting in because it spits out 
> that number of rows.
> 
> I am getting my data from dbd::mysql, it works because i have tested in 
> in a normal test.pl and it prints out the data but not using an array or 
> hash.
> I tried to follow this example: 
> http://search.cpan.org/~samtregar/HTML-Template-2.9/Template.pm#TMPL_LOOP
> I think its the second loop example on there.
> 
> Here is my code on the perl side:
> 
> my @wall_data = ();
>   while(my $wallref = $wallpostquery->fetchrow_hashref())
>   {
>   my %walldata;
>   
>   $walldata{WALL_SUBJECT} = $wallref{'SUBJECT'};
>   $walldata{WALL_DATE} = $wallref{'DATE'};
>   $walldata{WALL_POSTID} = $wallref{'POSTID'};
>   $walldata{WALL_MESSAGE} = $wallref{'MESSAGE'};
>   
>   push (@wall_data, \%walldata);

# The above pushes the address of hash on the array, over and over
again.
 push @wall_data, { %walldata };
# This code pushes an anonymous hash containing the data on to the
array.


>   }   
>   $template->param(WALL_LOOP => \...@wall_data);
> 
> Web side:
> 
>   
>- Posted by 
>  NAME="WALL_POSTID"> on 
>   
>   
>   
>   
> 
> thanks for those that help, sometimes handling data types is hard for me.
> 
> -Scott
> 
> 
-- 
Just my 0.0002 million dollars worth,
  Shawn

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



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




Re: html template loops not showing data

2009-07-10 Thread Scott

Shawn H. Corey wrote:

On Fri, 2009-07-10 at 15:19 -0600, Scott wrote:

Hello,
I almost have this but cannot figure out why its not showing the data, 
right now it knows how many entries im putting in because it spits out 
that number of rows.


I am getting my data from dbd::mysql, it works because i have tested in 
in a normal test.pl and it prints out the data but not using an array or 
hash.
I tried to follow this example: 
http://search.cpan.org/~samtregar/HTML-Template-2.9/Template.pm#TMPL_LOOP

I think its the second loop example on there.

Here is my code on the perl side:

my @wall_data = ();
while(my $wallref = $wallpostquery->fetchrow_hashref())
{
my %walldata;

$walldata{WALL_SUBJECT} = $wallref{'SUBJECT'};
$walldata{WALL_DATE} = $wallref{'DATE'};
$walldata{WALL_POSTID} = $wallref{'POSTID'};
$walldata{WALL_MESSAGE} = $wallref{'MESSAGE'};

push (@wall_data, \%walldata);


# The above pushes the address of hash on the array, over and over
again.
 push @wall_data, { %walldata };
# This code pushes an anonymous hash containing the data on to the
array.



}   
$template->param(WALL_LOOP => \...@wall_data);

Web side:


		 - Posted by NAME="WALL_POSTID"> on 






thanks for those that help, sometimes handling data types is hard for me.

-Scott



that was really fast,

I tried that and still no go here is what i have now, i modified the 
query so i know its getting the data correctly just not to the html 
part. And I think your right, it is something to do with how its pushing 
the has onto the array, it still sees the total rows of the hash because 
its printing out the number of entries from the database just not the 
data ;S


	my $wallpostquery = $dbstore->prepare("SELECT * FROM userwall WHERE 
USERID='6'") or die "Unable to connect: $DBI::errstr\n";

$wallpostquery->execute();
my @wall_data = ();
while(my $wallref = $wallpostquery->fetchrow_hashref())
{
my %walldata;

$walldata{WALL_SUBJECT} = $wallref{'SUBJECT'};
$walldata{WALL_DATE} = $wallref{'DATE'};
$walldata{WALL_POSTID} = $wallref{'POSTID'};
$walldata{WALL_MESSAGE} = $wallref{'MESSAGE'};

push @wall_data, { %walldata };
}   
$template->param(WALL_LOOP => \...@wall_data);

$wallpostquery->finish();


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




Re: html template loops not showing data

2009-07-10 Thread Jim Gibson
On 7/10/09 Fri  Jul 10, 2009  2:25 PM, "Shawn H. Corey"
 scribbled:

> On Fri, 2009-07-10 at 15:19 -0600, Scott wrote:
>> Hello,

>> Here is my code on the perl side:
>> 
>> my @wall_data = ();
>> while(my $wallref = $wallpostquery->fetchrow_hashref())
>> {
>> my %walldata;
>> 
>> $walldata{WALL_SUBJECT} = $wallref{'SUBJECT'};
>> $walldata{WALL_DATE} = $wallref{'DATE'};
>> $walldata{WALL_POSTID} = $wallref{'POSTID'};
>> $walldata{WALL_MESSAGE} = $wallref{'MESSAGE'};
>> 
>> push (@wall_data, \%walldata);
> 
> # The above pushes the address of hash on the array, over and over
> again.

Which works because %walldata is localized to the while loop and is
re-allocated each time through the loop. Each loop iteration creates a new
hash variable, and the array @wall_data contains a list of distinct hash
references.

>  push @wall_data, { %walldata };
> # This code pushes an anonymous hash containing the data on to the
> array.

Which also works, because a copy of the hash is saved, but it is not as
efficient.

In other words, the problem lies elsewhere. As far as I can tell the code is
fine as shown, but I don't have any experience with HTML::Template.



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




Re: html template loops not showing data

2009-07-10 Thread Scott

Jim Gibson wrote:

On 7/10/09 Fri  Jul 10, 2009  2:25 PM, "Shawn H. Corey"
 scribbled:


On Fri, 2009-07-10 at 15:19 -0600, Scott wrote:

Hello,



Here is my code on the perl side:

my @wall_data = ();
while(my $wallref = $wallpostquery->fetchrow_hashref())
{
my %walldata;

$walldata{WALL_SUBJECT} = $wallref{'SUBJECT'};
$walldata{WALL_DATE} = $wallref{'DATE'};
$walldata{WALL_POSTID} = $wallref{'POSTID'};
$walldata{WALL_MESSAGE} = $wallref{'MESSAGE'};

push (@wall_data, \%walldata);

# The above pushes the address of hash on the array, over and over
again.


Which works because %walldata is localized to the while loop and is
re-allocated each time through the loop. Each loop iteration creates a new
hash variable, and the array @wall_data contains a list of distinct hash
references.


 push @wall_data, { %walldata };
# This code pushes an anonymous hash containing the data on to the
array.


Which also works, because a copy of the hash is saved, but it is not as
efficient.

In other words, the problem lies elsewhere. As far as I can tell the code is
fine as shown, but I don't have any experience with HTML::Template.



I found the error, seems to not be putting the stuff into the hash right 
or something. I found a fix so thought i should put it on the list.

Here is an example i found after digging around on google:


   
  
  
  
  
   





# songs.cgi
use DBI;
use CGI;
use HTML::Template;
use strict;

my $DBH = DBI->connect(
   qw(DBI:vendor:database:host user pass),
   { RaiseError => 1}
);
my $CGI = CGI->new();

# grab the stuff from the database
my $sth = $DBH->prepare('
   select title, artist, album, year
   from songs
');
$sth->execute();

# prepare a data structure for HTML::Template
my $rows;
push @{$rows}, $_ while $_ = $sth->fetchrow_hashref();

# instantiate substitute the values
$template->param(ROWS => $rows);

print $CGI->header();
print $template->output();


$DBH->disconnect();

Thanks again for everyone helping me on this.


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




Re: html template loops not showing data

2009-07-10 Thread Steve Bertrand
scott wrote:
> Jim Gibson wrote:
>> On 7/10/09 Fri  Jul 10, 2009  2:25 PM, "Shawn H. Corey"
>>  scribbled:
>>
>>> On Fri, 2009-07-10 at 15:19 -0600, Scott wrote:
 Hello,
>>
 Here is my code on the perl side:

 my @wall_data = ();
 while(my $wallref = $wallpostquery->fetchrow_hashref())
 {
 my %walldata;

 $walldata{WALL_SUBJECT} = $wallref{'SUBJECT'};
 $walldata{WALL_DATE} = $wallref{'DATE'};
 $walldata{WALL_POSTID} = $wallref{'POSTID'};
 $walldata{WALL_MESSAGE} = $wallref{'MESSAGE'};

 push (@wall_data, \%walldata);
>>> # The above pushes the address of hash on the array, over and over
>>> again.
>>
>> Which works because %walldata is localized to the while loop and is
>> re-allocated each time through the loop. Each loop iteration creates a
>> new
>> hash variable, and the array @wall_data contains a list of distinct hash
>> references.
>>
>>>  push @wall_data, { %walldata };
>>> # This code pushes an anonymous hash containing the data on to the
>>> array.
>>
>> Which also works, because a copy of the hash is saved, but it is not as
>> efficient.
>>
>> In other words, the problem lies elsewhere. As far as I can tell the
>> code is
>> fine as shown, but I don't have any experience with HTML::Template.
>>
>>
>>
> I found the error, seems to not be putting the stuff into the hash right
> or something. I found a fix so thought i should put it on the list.
> Here is an example i found after digging around on google:

Aside from what you've found, it may help if you review a thread I
recently started:

http://osdir.com/ml/perl-beginners/2009-06/msg00295.html

Cheers,

Steve


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




Re: Index a list

2009-07-10 Thread Steve
On Fri, 2009-07-10 at 13:13 -0700, Jim Gibson wrote:
> On 7/10/09 Fri  Jul 10, 2009  12:37 PM, "Steve"
>  scribbled:
> 
> > Hi list memebers. I'm new here and I'm interested in learning about
> > Perl. I've managed to get some programs together but have a lot to
> > learn.
> > 
> > May I put a question to the experts?
> > 
> > Suppose I have a text file that has a whopping amount of lines
> > (20-50,000). What would be the best way to condense or index this with
> > Perl so I could query it and get a yes/no on it as fast as possible with
> > minimum overhead? I could maintain a database to do it, but for one line
> > entries this would probably be overkill. It's easy to swap a text file
> > to update it and manage the list concerned.
> > 
> > What would be the best approach?
> 
> That depends upon the query that you want to do. If it is just to see if a
> line exists in the file, then a hash would be best:
> 
> my %hash;
> while(<>) {
> chomp;
> $hash{$_} = 1;
> }
> 
> ...
> 
> if( exists $hash{$someline} ) {
> print "Line <$someline> exists\n";
> }else{
> print "Line <$someline> not found\n";
> }
> 
> If it is something more complex, then reading in the file to an array might
> work efficiently.
> 
> We need a little more information to help you further.
> 
> 
The list concerned is updated on a daily - and sometimes hourly - basis
and runs to about 90,000 lines as I look at it this morning.

If I used a database it would mean dropping the table and repopulating
it on a list change - rather than just swapping out the list and
building an index.

Each line of the file contains a single uri. The script will run
frequently - many times a minute - upon demand. To complicate things a
little there may be a total of three similar lists. One for URI's, one
for DOMAINS and one for TELEPHONE NUMBERS.

My key objectives are fast loading, fast acting and minimal overhead. (I
also live in an ideal world!)

Here is what I've considered;
Load the lists into a HASH on start. This is going to mean big hashes
and will take time. It strikes me as 'daft' to do this. If I could index
the list and load that instead I suspect it would take less room and
execute faster.

I suspect what I really need is a very simple form of indexing database.
I think the overhead of mysql or postgresql would be serious overkill
for single line queries. I'm hoping someone can suggest a much lighter
alternative.

If I can give an analogy - something that works a bit like Postfix's
'postmap' command may be ideal. It turns a flat file into a smaller
faster index file. I've seen some variations on this where .idx (rather
than .db) files have been created - but being new to this I don't fully
understand them.

Reading that back I don't know if I've made things any clearer or mad
things more muddy :-)



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