Perl Formula Help - Changing a Price in script

2012-03-20 Thread Dave K
Hello - I am new to Perl, and looking for some much needed direction.

I am trying to get a basic formula in place (within an existing IF
statement) that will serve to increase my price (i.e., $new_price)
by the following formula:

=PRICE divided by 0.8  plus 15

In other words, if the Price is equal to 3, the results of the
$new_price should be 18.75.  And if the price is equal to 5, then the
$new_price should be equal to 21.25.

The script that I am trying to alter  is shown below. Can someone
possibly let me know how I can alter the below script (which I believe
would mean to alter the line that reads $new_price = $o_list[2]-
{price};) so that the new price would would work as described above?
Also, just a layman's explanation of the syntax shown below would be
greatly helpful.  I am trying to understand what the -= and the -
and the ? characters represent.

Thanks for any suggestions or guidance.

#
if ( @o_list ) {
 if ( defined ( $o_list[2] ) ) {
 $new_price = $o_list[2]-{price};
 $new_price -= $new_price = 0 ? $new_price - .01 : 0;
 }
}


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




Re: Perl Formula Help - Changing a Price in script

2012-03-20 Thread John W. Krahn

Dave K wrote:

Hello - I am new to Perl, and looking for some much needed direction.

I am trying to get a basic formula in place (within an existing IF
statement) that will serve to increase my price (i.e., $new_price)
by the following formula:

=PRICE divided by 0.8  plus 15

In other words, if the Price is equal to 3, the results of the
$new_price should be 18.75.  And if the price is equal to 5, then the
$new_price should be equal to 21.25.

The script that I am trying to alter  is shown below. Can someone
possibly let me know how I can alter the below script (which I believe
would mean to alter the line that reads $new_price = $o_list[2]-

{price};) so that the new price would would work as described above?

Also, just a layman's explanation of the syntax shown below would be
greatly helpful.  I am trying to understand what the -=


$x -= $y;

Is  short for:

$x = $x - $y;

It is borrowed from the C programming language.



and the -


That is the dereference operator.

perldoc perlop
perldoc perlref
perldoc perldsc



and the ? characters represent.


? : is the conditional operator.

my $x = $a  $b ? $y : $z;

Is equivalent to:

my $x;
if ( $a  $b ) {
$x = $y;
}
else {
$x = $z;
}



Thanks for any suggestions or guidance.

#
if ( @o_list ) {
  if ( defined ( $o_list[2] ) ) {
  $new_price = $o_list[2]-{price};
  $new_price -= $new_price= 0 ? $new_price - .01 : 0;
  }
}


if ( @o_list  defined $o_list[ 2 ] ) {
$new_price = $o_list[ 2 ]{ price } / 0.8 + 15;
}




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

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




Re: XML::Twig Doubt

2012-03-20 Thread Rob Dixon

On 19/03/2012 15:12, Anirban Adhikary wrote:

Hi,
When I am trying to print the value against the tag ALPHA it is not prints
anything,though it is not showing any warnings.


use strict;
use warnings;

use XML::Twig;

my $twig = XML::Twig-new(start_tag_handlers =  {
BSC =  \on_BSC
});

sub on_BSC {
  my($twig, $bsc)= @_;
  print $bsc-id, \n;
  my $alpha = $bsc-field('ALPHA');
  print $alpha, \n;
  $twig-purge;
}

$twig-parsefile(ISProducts.xml');


Hi List,
When I have changed the method
from my $twig = XML::Twig-new(start_tag_
handlers =  {
BSC =  \on_BSC
});
  to
my $twig = XML::Twig-new(TwigHandlers =  {
BSC =  \on_BSC
});

I am able to print the value against the ALPHA tag.

Thanks to you for your support.


Hi Anirban

I suggested using start_tag_handlers because what you wanted to do was
extract the id attribute from the start tag of all BSC elements, so
there was no point in having any more of the element in store that just
that start tag. It is also more efficient

Changing to twig_handlers makes entirety of each twig (the BSC
element) available, so you are able to access the child elements as you
have found.

Rob

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




Re: XML::Twig Doubt

2012-03-20 Thread Rob Dixon

On 19/03/2012 14:45, Anirban Adhikary wrote:

On Mon, Mar 19, 2012 at 7:41 PM, Rob Dixonrob.di...@gmx.com  wrote:


XML::Twig uses callbacks to process pieces of the XML that you have
defined. In this case you are interested only in theBSC  start tag so
you can define a start tag handler. Using $twig-purge empties the
data read so far. If you use this once you have accessed all the
information you need from a given element there is no need to store the
entire XML data in memory.

The program below doeas what I think you want.

HTH,

Rob


use strict;
use warnings;

use XML::Twig;

my $twig = XML::Twig-new(start_tag_**handlers =  {
BSC =  \on_BSC
});

sub on_BSC {
  my($twig, $bsc)= @_;
  print $bsc-id, \n;
  $twig-purge;
}

$twig-parsefile('ISProducts.**xml');


Thanks for your support.The code does exactly what I want .
Can you please suggest me a tutorials with good examples on XML::Twig.

Another thing can you please explain me this line in the code my($twig,
$bsc)= @_;



When I am trying to print the value against the tag ALPHA it is not prints
anything,though it is not showing any warnings.

use strict;
use warnings;

use XML::Twig;

my $twig = XML::Twig-new(start_tag_handlers =  {
BSC =  \on_BSC
});

sub on_BSC {
  my($twig, $bsc)= @_;
  print $bsc-id, \n;
  my $alpha = $bsc-field('ALPHA');
  print $alpha, \n;
  $twig-purge;
}

$twig-parsefile(ISProducts.xml');


Hi Anirban

I use the POD documentation supplied with the module, but I agree it is
a little opaque. XML::Twig has its own website at http://xmltwig.org
where you will find some tutorials that may help you.

The line

  my ($twig, $bsc)= @_;

copies the values supplied as parameters to the callback into local
variables. $twig is the XML::Twig object itself, and $bsc is an
XML::Twig::Elt object representing the BSC XML element that has been
found.

You are unable to access the child elements of each BSC because I have
suggested you specify start_tag_handlers for which only the start tag
is available. That was fine when you just wanted the id attribute from
the tag, but there is no information about any other part of the XML.

You need to use twig_handlers instead, when the entire BSC element
will be accessible using

  $bsc-first_child_trimmed_text('ALPHA')

HTH,

Rob

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




Re: XML::Twig Doubt

2012-03-20 Thread Anirban Adhikary
Thanks a lot Rob for your nice help.

Best Regards
Anirban Adhikary.

On Tue, Mar 20, 2012 at 4:38 PM, Rob Dixon rob.di...@gmx.com wrote:

 On 19/03/2012 14:45, Anirban Adhikary wrote:

 On Mon, Mar 19, 2012 at 7:41 PM, Rob Dixonrob.di...@gmx.com  wrote:


 XML::Twig uses callbacks to process pieces of the XML that you have
 defined. In this case you are interested only in theBSC  start tag so
 you can define a start tag handler. Using $twig-purge empties the
 data read so far. If you use this once you have accessed all the
 information you need from a given element there is no need to store the
 entire XML data in memory.

 The program below doeas what I think you want.

 HTH,

 Rob


 use strict;
 use warnings;

 use XML::Twig;

 my $twig = XML::Twig-new(start_tag_handlers =  {

BSC =  \on_BSC
 });

 sub on_BSC {
  my($twig, $bsc)= @_;
  print $bsc-id, \n;
  $twig-purge;
 }

 $twig-parsefile('ISProducts.xml');


 Thanks for your support.The code does exactly what I want
 .
 Can you please suggest me a tutorials with good examples on XML::Twig.

 Another thing can you please explain me this line in the code my($twig,
 $bsc)= @_;


 When I am trying to print the value against the tag ALPHA it is not prints
 anything,though it is not showing any warnings.

 use strict;
 use warnings;

 use XML::Twig;

 my $twig = XML::Twig-new(start_tag_**handlers =  {
BSC =  \on_BSC
 });

 sub on_BSC {
  my($twig, $bsc)= @_;
  print $bsc-id, \n;
  my $alpha = $bsc-field('ALPHA');
  print $alpha, \n;
  $twig-purge;
 }

 $twig-parsefile(ISProducts.**xml');


 Hi Anirban

 I use the POD documentation supplied with the module, but I agree it is
 a little opaque. XML::Twig has its own website at http://xmltwig.org
 where you will find some tutorials that may help you.

 The line

  my ($twig, $bsc)= @_;

 copies the values supplied as parameters to the callback into local
 variables. $twig is the XML::Twig object itself, and $bsc is an
 XML::Twig::Elt object representing the BSC XML element that has been
 found.

 You are unable to access the child elements of each BSC because I have
 suggested you specify start_tag_handlers for which only the start tag
 is available. That was fine when you just wanted the id attribute from
 the tag, but there is no information about any other part of the XML.

 You need to use twig_handlers instead, when the entire BSC element
 will be accessible using

  $bsc-first_child_trimmed_**text('ALPHA')

 HTH,

 Rob



Re: Perl Formula Help - Changing a Price in script

2012-03-20 Thread Shawn H Corey

On 12-03-20 05:39 AM, John W. Krahn wrote:

$x -= $y;

Is  short for:

$x = $x - $y;

It is borrowed from the C programming language.


It would be more accurate to say it's equivalent to:

  $x = $x - ( $y );

This becomes important when using the multiplicative operators:

  $x *= $y + 1;

Which means:

  $x = $x * ( $y + 1 );


--
Just my 0.0002 million dollars worth,
  Shawn

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

Show me your code and conceal your interfaces, and I shall continue
to be mystified. Show me your interfaces, and I won't usually need
your code; it'll be obvious.
-- Fred Brooks [updated]

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