[33141] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 4420 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Apr 24 05:17:24 2015

Date: Fri, 24 Apr 2015 02:17:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 24 Apr 2015     Volume: 11 Number: 4420

Today's topics:
    Re: Backticks status in scalar context: bug or missunde (Tim McDaniel)
    Re: Backticks status in scalar context: bug or missunde <ams@ludd.ltu.se>
    Re: format file sharma__r@hotmail.com
        Get number of lines by page on ODF Document dahalpi@gmail.com
    Re: Get number of lines by page on ODF Document <gravitalsun@hotmail.foo>
        How do I use "when" to check for "one of two options"? <see.my.sig@for.my.address>
    Re: How do I use "when" to check for "one of two option <rweikusat@mobileactivedefense.com>
    Re: How do I use "when" to check for "one of two option <see.my.sig@for.my.address>
    Re: How do I use "when" to check for "one of two option <jurgenex@hotmail.com>
    Re: How do I use "when" to check for "one of two option <rweikusat@mobileactivedefense.com>
    Re: How do I use "when" to check for "one of two option <see.my.sig@for.my.address>
    Re: How do I use "when" to check for "one of two option <gamo@telecable.es>
    Re: How do I use "when" to check for "one of two option <rweikusat@mobileactivedefense.com>
    Re: How do I use "when" to check for "one of two option <jfeuerst@eecs.tufts.edu>
    Re: How do I use "when" to check for "one of two option <gamo@telecable.es>
    Re: How do I use "when" to check for "one of two option <gamo@telecable.es>
    Re: How do I use "when" to check for "one of two option <gamo@telecable.es>
    Re: Mentifex Strong AI Perlmind Programming Journal: 20 <see.my.sig@for.my.address>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Wed, 22 Apr 2015 19:35:59 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: Backticks status in scalar context: bug or missunderstanding
Message-Id: <mh8t6u$2nq$1@reader1.panix.com>

In article <mfjhoa$5to$1@speranza.aioe.org>,
Martin Str|mberg  <ams@luminous.ludd.ltu.se> wrote:
>I dare say, anyone reading this will assume the "undef if the command
>failed" will assume what I did and that `/bin/false` will return
>undef.

Reading it, I don't assume any such thing.  It would be useless and
stupid if Perl ran the command to be executed, perhaps with lots of
output, and then decided to throw away the output simply because it
happened to exit with a non-zero exit code.  That's because

- there are all sorts of cases of commands that may output useful data
  and even succeed in a sense, but happen to exit non-zero.  For
  example, diff where the files differ.

- There'd be no way to capture the output of a "failed" command in
  that sense, so no way to get any error messages -- even
  `whatever_command 2>&1` would toss out the error.

- The original place where Perl stole the idea, the shell `...`,
  doesn't throw away output.

That said, I agree that it could and should be expressed more
precisely:

"The collected standard output of the command is returned. ... In
scalar context, it comes back as a single string, or undef only if the
command does not exist and it is executed directly instead of via your
system's command shell."

and maybe copy the text from perlfunc on exec explaining about shell
metacharacters.

-- 
Tim McDaniel, tmcd@panix.com




------------------------------

Date: Thu, 23 Apr 2015 07:10:49 +0000 (UTC)
From: Martin Str|mberg <ams@ludd.ltu.se>
Subject: Re: Backticks status in scalar context: bug or missunderstanding
Message-Id: <mha5tn$m6u$1@speranza.aioe.org>

Tim McDaniel <tmcd@panix.com> wrote:
> That said, I agree that it could and should be expressed more
> precisely:

> "The collected standard output of the command is returned. ... In
> scalar context, it comes back as a single string, or undef only if the
> command does not exist and it is executed directly instead of via your
> system's command shell."

> and maybe copy the text from perlfunc on exec explaining about shell
> metacharacters.


Also some text refering to $@ would perhaps triggered a rethink about
undef. Something like last in the first paragraph (on qx/STRING/ in
perlop):

To get the exit status of the command, check the variable $@.


As far as I can see there is no reference to that variable at all in
that text.


-- 
MartinS


------------------------------

Date: Thu, 23 Apr 2015 02:59:40 -0700 (PDT)
From: sharma__r@hotmail.com
Subject: Re: format file
Message-Id: <c7aa0dce-67bf-4c74-b2fd-b4d26167bd92@googlegroups.com>

On Tuesday, 21 April 2015 03:55:22 UTC+5:30, lanc...@gmail.com  wrote:
> At this point if I wanted the format to be like below. Am I looking at a perl module? TEXT::CVS ? Thanks
> 
>          name name name
> time value value value
> time value value value
> time
> time

Since you are changing your output formats for your input, you can use the
"tbl" utility from unix to first architect the desired output format in a tbl
recipe file & then supply the appropriate data thru perl to it. Ofc, all this
is done from within perl.

There's a bunch of input-validation code in here, which is to avoid sputtering
in the main processing code. YMMV.

Also it wasn't very clear from the data you provided as to what is the time
and what is the value in the fields. I have assumed the 1st field as time
& the 2nd as value.

# %h = (
# signal1 => { t1 => v1, t2 => v2, ..., tN => vN },
# );

##############################################################

cat - <<__DATA__ |
#value,time, signal
12,1427766557, bob
22,1427762457, bill
53,1427769753, bob
11,1111111111, Joe
24,3333333334, Nansy
12,2222222222, Joe
22,3333333331, Nansy
23,3333333332, Nansy
24,3333333335, Nansy
25,3333333336, Nansy
__DATA__
perl -Mstrict -Mwarnings -F'/\s*,\s*/' -lane '

BEGIN{
   $::FLAG = 0;
   $::KNT = 0;
   %::EXIT_STATUS_FOR = (
      BAD_DATA     => 63,
      BAD_INP_LINE => 65,
      EMPTY_FILE   => 67,
   );
}

next if /^\s*#/;

unless (@F == 3) {
   print STDERR "[ERROR] Invalid line at number $..";
   print STDERR "A valid line has exactly 3 CSV fields.";
   $::FLAG = $::EXIT_STATUS_FOR{ BAD_INP_LINE };
   exit 1;
}

++$::KNT;

#my($value, $timepoint, $signal) = @F;
my($timepoint, $value, $signal) = @F;

if ($timepoint =~ /\D/) {
   print STDERR "[ERROR] Invalid time specified \`$timepoint\" on line # $..";
   print STDERR "The time is to be an integer.";
   $::FLAG = 1;
   $::FLAG = $::EXIT_STATUS_FOR{ BAD_DATA };
   exit 1;
}

$::h{$signal}{$timepoint} = $value;
if ($value =~ /\D/) {
   print STDERR "[ERROR] Invalid value specified \`$value\" on line # $..";
   print STDERR "The value is to be an integer.";
   $::FLAG = $::EXIT_STATUS_FOR{ BAD_DATA };
   exit 1;
}

END{
   exit $::FLAG
     if $::FLAG != 0;

   if ($::KNT == 0) {
      print STDERR "[ERROR] No valid line found. Hence nothing outputted.";
      print STDERR "A valid line has exactly 3 CSV fields.";
      $::FLAG = $::EXIT_STATUS_FOR{ EMPTY_FILE };
      exit $::FLAG;
   }

   my $NULL = q{};

   sub gen_tbl {
      my @time_sorted = @{ +shift };

      open my $fh, "| tbl - | groff -T ascii -mm | less -r" or
         die "Cannot open pipe for writing: $!";

      print {$fh} ".TS";
      print {$fh} ".allbox;";
      print {$fh} "c " x (0+keys %::h) . "c";
      print {$fh} "r " . "n " x (-1+keys %::h) . "n.";
      print {$fh} join("\t", q{timeslot}, keys %::h);

      for my $timepoint ( @time_sorted ) {
         print {$fh}
            join("\t", $timepoint,
               map {
                  my $signal = $_;
                  (exists $::h{$signal}{$timepoint})
                        ? $::h{$signal}{$timepoint}
                        : $NULL
                        ;
               } keys %::h
            );
      }

      print {$fh} ".TE";

      close $fh or
         die "Could not close the pipe after writing: $!";
   }

   gen_tbl([
      sort { $a <=> $b }
      keys %{{
         map { $_ => 1 }
         map { keys %{$::h{$_}} }
         keys %::h
      }}
   ]);
}
'
######################################################################


------------------------------

Date: Thu, 23 Apr 2015 09:08:04 -0700 (PDT)
From: dahalpi@gmail.com
Subject: Get number of lines by page on ODF Document
Message-Id: <f2f450f1-0bbc-4a6e-878b-2f170225789b@googlegroups.com>

Hello
I'm newbie on perl and its modules but i need to modify an ODF document with some content.
I have a table with data and i have to complete with '' (nulls) rows at the end of page.
So i can add content with OpenOffice::OODoc but i need help to obtain the avalaible number of lines on page to fill.
I put a flag 'XNULLX' on the last row to find with perl and replace with the lines but i don't know yet how to get the value
Thanks! 


------------------------------

Date: Thu, 23 Apr 2015 20:48:56 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: Get number of lines by page on ODF Document
Message-Id: <mhbbac$ug0$1@news.grnet.gr>

On 23/4/2015 19:08, dahalpi@gmail.com wrote:
> Hello
> I'm newbie on perl and its modules but i need to modify an ODF document with some content.
> I have a table with data and i have to complete with '' (nulls) rows at the end of page.
> So i can add content with OpenOffice::OODoc but i need help to obtain the avalaible number of lines on page to fill.
> I put a flag 'XNULLX' on the last row to find with perl and replace with the lines but i don't know yet how to get the value
> Thanks!
>


unzip the ODF
parse xml file holding your table with easy xml
thats all



------------------------------

Date: Wed, 22 Apr 2015 08:09:15 -0700
From: Robbie Hatley <see.my.sig@for.my.address>
Subject: How do I use "when" to check for "one of two options"?
Message-Id: <RradnRaIpIcFJKrInZ2dnUVZ57ydnZ2d@giganews.com>


The following code from a file-renaming script I'm writing
is broken:

GETCHAR: my $c = get_character;
given ($c) {
    when ('a' || 'A') {RenameFile($OldName, $NewName);$Settings{Mode} = 'Y';}
    when ('y' || 'Y') {RenameFile($OldName, $NewName);}
    when ('n' || 'N') {next FILE;}
    when ('q' || 'Q') {exit 0;}
    default {
       say 'Invalid keystroke!';
       say 'Press \'y\' to rename, \'n\' to skip, \'q\' to quit, or \'a\' to rename all.';
       goto GETCHAR;
    }
}

It correctly responds to the lower-case letters but ignores upper-case.
Apparently the "when ('a' || 'A') {do_stuff;}" construct is wrong.
Actually, it looked wrong to me from the beginning and I was expecting
it to give a syntax error. But it doesn't give any error or warning;
it just fails to work right. So, what's the correct way of doing this?

I suppose I could do *this*:

    when (/^[yY]$/) {RenameFile($OldName, $NewName);}

Or, I could do *this*:

    when ($c eq 'y' || $c eq 'Y') {RenameFile($OldName, $NewName);}

That last one sux because if I'm going to do that, why not just use
if/elsif/elsif/else ?

Seems to me there should be a more general way of doing:

    given (something)
       when (one_thing or the_other) {then_do_this();}
    }


-- 
Cheers,
Robbie Hatley
Midway City, CA, USA
perl -le 'print "\154o\156e\167o\154f\100w\145ll\56c\157m"'
http://www.well.com/user/lonewolf/
https://www.facebook.com/robbie.hatley


------------------------------

Date: Wed, 22 Apr 2015 16:51:00 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: How do I use "when" to check for "one of two options"?
Message-Id: <87wq14b1qz.fsf@doppelsaurus.mobileactivedefense.com>

Robbie Hatley <see.my.sig@for.my.address> writes:
> The following code from a file-renaming script I'm writing
> is broken:
>
> GETCHAR: my $c = get_character;
> given ($c) {
>    when ('a' || 'A') {RenameFile($OldName, $NewName);$Settings{Mode} = 'Y';}
>    when ('y' || 'Y') {RenameFile($OldName, $NewName);}
>    when ('n' || 'N') {next FILE;}
>    when ('q' || 'Q') {exit 0;}
>    default {
>       say 'Invalid keystroke!';
>       say 'Press \'y\' to rename, \'n\' to skip, \'q\' to quit, or \'a\' to rename all.';
>       goto GETCHAR;
>    }
> }
>
> It correctly responds to the lower-case letters but ignores
> upper-case.

That's something I also stumbled over a while ago: The code you wrote
compares the 'given' value with the value of the expression 'a' || 'A'
which is 'a', cf

------------
use feature 'switch';

sub checkit
{
    given ($_[0]) {
	when (0 || 'b') {
	    print("It's $_\n");
	}

	default {
	    print("Don't know what to make of $_\n");
	}
    }
}

checkit(0);
checkit('b');
------------

An anonymous array of values to compare against has to be used instead
to make this work as intended:

------------
use feature 'switch';

sub checkit
{
    given ($_[0]) {
	when ([0, 'b']) {
	    print("It's $_\n");
	}

	default {
	    print("Don't know what to make of $_\n");
	}
    }
}

checkit(0);
checkit('b');
------------

But at least, this demonstrates that the smart matching code is smarter
than the people trying to use it :->


------------------------------

Date: Wed, 22 Apr 2015 10:02:12 -0700
From: Robbie Hatley <see.my.sig@for.my.address>
Subject: Re: How do I use "when" to check for "one of two options"?
Message-Id: <__2dnfOM7PWMSarInZ2dnUVZ57ydnZ2d@giganews.com>


On 4/22/2015 8:51 AM, Rainer Weikusat wrote:

> Robbie Hatley <see.my.sig@for.my.address> writes:
>> The following code from a file-renaming script I'm writing
>> is broken:
>>
>> GETCHAR: my $c = get_character;
>> given ($c) {
>>     when ('a' || 'A') {RenameFile($OldName, $NewName);$Settings{Mode} = 'Y';}
>>     when ('y' || 'Y') {RenameFile($OldName, $NewName);}
>>     when ('n' || 'N') {next FILE;}
>>     when ('q' || 'Q') {exit 0;}
>>     default {
>>        say 'Invalid keystroke!';
>>        say 'Press \'y\' to rename, \'n\' to skip, \'q\' to quit, or \'a\' to rename all.';
>>        goto GETCHAR;
>>     }
>> }
>>
>> It correctly responds to the lower-case letters but ignores
>> upper-case.
>
> ...The code you wrote compares the 'given' value with the value of
> the expression 'a' || 'A' which is 'a', cf ...

Not quite what I had in mind.

> An anonymous array of values to compare against has to be used instead
> to make this work as intended:
>
> ------------
> use feature 'switch';
>
> sub checkit
> {
>      given ($_[0]) {
> 	when ([0, 'b']) {
> 	    print("It's $_\n");
> 	}
>
> 	default {
> 	    print("Don't know what to make of $_\n");
> 	}
>      }
> }
>
> checkit(0);
> checkit('b');
> ------------

Ah. Thanks for the tip!

I could do something like THIS, then:

GETCHAR: my $c = get_character;
given ($c) {
    when (['a','A']) {RenameFile($OldName, $NewName);$Settings{Mode} = 'Y';}
    when (['y','Y']) {RenameFile($OldName, $NewName);}
    when (['n','N']) {next FILE;}
    when (['q','Q']) {exit 0;}
    default {
       say 'Invalid keystroke!';
       say 'Press \'y\' to rename, \'n\' to skip, \'q\' to quit, or \'a\' to rename all.';
       goto GETCHAR;
    }
}

> But at least, this demonstrates that the smart matching code is smarter
> than the people trying to use it :->

The rules of smart-matching give me a headache when I read them.
In The Camel Book, the author kinda realizes that; after writing
two pages of complicated, arbitrary rules, he writes something
along the lines of "...I realize that this all seems a bit
complicated, but..."  Well, yes, it does. I think it would have
been a better feature if it had been made simpler.

On the other hand, given/when smart-matching does seem to usually
"just do the right thing".  Except when it doesn't.


-- 
Cheers,
Robbie Hatley
Midway City, CA, USA
perl -le 'print "\154o\156e\167o\154f\100w\145ll\56c\157m"'
http://www.well.com/user/lonewolf/
https://www.facebook.com/robbie.hatley


------------------------------

Date: Wed, 22 Apr 2015 10:12:52 -0700
From: J黵gen Exner <jurgenex@hotmail.com>
Subject: Re: How do I use "when" to check for "one of two options"?
Message-Id: <sblfjaddeisrg0lvf9ccqka5fbcvm7gqp6@4ax.com>

Robbie Hatley <see.my.sig@for.my.address> wrote:
>The following code from a file-renaming script I'm writing
>is broken:

It is always so much easier to work with normalized data wherever
possible.

>GETCHAR: my $c = get_character;

$normalized_c = lc $c; 

>given ($c) {
given ($normalized_c){
>    when ('a' || 'A') {RenameFile($OldName, $NewName);$Settings{Mode} = 'Y';}
when ('a') {...}
>    when ('y' || 'Y') {RenameFile($OldName, $NewName);}
when ('y'){...}

 .... and so on.

jue


------------------------------

Date: Wed, 22 Apr 2015 21:48:51 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: How do I use "when" to check for "one of two options"?
Message-Id: <87oamfc2j0.fsf@doppelsaurus.mobileactivedefense.com>

Robbie Hatley <see.my.sig@for.my.address> writes:
> On 4/22/2015 8:51 AM, Rainer Weikusat wrote:
>> Robbie Hatley <see.my.sig@for.my.address> writes:
>>> The following code from a file-renaming script I'm writing
>>> is broken:
>>>
>>> GETCHAR: my $c = get_character;
>>> given ($c) {
>>>     when ('a' || 'A') {RenameFile($OldName, $NewName);$Settings{Mode} = 'Y';}

[...]

>> ...The code you wrote compares the 'given' value with the value of
>> the expression 'a' || 'A' which is 'a', cf ...
>
> Not quite what I had in mind.

[...]

>> But at least, this demonstrates that the smart matching code is smarter
>> than the people trying to use it :->

[...]

> On the other hand, given/when smart-matching does seem to usually
> "just do the right thing".  Except when it doesn't.

It's fairly blameless in this case as 'the human' (which got confused by
all the smartness) demanded something which is positively impossible
(NB: I didn't really understand this until I saw you making the same
mistake I made earlier): 'a' || 'A' is a constant expression, as such,
it gets evaluated by the compiler and replaced by its result, meaning,
the smart matching code doesn't have the least bit of a chance of being
that smart.

Assuming the following somewhat simplified 'checkit'

----------
sub checkit
{
    given ($_[0]) {
        when ('a' || 'A') {
            print("It's $_\n");
        }
    }
}
----------

the corresponding disassembled 'internal perl code' (just for the when)
is

<|> enterwhen(other->a) K ->b
   <2> smartmatch sK*/2 ->9
   <0> padsv[$_:47,51] s ->7
   <$> const[PV "a"] s/SHORT ->8

ie, the compiler removed the  || 'A' parts as 'useless appendix'.


------------------------------

Date: Wed, 22 Apr 2015 15:50:04 -0700
From: Robbie Hatley <see.my.sig@for.my.address>
Subject: Re: How do I use "when" to check for "one of two options"?
Message-Id: <6pidnZ1fq4gEuKXInZ2dnUVZ57ydnZ2d@giganews.com>


On 4/22/2015 10:12 AM, jurgenex@hotmail.com wrote:

> $normalized_c = lc $c;

Ah! I never thought of that. Yes, would be simpler for me to say:

my $c = lc get_character;
given ($c) {
    when ('a') {action1;}
    when ('y') {action2;}
    when ('n') {action3;}
    when ('q') {action4;}
    default    {action5;}
}

But on the other hand, in this case, I think I prefer:

my $c = get_character;
given ($c) {
    when (['a','A']) {action1;}
    when (['y','Y']) {action2;}
    when (['n','N']) {action3;}
    when (['q','Q']) {action4;}
    default          {action5;}
}

because it's easier to see at a glance that both cases are accepted.


-- 
Cheers,
Robbie Hatley
Midway City, CA, USA
perl -le 'print "\154o\156e\167o\154f\100w\145ll\56c\157m"'
http://www.well.com/user/lonewolf/
https://www.facebook.com/robbie.hatley


------------------------------

Date: Thu, 23 Apr 2015 06:58:29 +0200
From: gamo <gamo@telecable.es>
Subject: Re: How do I use "when" to check for "one of two options"?
Message-Id: <mh9u5m$5kn$1@speranza.aioe.org>

El 23/04/15 a las 00:50, Robbie Hatley escribi贸:
>
> On 4/22/2015 10:12 AM, jurgenex@hotmail.com wrote:
>
>> $normalized_c = lc $c;
>
> Ah! I never thought of that. Yes, would be simpler for me to say:
>
> my $c = lc get_character;
> given ($c) {
>     when ('a') {action1;}
>     when ('y') {action2;}
>     when ('n') {action3;}
>     when ('q') {action4;}
>     default    {action5;}
> }
>
> But on the other hand, in this case, I think I prefer:
>
> my $c = get_character;
> given ($c) {
>     when (['a','A']) {action1;}
>     when (['y','Y']) {action2;}
>     when (['n','N']) {action3;}
>     when (['q','Q']) {action4;}
>     default          {action5;}
> }
>
> because it's easier to see at a glance that both cases are accepted.
>
>

Another possibility is to read 'when' as if was a mere 'if' so you
are dealing with the less experimental of the experimental feature.
Not likely a popular solution, but if you write

when (/a/i) {   }

I think that chances are that you have not to modify the syntax.

If you 'use 5.14;' another way of see it is 'use 5.014;' to put
the thing in wider context.


-- 
http://www.telecable.es/personales/gamo/
The generation of random numbers is too important to be left to chance


------------------------------

Date: Thu, 23 Apr 2015 12:22:24 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: How do I use "when" to check for "one of two options"?
Message-Id: <87383rw0lr.fsf@doppelsaurus.mobileactivedefense.com>

gamo <gamo@telecable.es> writes:
> El 23/04/15 a las 00:50, Robbie Hatley escribi贸:
>>
>> On 4/22/2015 10:12 AM, jurgenex@hotmail.com wrote:
>>
>>> $normalized_c = lc $c;
>>
>> Ah! I never thought of that. Yes, would be simpler for me to say:

[...]

>> But on the other hand, in this case, I think I prefer:
>>
>> my $c = get_character;
>> given ($c) {
>>     when (['a','A']) {action1;}

[...]

>> because it's easier to see at a glance that both cases are accepted.
>>
>
> Another possibility is to read 'when' as if was a mere 'if' so you
> are dealing with the less experimental of the experimental feature.
> Not likely a popular solution, but if you write
>
> when (/a/i) {   }
>
> I think that chances are that you have not to modify the syntax.

The sane way to implement a case-blind character comparison would be

given (lc($c)) {
     when ('a') {action1;}
     when ('y') {action2;}
     when ('n') {action3;}
     when ('q') {action4;}
     default    {action5;}
}
[or use a hash instead which is even faster --- despite being one of the
founding myths of Perl 6, 'function call overhead' simply isn't what it
used to be in the good, old times anymore ...[*]]

as this neither introduces a second variable nor requires a bunch of
regexes each of which might or might not have a certain flag. Being able
to compare the result/ value of an expression with different values is
one of the reasons why multi-way conditionals are useful. But this is
really all besides the point because that the comparison with 'more than
one option' can (and likely should) be avoided for this particular
example is an accidental property of the example.

BTW, a sensible table of comparison operations for when could look like
this:

           Scalar     undef     undefined                !defined $a
           Scalar     Regex     pattern match            $a =~ /$b/
           Scalar     Num       numeric equality         $a == $b
           Scalar     Any       string equality          $a eq $b
           
and

           Scalar     Array     match against an array element[3]

could be added as an obvious extension. A second, less obvious extension
would be

	   Scalar    function call expression		use return value

which would make the construct extensible as one could define a function

sub any_of
{
	my $it = $_;
        
        return 1 if $it ~~ $_ for @_;
        return;
}

OTOH, one can argue that this (and the previous one) again stray into
the territory of unobvious (to anyone but the inventor) semantic
extensions vaguely related to existing language constructs.

[*] As a recent example showed, recursing 15,000 levels deep can
outperform a flat multiplication of 15,000 numbers.

- flamebait ahead -

It's really not that difficult to create something useful provided one
can muster the moral courage to tell one bazillion twentysomething CS
students that

	a) nobody in the world is waiting for their weird ideas
        
        b) especially so since they're likely not even new, anyway


------------------------------

Date: Thu, 23 Apr 2015 13:42:33 +0000 (UTC)
From: Jozxyqk <jfeuerst@eecs.tufts.edu>
Subject: Re: How do I use "when" to check for "one of two options"?
Message-Id: <mhass5$j6o$1@dont-email.me>

How about just starting with

my $c = lc(get_character);



Robbie Hatley <see.my.sig@for.my.address> wrote:
> 
> The following code from a file-renaming script I'm writing
> is broken:
> 
> GETCHAR: my $c = get_character;
> given ($c) {
>    when ('a' || 'A') {RenameFile($OldName, $NewName);$Settings{Mode} = 'Y';}
>    when ('y' || 'Y') {RenameFile($OldName, $NewName);}
>    when ('n' || 'N') {next FILE;}
>    when ('q' || 'Q') {exit 0;}
>    default {
>       say 'Invalid keystroke!';
>       say 'Press \'y\' to rename, \'n\' to skip, \'q\' to quit, or \'a\' to rename all.';
>       goto GETCHAR;
>    }
> }
> 
> It correctly responds to the lower-case letters but ignores upper-case.
> Apparently the "when ('a' || 'A') {do_stuff;}" construct is wrong.
> Actually, it looked wrong to me from the beginning and I was expecting
> it to give a syntax error. But it doesn't give any error or warning;
> it just fails to work right. So, what's the correct way of doing this?
> 
> I suppose I could do *this*:
> 
>    when (/^[yY]$/) {RenameFile($OldName, $NewName);}
> 
> Or, I could do *this*:
> 
>    when ($c eq 'y' || $c eq 'Y') {RenameFile($OldName, $NewName);}
> 
> That last one sux because if I'm going to do that, why not just use
> if/elsif/elsif/else ?
> 
> Seems to me there should be a more general way of doing:
> 
>    given (something)
>       when (one_thing or the_other) {then_do_this();}
>    }
> 
> 


------------------------------

Date: Fri, 24 Apr 2015 09:25:26 +0200
From: gamo <gamo@telecable.es>
Subject: Re: How do I use "when" to check for "one of two options"?
Message-Id: <mhcr5d$7s9$1@speranza.aioe.org>

El 23/04/15 a las 13:22, Rainer Weikusat escribi贸:
> [*] As a recent example showed, recursing 15,000 levels deep can
> outperform a flat multiplication of 15,000 numbers.

You doesn't understand the difference between having a factorial
to calculate various factorials than one-shot computation.

Do you?

I think not, as you experiment with factorials without my
reading my record in time script to computate the 10000!

-- 
http://www.telecable.es/personales/gamo/
The generation of random numbers is too important to be left to chance


------------------------------

Date: Fri, 24 Apr 2015 09:36:54 +0200
From: gamo <gamo@telecable.es>
Subject: Re: How do I use "when" to check for "one of two options"?
Message-Id: <mhcrql$9j3$1@speranza.aioe.org>

El 23/04/15 a las 13:22, Rainer Weikusat escribi贸:
> - flamebait ahead -
>
> It's really not that difficult to create something useful provided one
> can muster the moral courage to tell one bazillion twentysomething CS
> students that
>
> 	a) nobody in the world is waiting for their weird ideas
>
>          b) especially so since they're likely not even new, anyway

Do you think a person who wrote trivial solutions,
to trivial problems are "idiot" CS students?

BTW, if you do

	my $c = lc (Record_key());
	given ($c) {
	}

you limit your alphabet from 52 to 26 possibilities. So this could
never happen:

	given ($c) {
		when (/a/i){ }
		when (/D/) { call_delete(); }
		when (/d/) { call_ls(); }
		default { }
	}

Do you comprehend that? Or you fail in the obvious again and again?

-- 
http://www.telecable.es/personales/gamo/
The generation of random numbers is too important to be left to chance


------------------------------

Date: Fri, 24 Apr 2015 09:48:32 +0200
From: gamo <gamo@telecable.es>
Subject: Re: How do I use "when" to check for "one of two options"?
Message-Id: <mhcsgf$bks$1@speranza.aioe.org>

El 23/04/15 a las 13:22, Rainer Weikusat escribi贸:
> sub any_of
> {
> 	my $it = $_;
>
>          return 1 if $it ~~ $_ for @_;

This have no sense since $var ~~ @list will
return true if any element of @list match.
So the smart match replaces the whole sub.
That's why it is called smart.

To do a normal comparison you doesn't need ~~


>          return;
> }
>


-- 
http://www.telecable.es/personales/gamo/
The generation of random numbers is too important to be left to chance


------------------------------

Date: Thu, 23 Apr 2015 18:50:02 -0700
From: Robbie Hatley <see.my.sig@for.my.address>
Subject: Re: Mentifex Strong AI Perlmind Programming Journal: 2015 April 12
Message-Id: <4qWdnXYY_8jbPKTInZ2dnUVZ572dnZ2d@giganews.com>


On 4/16/2015 3:28 PM, mentificium@gmail.com wrote:

> On Monday, April 13, 2015 at 1:40:02 AM UTC-7, George Mpouras wrote:
> > On 12/4/2015 8:48 渭渭, mentificium@gmail.com wrote:
> > What you have done so far [...]
>
> My library for coding Perl AI now has five books.
>
> On Mon.14.APR.2015 at Goodwill I bought for
> $01.99 the 1,283-page "PERL Black Book" by Holzner.
> $00.19 WA Sales tax.
> $02.18 TOTAL expenditure.
>
> On Thurs.16.APR.2015 at Half Price Books I bought:
> $02.00 Programming Perl, Second Edition, (C) 1996;
> $09.99 Learning Perl, Second Edition, (C) 1997;
> $05.99 Programming Perl, Third Edition, (C) 2000;
> $05.99 PERL In Easy Steps, (C) 2004.
> $23.97 subtotal
> $02.30 WA 9.6% Sales tax.
> $26.27 TOTAL
>
> Mentifex

That's not quite an answer to what George meant.
What he meant was, what Perl code have you written so far in
your attempts to reach your goals?

When I say "my factorial calculating script is too slow and
gives 'deep recursion errors', what should I do about this?",
and folks here say "show us what you've done", I don't reply
"Well, I own paper copies of 'Learning Perl, 4th Ed' and
'Programming Perl, 3rd Ed', plus a pdf copy of 'Programming
Perl, 4th Ed'."

No, what I'd actually reply with would be this:

#! /usr/bin/perl
#  /rhe/scripts/math/factorial-recursive.perl
use v5.14;
use strict;
use warnings;
use Math::BigInt;
sub Factorial ($);
warn("Entry time: ", time, "\n");
my $x = Math::BigInt->new($ARGV[0]);
my $f = Factorial($x);
say "$x! = $f";
warn("Exit  time: ", time, "\n");
exit 0;
sub Factorial ($) {
    my $x = shift;
    return (1 == $x) ? 1 : $x * Factorial($x - 1);
}

And others would reply back that perhaps recursion is not
the best way to tackle this task, and that perhaps using
the Math::BigInt->bfac() function might be faster, etc.

That's how these Usenet programming-language groups work.
When someone says "show us what you have so far", that's
an invitation to apply fingers to keyboard and do some
programming. Computer programming is not a spectator sport.

How many decades have you been working on your project?
It *has* been decades, hasn't it? Surely you've written
some code?


-- 
Cheers,
Robbie Hatley
Midway City, CA, USA
perl -le 'print "\154o\156e\167o\154f\100w\145ll\56c\157m"'
http://www.well.com/user/lonewolf/
https://www.facebook.com/robbie.hatley


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 4420
***************************************


home help back first fref pref prev next nref lref last post