Re: Please help: perl run out of memory

2022-04-17 Thread David Mertens
I see nothing glaringly inefficient in the Perl. This would be fine on your
system if you were dealing with 1 million items, but you could easily be
pushing up against your system's limits with the generic data structures
that Perl uses, especially since Perl is probably using 64-bit floats and
ints, and storing the hash keys twice (because you have to hashes).

You could try to use the Perl Data Language, PDL, to create large typed
arrays with minimal overhead. However, I think a more Perlish approach
would be to use a single hash to store the data, as you do (or maybe using
pack/unpack to store the data using 32-bit floats and integers). Then
instead of using sort, run through the whole collection and build your own
top-20 list (or 50 or whatever) by hand. This way the final process of
picking out the top 20 doesn't allocate new storage for all 80 million
items.

Does that make sense? I could bang out some code illustrating what I mean
if that would help.

David

On Sun, Apr 17, 2022, 5:33 AM wilson  wrote:

> hello the experts,
>
> can you help check my script for how to optimize it?
> currently it was going as "run out of memory".
>
> $ perl count.pl
> Out of memory!
> Killed
>
>
> My script:
> use strict;
>
> my %hash;
> my %stat;
>
> # dataset: userId, itemId, rate, time
> # AV056ETQ5RXLN,031887,1.0,1397692800
>
> open HD,"rate.csv" or die $!;
> while() {
>  my ($item,$rate) = (split /\,/)[1,2];
>  $hash{$item}{total} += $rate;
>  $hash{$item}{count} +=1;
> }
> close HD;
>
> for my $key (keys %hash) {
>  $stat{$key} = $hash{$key}{total} / $hash{$key}{count};
> }
>
> my $i = 0;
> for (sort { $stat{$b} <=> $stat{$a}} keys %stat) {
>  print "$_: $stat{$_}\n";
>  last if $i == 99;
>  $i ++;
> }
>
> The purpose is to aggregate and average the itemId's scores, and print
> the result after sorting.
>
> The dataset has 80+ million items:
>
> $ wc -l rate.csv
> 82677131 rate.csv
>
> And my memory is somewhat limited:
>
> $ free -m
>totalusedfree  shared  buff/cache
> available
> Mem:   1992 152  76   01763
>1700
> Swap:  1023 802 221
>
>
>
> What confused me is that Apache Spark can make this job done with this
> limited memory. It got the statistics done within 2 minutes. But I want
> to give perl a try since it's not that convenient to run a spark job
> always.
>
> The spark implementation:
>
> scala> val schema="uid STRING,item STRING,rate FLOAT,time INT"
> val schema: String = uid STRING,item STRING,rate FLOAT,time INT
>
> scala> val df =
> spark.read.format("csv").schema(schema).load("skydrive/rate.csv")
> val df: org.apache.spark.sql.DataFrame = [uid: string, item: string ...
> 2 more fields]
>
> scala>
>
> df.groupBy("item").agg(avg("rate").alias("avg_rate")).orderBy(desc("avg_rate")).show()
> +--++
>
> |  item|avg_rate|
> +--++
> |0001061100| 5.0|
> |0001543849| 5.0|
> |0001061127| 5.0|
> |0001019880| 5.0|
> |0001062395| 5.0|
> |143502| 5.0|
> |14357X| 5.0|
> |0001527665| 5.0|
> |000107461X| 5.0|
> |191639| 5.0|
> |0001127748| 5.0|
> |791156| 5.0|
> |0001203088| 5.0|
> |0001053744| 5.0|
> |0001360183| 5.0|
> |0001042335| 5.0|
> |0001374400| 5.0|
> |0001046810| 5.0|
> |0001380877| 5.0|
> |0001050230| 5.0|
> +--++
> only showing top 20 rows
>
>
> I think my perl script should be possible to be optimized to run this
> job as well. So ask for your helps.
>
> Thanks in advance.
>
> wilson
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Can't call method "Convolve" on unblessed reference at gifaninmation.pl line 47.

2022-02-04 Thread David Mertens
Thanks!

The problem here is actually fairly straight forward: $image->[1] is not
defined. To verify this, try running the code below, which is a trimmed
version of what you provided. You'll notice that I added a few print
statements, for $x in the for-loop and for $p after we get it from
$image->[1]. You'll notice that (1) no numbers print and (2) we get the
warning "Use of uninitialized value $p in concatenation..." arising from
printing $p. So, what ever you are trying to do is not doing what you think
it is doing. :-/

This looks like it is a bunch of example scripts cobbled together. Do you
have an example of just how to convolve an image?

David

--%<
use strict;
use warnings;
use Image::Magick;

# The script reads three images, crops them, and writes a single image as a
GIF animation sequence
my($image,$p,$q);

$image = new Image::Magick;
$image->Read('Bugs_Bunny.svg.png');
$image->Read('ng-clipart-bugs-bunny-witch-hazel-drawing-looney-tunes-cartoon-bugs-and-lola-bunny.png');
$image->Read('k.miff[1, 5, 3]');
$image->Contrast();
for(my $x = 0; $image->[$x]; $x++){
print "$x\n";
$image->[$x]->Frame('100x200') if $image->[$x]->Get('magick') eq 'GIF';
}

$p = $image->[1];

print "$p\n";

$p->Convolve([1, 2, 1, 2, 4, 2, 1, 2, 1]);



On Fri, Feb 4, 2022 at 11:05 AM William Torrez Corea 
wrote:

> #!/usr/local/bin/perl
>> use Image::Magick;
>> use PDL::ImageND;
>>
>> # The script reads three images, crops them, and writes a single image as
>> a GIF animation sequence
>> my($image,$p,$q);
>>
>> $image = new Image::Magick;
>> $image->Read('Bugs_Bunny.svg.png');
>>
>> $image->Read('ng-clipart-bugs-bunny-witch-hazel-drawing-looney-tunes-cartoon-bugs-and-lola-bunny.png');
>> $image->Read('k.miff[1, 5, 3]');
>> $image->Contrast();
>> for($x = 0; $image->[$x]; $x++){
>> $image->[$x]->Frame('100x200') if $image->[$x]->Get('magick') eq
>> 'GIF';
>> undef $image->[$x] if $image->[$x]->Get('columns') < 100;
>> }
>>
>> $p = $image->[1];
>>
>> # Suppose you want to start out with a 100x100 pixel white canvas with a
>> red
>> $image = Image::Magick->new;
>> $image->Set(size=>'100x100');
>> $image->ReadImage('canvas:white');
>> $image->Set('pixel[49,49]'=>'red');
>>
>> # Here we reduce the intensity of the red component at (1,1) by half:
>> @pixels = $image->GetPixel(x=>1,y=>1);
>> $pixels[0]*=0.5;
>> $image->SetPixel(x=>1,y=>1,color=>\@pixels);
>>
>> # Or suppose you want to convert your color image to grayscale
>> $image->Quantize(colorspace=>'gray');
>>
>> # Let's annotate an image with a taipai truetype font
>> $text = 'Works like magick!';
>> $image->Annotate(font=>'kai.ttf',
>> pointsize=>40,fill=>'green',text=>$text);
>>
>> # Perhaps you want to extract all the pixel intensities from an image and
>> write them to STDOUT
>> @pixels = $image->GetPixels(map=>'I',
>> height=>$height,width=>$width,normalize=>true);
>> binmode STDOUT;
>> print pack('B*',join('',@pixels));
>>
>> # Other clever things you can do with a PerlMagick objects include
>> $i = "$#$p+1";   # return the number of images associated with object p
>> push(@$q, @$p); # push the images from object p onto object q
>> @$p = (); #delete the images but not the object p
>> $p->Convolve([1, 2, 1, 2, 4, 2, 1, 2, 1]);
>>
>
> On Fri, Feb 4, 2022 at 9:41 AM David Mertens 
> wrote:
>
>> The issue isn't having the method, the issue is calling it on an
>> unblessed reference. Can you show the code that gives this problem?
>>
>> Btw, my bet is that it's not a matter of having PDL's method. You would
>> use PDL for number crunching, not gif animation. :-)
>>
>> David
>>
>> On Fri, Feb 4, 2022, 10:14 AM William Torrez Corea 
>> wrote:
>>
>>> Ready, install the method from https://metacpan.org/pod/PDL::ImageND.
>>>
>>> $p->Convolve([1, 2, 1, 2, 4, 2, 1, 2, 1]);
>>>>
>>>
>>>
>>> According to the function convolve this declaration is of the following
>>> way
>>>
>>>> $new = convolve $x, $kernel
>>>

Re: Can't call method "Convolve" on unblessed reference at gifaninmation.pl line 47.

2022-02-04 Thread David Mertens
The issue isn't having the method, the issue is calling it on an unblessed
reference. Can you show the code that gives this problem?

Btw, my bet is that it's not a matter of having PDL's method. You would use
PDL for number crunching, not gif animation. :-)

David

On Fri, Feb 4, 2022, 10:14 AM William Torrez Corea 
wrote:

> Ready, install the method from https://metacpan.org/pod/PDL::ImageND.
>
> $p->Convolve([1, 2, 1, 2, 4, 2, 1, 2, 1]);
>>
>
>
> According to the function convolve this declaration is of the following
> way
>
>> $new = convolve $x, $kernel
>>
>> But when I declare the function in my code the result is wrong.
>
> Can't call method "Convolve" on unblessed reference at gifaninmation.pl
>> line 47.
>>
>
> I don't know in what part I am confused.
>
>
>
>
>
>
> On Fri, Feb 4, 2022 at 8:34 AM Andrew Solomon  wrote:
>
>> Is this what you're looking for?
>>
>> https://metacpan.org/pod/PDL::ImageND
>>
>>
>> On Fri, Feb 4, 2022 at 2:30 PM William Torrez Corea <
>> willitc9...@gmail.com> wrote:
>>
>>> I'm using cpanm trying to find a method that contains the function
>>> convolve but I only found the method Imager.
>>>
>>> https://metacpan.org/pod/Imager
>>>
>>> When I try to declare the method and use it in the code, the result is
>>> always wrong.
>>>
>>> I am using the following libraries:
>>>



 *#!/usr/local/bin/perl use Image::Magick;use Imager;*
>>>
>>>
>>>
>>>
>>> --
>>>
>>> With kindest regards, William.
>>>
>>> ⢀⣴⠾⠻⢶⣦⠀
>>> ⣾⠁⢠⠒⠀⣿⡁ Debian - The universal operating system
>>> ⢿⡄⠘⠷⠚⠋⠀ https://www.debian.org
>>> ⠈⠳⣄
>>>
>>>
>>
>> --
>> Andrew Solomon
>> Director, Geekuni 
>> P: +44 7931 946 062
>> E: and...@geekuni.com
>>
>
>
> --
>
> With kindest regards, William.
>
> ⢀⣴⠾⠻⢶⣦⠀
> ⣾⠁⢠⠒⠀⣿⡁ Debian - The universal operating system
> ⢿⡄⠘⠷⠚⠋⠀ https://www.debian.org
> ⠈⠳⣄
>
>


Re: covert perl code to binary

2019-01-11 Thread David Mertens
How dumb is your "nobody"? Would Acme::Bleach do the trick? Or something
similar?

:)

On Fri, Jan 11, 2019, 5:01 AM Uday Vernekar  Hi all,
>
> I have a perl code which I need to covert to binary so that nobody can see
> the code.
>
> we used pp package to make the perl code binary but here the user can see
> the code which gets created in tmp.
>
> Need help if anybody knows kindly reply
>
> With regards
> Uday V G
>


Re: "Information station" using Perl and Raspberry Pi

2017-07-25 Thread David Mertens
Hello Frank,

I think I can speak with some minor authority on this. I am a BeagleBone
Black guy, not a RPi guy, but in this situation I think my knowledge of one
is enough to inform the other.

The RPi (and BeagleBone Black) run full Linux OSes running on ARM. Perl
runs just fine on these; web servers run without trouble by extension; and
more interestingly, the X windowing system and GUI layers (like Gnome,
Mate, KDE, etc) run just fine, too. These are fully functional computers,
powered by ARM processors. As such, they would do just fine creating a
display if you can plug the monitor into the computer. BeagleBone has
pre-compiled Debian and Ubuntu images; I'm sure RPi does, too.

RPi is supposed to be better than BeagleBone for multimedia because it has
more and varied ports, but the BBB has HDMI output, too. I don't do
mutlimedia with my BBs so I don't know or care. If you can plug your
mother's TV into a DVI input, or get a converter, then you could simply use
the TV as a monitor. Then you could write Tk or Prima applications to
achieve what you want. Language level support (Python, Javascript, or
otherwise) need not have any bearing on your decision here.

BTW, the big differences that I know of between the BB and RPi are:

   - BB is fully open-source, including board layout; RPi is not because of
   Broadcom (I believe) licensing agreements.
   - RPi's Broadcom chipset is more multimedia friendly, and it has two USB
   ports to the BB's one.
   - BB is built on TI's Sitara chip. This chip includes two
   microcontrollers *within the main CPU silicon*, making it all-around
   much better for industrial and real-time applications. It's like having two
   Arduinos baked into the chip. This should have no bearing on your decision,
   but was the deciding factor for me picking BB.

Good luck! Feel free to reach out either on this list or directly if you
have questions!

David

On Tue, Jul 25, 2017 at 7:53 PM, SSC_perl  wrote:

> On Jul 25, 2017, at 2:08 PM, Andrew Solomon  wrote:
>
> In case you want to see what's already out there, the search engine term
> for you is "assistive technology".
>
>
> Thanks, Andrew!  I appreciate it.  However, I think that's more than what
> I need.  I'm really only looking to display formatted text on a TV screen.
>
> ---
>
> I finally found a project that looks similar to what I want to do:
>
> https://www.cnet.com/how-to/turn-an-old-monitor-into-a-
> wall-display-with-a-raspberry-pi/
>
> It's a lot more elaborate than what I need and it uses a web service to
> display the information.  I'd prefer to use Perl, if at all possible, and
> use my own server for the files.
>
> Frank
>



-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


Re: debug perl package

2017-07-17 Thread David Mertens
If you really want a GUI debugger you might consider Padre. I've used it as
a debugger once or twice, but recall running into issues. Note that Padre
has been a stale project for a while, so it may not even install.

A safe bet, if non-gui, is the perl debugger, as already mentioned.

David

On Mon, Jul 17, 2017 at 4:39 PM, Andrew Solomon  wrote:

> There's also a very nice tutorial here http://techblog.net-a-
> porter.com/2014/03/learning-the-perl-debugger-introduction/
>
> On Mon, Jul 17, 2017 at 8:54 PM, Chas. Owens  wrote:
>
>> Perl has a built in debugger.  You can say
>>
>> perl -d abc.pl
>>
>> And it will stop at the first executable line (ignoring BEGIN blocks and
>> use statements).  You can then step through or over the code.  See
>> https://perldoc.perl.org/perldebug.html or perldoc perldebug for more
>> information.
>>
>> On Mon, Jul 17, 2017 at 3:48 PM Asad  wrote:
>>
>>> Hi All ,
>>>
>>>   I am new to perl , I a have a abc.pl script and abc.pm module
>>> . I want to understand when I execute abc.pl hw to get to a debug state
>>> to identify what values does it take . Any GUI interface available to see
>>> the flow of events.
>>>
>>>
>>> --
>>> Asad Hasan
>>> +91 9582111698 <+91%2095821%2011698>
>>>
>>
>
>
> --
> Andrew Solomon
>
> Mentor@Geekuni http://geekuni.com/
> http://www.linkedin.com/in/asolomon
>



-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


Re: Filehandle within foreach loop

2017-07-16 Thread David Mertens
Yes, and I think that gives us *two* reasons to always explicitly close
filehandles. :-)

David

On Sun, Jul 16, 2017 at 8:00 AM, Shawn H Corey 
wrote:

> On Sun, 16 Jul 2017 07:36:39 -0400
> David Mertens  wrote:
>
> > Also note that lexical filehandles close when they go out of scope
>
> True but you should always explicitly close your files. This gives you
> a chance to report any errors it had, have rather than silently
> ignoring them.
>
>
> --
> Don't stop where the ink does.
>
> Shawn H Corey
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


Re: Filehandle within foreach loop

2017-07-16 Thread David Mertens
Even more readable:

FILE: foreach my $file ( @files ) }
  ...
  last FILE if (some_condition);
  ...
}

Also note that lexical filehandles close when they go out of scope, except
for the most recently "stat"ed file. Perl holds a reference to "the most
recently stat-ed filehandle" in "the solitary underscore" as discussed in the
documentation about -X . The
upshot is that a filehandle used in an expression like "-M $fh" will live
until the next such expression, and the filehandle used in the last such
expression won't close until the end of the program.

David

On Wed, Jul 12, 2017 at 5:28 PM, Jim Gibson  wrote:

> If you wish to terminate execution of a foreach loop without iterating
> over all of the elements (@files, in this case) use the “last” statement:
>
> foreach my $file ( @files ) {
>  # process file
>  open( my $fh, ‘<‘, $file ) or die(…);
>  while( my $line = <$fh> ) {
># process line
>  }
>  close ($fh) or die( … );
>
>  last if (some_condition);
> }
>
> If you wish to terminate the foreach loop from inside the while loop, you
> can give the foreach loop a label and use the label in the “last”
> statement. Without an explicit label, “last” will terminate the innermost
> loop (the while loop here):
>
> ALL: foreach my $file ( @files ) {
>  # process file
>  open( my $fh, ‘<‘, $file ) or die(…);
>  while( my $line = <$fh> ) {
># process line
>last ALL if (some_condition);
>  }
>  close ($fh) or die( … );
> }
>
> However, in that case you have skipped the close statement, and the close
> will happen automatically when the file handle $fh goes out of scope, but
> you cannot do any explicit error checking on the close.
>
>
> > On Jul 12, 2017, at 12:20 PM, perl kamal  wrote:
> >
> > Hello All,
> >
> > I would like to read multiple files and process them.But we could read
> the first file alone and the rest are skipped from the while loop. Please
> correct me where am i missing the logic.Thanks.
> >
> > use strict;
> > use warnings;
> > my @files=qw(Alpha.txt Beta.txt Gama.txt);
> >
> > foreach my $file (@files)
> > {
> > open (my $FH, $file) or die "could not open file\n";
> >
> > while( my $line = <$FH> ){
> > #[process the lines & hash construction.]
> > }
> > close $FH;
> > }
> >
> > Regards,
> > Kamal.
>
>
>
> Jim Gibson
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


Re: perl -e 'my $i = 0; $i = defined($i) ? (!!$i) : 0; print "i: $i\n";'

2017-07-07 Thread David Mertens
On Thu, Jul 6, 2017 at 11:05 PM,  wrote:

> Perl is highly unusual in that the operator, not the operand, dictates the
>> context.
>>
>
> Good point - and one that I hadn't got around to noticing.
>
> Therefore, the '!' operator has to be set up to either:
> a) operate always in numeric context;
> or
> b) operate always in string context;
> or
> c) operate always in both contexts (as per the current behaviour).
>
> Having an ambivalent '!' operator (where it  alternates between a) and b),
> according to the operand's flags) is therefore not an option.
>
> If we wanted an operator for "logical string negation" and an operator for
> "logical numeric negation" we would need 2 different operators.
>
> Have I got that  somewhere near right ?
>
> Cheers,
> Rob
>

Yes, I think so. Of course, that was one of Larry's design decisions, and I
think it was a good one. If you really care to have operand-dependent
behavior, you can create a class that overloads the operator and carry your
data around in instances of that class.

Also, boolean works beyond simple string and numeric context. In
particular, boolean context coerces undefined values to false without
issuing warnings, and it's not clear how an undefined value would operate
under "logical string negation" and "logical numeric negation".

David

-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


Re: perl -e 'my $i = 0; $i = defined($i) ? (!!$i) : 0; print "i: $i\n";'

2017-07-06 Thread David Mertens
On Thu, Jul 6, 2017 at 9:12 PM,  wrote:

> I find it a little surprising that use of the '!' operator is all that's
> needed to add the stringification stuff:
>
> ...
>
> If the '!' operator didn't do that, then I believe the OP would be seeing
> precisely what he expects.
>
> So ... why should the '!' operator *not* respect the string/numeric
> context of the operand ?
>

Perl is highly unusual in that the *operator*, not the operand, dictates
the context. We know tha

  $foo eq $bar

forces $foo and $bar into string context and then compares them as strings,
and

  $foo == $bar

forces numeric context. I personally find this to be a hallmark of Perl, a
way in which it Does What I Mean, where what I mean is indicated by the
operator that I chose to use.

Assignment like

  $result = !$i

does not provide a context, but the negation operator does---boolean---and
so we get a result that is boolean true or false, both of which are
represented by special kinds of dual-vars, as was already explained.

David

-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


Re: Module to extract patterns

2017-06-13 Thread David Mertens
See also Text::Balanced and its extract_quotelike:

http://search.cpan.org/~shay/Text-Balanced-2.03/lib/Text/Balanced.pm#extract_quotelike

David

On Tue, Jun 13, 2017 at 7:23 AM, Paul Johnson  wrote:

> On Tue, Jun 13, 2017 at 02:03:10PM +0300, Lars Noodén wrote:
> > There are a lot of ways to write patterns.
>
> > Is there a module or method that can identify and extract them from a
> > larger bit of code regardless of style?
>
> That's not an easy problem.  Fortunately, there is a module which will
> probably do what you want: PPI.  See https://metacpan.org/pod/PPI
>
> --
> Paul Johnson - p...@pjcj.net
> http://www.pjcj.net
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


Re: file type

2016-08-24 Thread David Mertens
I don't know if I add much to the conversation by simply referring you to
Stack Overflow, but I've found that it's a decent repository of information
for this sort of question. This question has been asked and answered before
,
and the answer suggests MIME::Types, but also suggests some content
introspection, for which File::Type and File::MimeInfo::Magic get a mention.

I have never used any of these, so I cannot vouch for them myself.

Good luck!
David

On Tue, Aug 23, 2016 at 12:41 PM, hw  wrote:

>
> Hi,
>
> what´s the best way to find out of what type a file is?
>
> I´ll probably be having a list of available files and another
> list of the types I´ll be interested in, and it needs to be
> determined which files in the list of available files need to
> be processed further and which ones are to be ignored.  This
> is to store references to files in a database with some
> additional information, like the type of the file that is
> being referenced.
>
> File::MimeInfo looks most promising, but maybe there´s a
> better way?
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


Re: Perl regular expression for implementing infix expression parser?

2016-08-14 Thread David Mertens
Hello siegried,

This is a fun question! In fact, v5.10 introduced "recursive subpatterns"
into the regex lexicon, so using v5.10 or later, it is possible to
implement (rather arcane) recursive descent regexes without resorting to
eval-based tricks like what you have.

But before diving in, a few questions are in order to make sure we're
working on the right problem. Are you looking for something to cram into a
one-liner? (Do you have a character limit for your one-liner?) Are you open
to solving this with a combination of for-loops and regexes, or do you want
a purely regex-based approach? Are you trying to write something that
merely validates that an expression as valid mathematics, or do you want to
build an abstract syntax tree? Are you interested in using modules from
CPAN or rolling your own? Finally, if your answer to all of these is, "I'm
just curious!", then you can expect to get a very wide range of answers,
not just regex-based.

Thanks! Looking forward to the fun!
David

On Sat, Aug 13, 2016 at 3:45 PM, Richard Heintze via beginners <
beginners@perl.org> wrote:

> I this perl one liner for evaluating infix expressions in VI and emacs/VI
> emulator mode:
>
>
>
> .! perl -MPOSIX   -pe ' BEGIN{ $np = qr{ \( (?: (?> [^()]+ ) | (??{ $np })
> )* \) }x;$funpat = qr/$np/;} s/($funpat)=($funpat|(")[^\"]*
> (")|[0-9\.]+)/"$1=$3".eval($1)."$4"/ge'
>
> That $np is from the camel book and it is a regular expression that parses
> nested sets of parentheses and then my replace command evaluates the
> arithmetic expression.
>
> Since perl accommodates recursive regular expressions, it ought to be
> possible to implement a recursive decent parser.
>
> Can someone help me enhance the above code so that instead of just blindly
> looking for balanced parenthesis, the regular expression will recognize
> (match) this:
>
> 5+8*(2+8/(3+2))*(2+22/3)=()
> Thanks
> siegfried
>
>
>


-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


Re: Interpolation Problem

2016-07-17 Thread David Mertens
To second what Uri mentioned already,

On Fri, Jul 15, 2016 at 11:42 PM, AC P  wrote:

> 
>
> Argument "$command" isn't numeric in subtraction (-) at then it gives me
> my directory.
>
> 
>

This error message (I suspect it was a warning, can you check?) probably
reported a line number. Could you post the code in the vicinity of that
line number? (Better yet, post your whole script, or enough of a script to
reproduce the problem.)

Thanks!
David

-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan