Need explanation about this code.

2009-10-08 Thread Raheel Hassan
Hello,

I have problems in understanding $...@$ use ?

1- my $ref = $$temp_sth - fetchall_arrayref({});
for(my $i=0; $i = $...@$ref}; $i++) {
push(@$temp_table,$ref-[$i]);}

Can some one explain the under given function.
2- sub buildSensorsHashtable
{

   my ($dbh) = @_;
   my $error = 0;

 my $query = SELECT * FROM Sensors;
 my $sth = $dbh-prepare($query);
 if(!$sth || !($sth-execute())) { $error = 1 ;
 }

 my $ref = $sth-fetchall_arrayref({});
 my %s;
 my @s = @$ref;
foreach (@s)
{
if($_-{StatusID} eq 1) { $s{$_-{IP}} = $_-{SensorID}; }
}

return (\%s,$error);

3- Which is the best tool for debuging perl programs having CGI support. I
used DDD but that does not support CGI.

Regards,
Raheel


Re: Need explanation about this code.

2009-10-08 Thread Jim Gibson
On 10/8/09 Thu  Oct 8, 2009  9:00 AM, Raheel Hassan
raheel.has...@gmail.com scribbled:

 Hello,
 
 I have problems in understanding $...@$ use ?
 
 1- my $ref = $$temp_sth - fetchall_arrayref({});
 for(my $i=0; $i = $...@$ref}; $i++) {

For any array @a, the largest index is given by $#a.

$ref is a reference to an array, as returned by the fetchall_arrayref
method.

@{$ref} is the array returned.

Therefore, $...@$ref} is the largest index of the returned array, i.e., the
index of the last element of the array.


 push(@$temp_table,$ref-[$i]);}
 
 Can some one explain the under given function.

It looks like a function that queries a database, getting all of the data
from a table called Sensors. Then for each row in that table, checks the
value of the column labeled StatusID, and, if the value of that column is
1, adds an entry to a hash with the contents of the IP column as key,
and the value taken from the column SensorID. The function returns a list,
consisting of a reference to the hash and the value of the $error variable,
which will be 0 if no error occurred, and 1 if an error did occur.

 2- sub buildSensorsHashtable
 {
 
my ($dbh) = @_;
my $error = 0;
 
  my $query = SELECT * FROM Sensors;
  my $sth = $dbh-prepare($query);
  if(!$sth || !($sth-execute())) { $error = 1 ;
  }
 
  my $ref = $sth-fetchall_arrayref({});
  my %s;
  my @s = @$ref;
 foreach (@s)
 {
 if($_-{StatusID} eq 1) { $s{$_-{IP}} = $_-{SensorID}; }
 }
 
 return (\%s,$error);
 
 3- Which is the best tool for debuging perl programs having CGI support. I
 used DDD but that does not support CGI.

Debugging CGI programs can be difficult. You need access to the HTTP logs.
You can help yourself by following the advice given in the FAQ How can I
get better error messages from a CGI program? (see 'perldoc -g CGI'). Try
to set up a test environment, either by executing the CGI program from the
command line (by supplying the various parameters needed) or by running the
program first on a local server where you have access to the logs.

Good luck.



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




Re: Need explanation about this code.

2009-10-08 Thread John W. Krahn

Raheel Hassan wrote:

Hello,


Hello,


I have problems in understanding $...@$ use ?

1- my $ref = $$temp_sth - fetchall_arrayref({});
for(my $i=0; $i = $...@$ref}; $i++) {
push(@$temp_table,$ref-[$i]);}


$...@$ref} should be $#{$ref} or simply $#$ref

And

for(my $i=0; $i = $...@$ref}; $i++) {

should be

for my $i ( 0 .. $#$ref ) {

And

 for(my $i=0; $i = $...@$ref}; $i++) {
 push(@$temp_table,$ref-[$i]);}

should be

 push @$temp_table, @$ref;




John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.   -- Damian Conway

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




for output from other unix commands

2009-10-08 Thread Harry Putnam

I'm just looking for a push to get started... I do have some perl
skills (pretty lowlevel to be sure) so can do basic formating with
printf and such.

But here... I expect my input to be pretty uniform actually already
formatted ok. Except, 1 or 2 lines somewhere in the input that will be
longer and make reading the output a bit of a chore.

I want to catch those long lines and format them like one might format
a news/mail message... wrapped at column 72 or so but also indented 
whatever spcs looks good.

I looked at perldoc -f format but that says its about pictures and
doesn't appear to be intended to format text.

Imagine input like:

somevar=settingotherinfootherinfo2
somevar=setting, that , is way longer than any thing else in the 
history of mankindotherinfootherinfo2
somevar=settingotherinfootherinfo2
----   ---=---   -   

The `var=setting' with all the baloney should be wrapped but still end
up with the last 2 elements lined up like the other lines:

somevar=settingotherinfootherinfo2
somevar=setting, that , is way longer than any 
thing else in the history of mankind 
   otherinfootherinfo2
somevar=settingotherinfootherinfo2



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




Re: adding populations

2009-10-08 Thread John W. Krahn

Jesus Fernandez wrote:

Dear friends,


Hello


I'm trying to write a program that draws 5,000 numbers from an exponential
distribution with mean 4N/k(k-1)

N=10 000
k = 25

I have this:

#!jesusafernandez/bin/perl


use warnings;
use strict;


$k = 25;


my $k = 25;


$N = 1;


my $N = 10_000;


for ($samp=0; $samp5000; $samp++)
{


for my $samp ( 0 .. 4_999 ) {


while ($k!= $25)


The numeric variable $25 can only be used with regular expressions. 
Perhaps you meant:


while ( $k != 25 )


{
$mean = 4$N/$k($k-1));


You are missing an operator between 4 and $N and between $k and ($k-1). 
 Perhaps you meant:


my $mean = 4 * $N / $k * ( $k - 1 );


$time = (-log (rand)*$ mean);


Although a space between '$' and 'mean' is allowed it is usually not 
good practice.


my $time = -log( rand ) * $mean;


push (@xx,$time);
$k=$k-y}


The string literal 'y' should be in quotes, but a string literal used in 
numerical context will just evaluate to the number 0 so:


$k -= 0;
}

Although using 0 there does not make much sense.


print $time\n;


You are missing the closing } for the for loop.



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.   -- Damian Conway

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




Re: for output from other unix commands

2009-10-08 Thread Shlomi Fish
Hi Harry!

On Thursday 08 Oct 2009 21:29:04 Harry Putnam wrote:
 I'm just looking for a push to get started... I do have some perl
 skills (pretty lowlevel to be sure) so can do basic formating with
 printf and such.
 
 But here... I expect my input to be pretty uniform actually already
 formatted ok. Except, 1 or 2 lines somewhere in the input that will be
 longer and make reading the output a bit of a chore.
 
 I want to catch those long lines and format them like one might format
 a news/mail message... wrapped at column 72 or so but also indented
 whatever spcs looks good.
 
 I looked at perldoc -f format but that says its about pictures and
 doesn't appear to be intended to format text.
 

Have you tried http://search.cpan.org/dist/Perl6-Form/ ? Or maybe:

http://search.cpan.org/dist/Text-Tabs+Wrap/

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
http://www.shlomifish.org/humour/ways_to_do_it.html

Chuck Norris read the entire English Wikipedia in 24 hours. Twice.

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




Re: for output from other unix commands

2009-10-08 Thread Harry Putnam
Shlomi Fish shlo...@iglu.org.il writes:

 Hi Harry!

[...]

 
 I want to catch those long lines and format them like one might format
 a news/mail message... wrapped at column 72 or so but also indented
 whatever spcs looks good.
 
 I looked at perldoc -f format but that says its about pictures and
 doesn't appear to be intended to format text.
 

 Have you tried http://search.cpan.org/dist/Perl6-Form/ ? Or maybe:

 http://search.cpan.org/dist/Text-Tabs+Wrap/

Those do look tempting, especially the first, but both seem a little
heavy handed for what I need... And might take me a good bit to figure
out how to use themI may just look for a specific line and
torture it till it looks like I want it too.

At this point my input will only include one recalcitrant line where
there is a problem... but it makes the tool whos' output I want to fix
put out all lines to match that long one... 

Where all but one line follow this:
some  setting=wordsetting=word setting=word

----   ---=---   -   
Adding the bad line does this:
some  setting=word lots of extra space now  here
setting=word setting=word
some  OtherSetting=a whole big long line of never ending words like this one 
causing the program to ouput   setting=word setting=word
----   ---=---   -   

So the line with OtherSetting is causing all lines to be output with
space to accommodate the long setting.

The tool provided by OS does that on its own so I want to fix its ouput... 

I guess just watching for that line.. and manually folding it with
perl will do what I want.  If I can figure out how to clip out the
extra space now included in all other lines.


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




Re: for output from other unix commands

2009-10-08 Thread Jim Gibson
On 10/8/09 Thu  Oct 8, 2009  3:56 PM, Harry Putnam rea...@newsguy.com
scribbled:

 Shlomi Fish shlo...@iglu.org.il writes:
 
 Hi Harry!
 
 [...]
 
 
 I want to catch those long lines and format them like one might format
 a news/mail message... wrapped at column 72 or so but also indented
 whatever spcs looks good.
 
 I looked at perldoc -f format but that says its about pictures and
 doesn't appear to be intended to format text.
 
 
 Have you tried http://search.cpan.org/dist/Perl6-Form/ ? Or maybe:
 
 http://search.cpan.org/dist/Text-Tabs+Wrap/
 
 Those do look tempting, especially the first, but both seem a little
 heavy handed for what I need... And might take me a good bit to figure
 out how to use themI may just look for a specific line and
 torture it till it looks like I want it too.
 
 At this point my input will only include one recalcitrant line where
 there is a problem... but it makes the tool whos' output I want to fix
 put out all lines to match that long one...
 
 Where all but one line follow this:
 some  setting=wordsetting=word setting=word
 
 ----   ---=---   -  
 Adding the bad line does this:
 some  setting=word lots of extra space now  here
 setting=word setting=word
 some  OtherSetting=a whole big long line of never ending words like this one
 causing the program to ouput   setting=word setting=word
 ----   ---=---   -  
 
 So the line with OtherSetting is causing all lines to be output with
 space to accommodate the long setting.
 
 The tool provided by OS does that on its own so I want to fix its ouput...
 
 I guess just watching for that line.. and manually folding it with
 perl will do what I want.  If I can figure out how to clip out the
 extra space now included in all other lines.
 

You might try splitting the line on whitespace, extracting the first two and
last two elements, then joining the remainder back with a space. You can
then test the length of the middle string for excessive length and print or
trim accordingly. This will concatenate any extra spaces in the middle
string.

This only works if the first two and last two fields contain no spaces.



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




Building a record on the fly via hash of hashes

2009-10-08 Thread Soham Das
Hello All,

I am doing some file reading operation, and parsing the data(its a CSV file) 
with a hash reference and then intend to store it in a record.

something like:

loop: until file ends;
 $hashref-{'A'}=$filehandle-{'Action'};
 $hashref-{'B'}= $filehandle-{'Name'};
 $hashref-{'C'}= $filehandle-{'System'};
 $hashref-{'D'}=($filehandle-{'Price'});

 $recordref-{$hashref-{'B'}}= $hashref;

loop : ends

Here Action, Name,System,price are the CSV headers. 

Now, when the first line is read, the details are parsed and stored in the 
$recordref as a hash reference. Now when the loop iterates, and goes to the 
second line. The first line contents are lost. Because the hash reference now 
points to the newer data.

How do I overcome this?
More importantly do we have a push equivalent for hash of hashes?

Soham


Send free SMS to your Friends on Mobile from your Yahoo! Messenger. Download 
Now! http://messenger.yahoo.com/download.php

Re: Building a record on the fly via hash of hashes

2009-10-08 Thread Jim Gibson

At 10:14 PM -0700 10/8/09, Soham Das wrote:
Hello All, I am doing some file reading operation, and parsing the 
data(its a CSV file) with a hash reference and then intend to store 
it in a record. something like: loop: until file ends; 
$hashref-{'A'}=$filehandle-{'Action'}; 
$hashref-{'B'}= $filehandle-{'Name'}; 
$hashref-{'C'}= $filehandle-{'System'}; 
$hashref-{'D'}=($filehandle-{'Price'}); 
$recordref-{$hashref-{'B'}}= $hashref; loop : ends



You are better off including some actual Perl code, including some sample data.

Here Action, Name,System,price are the CSV headers. Now, when the 
first line is read, the details are parsed and stored in the 
$recordref as a hash reference. Now when the loop iterates, and goes 
to the second line. The first line contents are lost. Because the 
hash reference now points to the newer data. How do I overcome this? 
More importantly do we have a push equivalent for hash of hashes?


No. Unless each record has a unique key, you are better off using an 
array of hashes, rather than a hash of hashes. Of course, you could 
use the line number as a unique key, but an array would be more 
efficient.


--
Jim Gibson
j...@gibson.org

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




Re: Building a record on the fly via hash of hashes

2009-10-08 Thread Soham Das


The code:




From: Jim Gibson jimsgib...@gmail.com
To: beginners@perl.org
Sent: Fri, 9 October, 2009 11:00:55 AM
Subject: Re: Building a record on the fly via hash of hashes

At 10:14 PM -0700 10/8/09, Soham Das wrote:
 Hello All, I am doing some file reading operation, and parsing the data(its a 
 CSV file) with a hash reference and then intend to store it in a record. 
 something like: loop: until file ends; 
 $hashref-{'A'}=$filehandle-{'Action'}; $hashref-{'B'}= 
 $filehandle-{'Name'}; $hashref-{'C'}= $filehandle-{'System'}; 
 $hashref-{'D'}=($filehandle-{'Price'}); $recordref-{$hashref-{'B'}}= 
 $hashref; loop : ends


You are better off including some actual Perl code, including some sample data.

 Here Action, Name,System,price are the CSV headers. Now, when the first line 
 is read, the details are parsed and stored in the $recordref as a hash 
 reference. Now when the loop iterates, and goes to the second line. The first 
 line contents are lost. Because the hash reference now points to the newer 
 data. How do I overcome this? More importantly do we have a push equivalent 
 for hash of hashes?

No. Unless each record has a unique key, you are better off using an array of 
hashes, rather than a hash of hashes. Of course, you could use the line number 
as a unique key, but an array would be more efficient.

-- Jim Gibson
j...@gibson.org

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


  Yahoo! India has a new look. Take a sneak peek http://in.yahoo.com/trynew

Re: Building a record on the fly via hash of hashes

2009-10-08 Thread Soham Das
The Code:

use strict;
use warnings;
use Tie::Handle::CSV;

my $qbook = Tie::Handle::CSV-new('C:\Documents and Settings\Soham 
Das\Desktop\Quotes.csv',header=1);
my $tradebook = Tie::Handle::CSV-new('C:\Documents and Settings\Soham 
Das\Desktop\Transactions.csv',header=1);

my $dailytrade={};
my $portfolio={};
my $singletrade={};
while(my $mktdates =$qbook)
{
while(my $trades=$tradebook)
{
if($trades-{'Date'} eq $mktdates-{'Date'})
{
 $singletrade-{'Action'}=$trades-{'Action'};
 $singletrade-{'Scrip'}= $trades-{'Scrip'};
 $singletrade-{'Shares'}= $trades-{'Shares'};
 $singletrade-{'Price'}=($trades-{'Price'})*1.00845;

 $dailytrade-{$singletrade-{'Scrip'}}= $singletrade;
# print $dailytrade-{$singletrade-{'Scrip'}}-{'Price'},\n;
}
#Update the portfolio

}
}
close $qbook ;
close $tradebook;

Soham

P.S: apologies for the previous mail being a blank mail, it was a mistake





From: Soham Das soham...@yahoo.co.in
To: beginners@perl.org
Sent: Fri, 9 October, 2009 11:02:51 AM
Subject: Re: Building a record on the fly via hash of hashes




The code:




From: Jim Gibson jimsgib...@gmail.com
To: beginners@perl.org
Sent: Fri, 9 October, 2009 11:00:55 AM
Subject: Re: Building a record on the fly via hash of hashes

At 10:14 PM -0700 10/8/09, Soham Das wrote:
 Hello All, I am doing some file reading operation, and parsing the data(its a 
 CSV file) with a hash reference and then intend to store it in a record. 
 something like: loop: until file ends; 
 $hashref-{'A'}=$filehandle-{'Action'}; $hashref-{'B'}= 
 $filehandle-{'Name'}; $hashref-{'C'}= $filehandle-{'System'}; 
 $hashref-{'D'}=($filehandle-{'Price'}); $recordref-{$hashref-{'B'}}= 
 $hashref; loop : ends


You are better off including some actual Perl code, including some sample data.

 Here Action, Name,System,price are the CSV headers. Now, when the first line 
 is read, the details are parsed and stored in the $recordref as a hash 
 reference. Now when the loop iterates, and goes to the second line. The first 
 line contents are lost. Because the hash reference now points to the newer 
 data. How do I overcome this? More importantly do we have a push equivalent 
 for hash of hashes?

No. Unless each record has a unique key, you are better off using an array of 
hashes, rather than a hash of hashes. Of course, you could use the line number 
as a unique key, but an array would be more efficient.

-- Jim Gibson
j...@gibson.org

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




 Now, send attachments up to 25MB with Yahoo! India Mail. Learn how.

Send free SMS to your Friends on Mobile from your Yahoo! Messenger. Download 
Now! http://messenger.yahoo.com/download.php