[32980] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4256 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jul 28 05:17:19 2014

Date: Mon, 28 Jul 2014 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           Mon, 28 Jul 2014     Volume: 11 Number: 4256

Today's topics:
    Re: Determining the last key when iterating through a h (Tim McDaniel)
    Re: Determining the last key when iterating through a h <rvtol+usenet@xs4all.nl>
    Re: Determining the last key when iterating through a h <rweikusat@mobileactivedefense.com>
    Re: Determining the last key when iterating through a h <hjp-usenet3@hjp.at>
    Re: Determining the last key when iterating through a h (Tim McDaniel)
    Re: Determining the last key when iterating through a h (Tim McDaniel)
    Re: Determining the last key when iterating through a h <derykus@gmail.com>
    Re: Determining the last key when iterating through a h <derykus@gmail.com>
    Re: Determining the last key when iterating through a h <rweikusat@mobileactivedefense.com>
    Re: Determining the last key when iterating through a h <rweikusat@mobileactivedefense.com>
    Re: Determining the last key when iterating through a h <derykus@gmail.com>
    Re: Determining the last key when iterating through a h <rweikusat@mobileactivedefense.com>
    Re: Determining the last key when iterating through a h <uri@stemsystems.com>
    Re: knocking rough edges off template project: issue1:  <cal@example.invalid>
    Re: Regex to select between first and third occurence o <news@lawshouse.org>
    Re: Switch module causes __DATA__ to not be read <uri@stemsystems.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 26 Jul 2014 00:27:04 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: Determining the last key when iterating through a hash
Message-Id: <lqusko$943$1@reader1.panix.com>

In article <g73rs9d539rrkiv2drre6jn8jq7m7bpjfv@4ax.com>,
J_rgen Exner  <jurgenex@hotmail.com> wrote:
>about how to process the last element of a list differently from all the
>others, no matter where this list came from.

(Note to Original Poster: don't forget to consider the case of an
empty hash/list, and also a hash/list with just one element.)

>Having said that e.g. by processing the last element separately (i'm
>sure this can be written more compact if so desired):
>
>@regular = sort keys %signals;
>$last = pop @regular;
>foreach $item (@regular){
>....}
>process_last $last;

Or, if there's a lot of overlap in code between the normal case and
the last case, it may be more compact to write

@regular = sort keys %signals;
$last = pop @regular;
foreach $item (@regular, $last) {
    ...
    if ($item eq $last) { # or whatever other test tells you that
        ... # special stuff for the last iteration
    }
    ...
}

That is, "(@regular, $last)" in a foreach (which takes a list) is a
list of all the elements of @regular followed by $last.

-- 
Tim McDaniel, tmcd@panix.com


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

Date: Sat, 26 Jul 2014 11:09:08 +0200
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Determining the last key when iterating through a hash
Message-Id: <53d3700f$0$2966$e4fe514c@news2.news.xs4all.nl>

On 2014-07-26 02:27, Tim McDaniel wrote:

> @regular = sort keys %signals;
> $last = pop @regular;
> foreach $item (@regular, $last) {

If @regular was an empty list, you'll now loop once with undef.

-- 
Ruud



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

Date: Sat, 26 Jul 2014 13:03:24 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Determining the last key when iterating through a hash
Message-Id: <87bnscjpir.fsf@sable.mobileactivedefense.com>

tmcd@panix.com (Tim McDaniel) writes:
> In article <g73rs9d539rrkiv2drre6jn8jq7m7bpjfv@4ax.com>,
> J_rgen Exner  <jurgenex@hotmail.com> wrote:
>>about how to process the last element of a list differently from all the
>>others, no matter where this list came from.
>
> (Note to Original Poster: don't forget to consider the case of an
> empty hash/list, and also a hash/list with just one element.)
>
>>Having said that e.g. by processing the last element separately (i'm
>>sure this can be written more compact if so desired):
>>
>>@regular = sort keys %signals;
>>$last = pop @regular;
>>foreach $item (@regular){
>>....}
>>process_last $last;
>
> Or, if there's a lot of overlap in code between the normal case and
> the last case, it may be more compact to write
>
> @regular = sort keys %signals;
> $last = pop @regular;
> foreach $item (@regular, $last) {
>     ...
>     if ($item eq $last) { # or whatever other test tells you that
>         ... # special stuff for the last iteration
>     }
>     ...
> }

In this case, you could just store the list of keys in the array,
execute the loop without the repeated test and do any remaining 'special
processing' for the last element afterwards.


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

Date: Sat, 26 Jul 2014 15:13:20 +0200
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: Determining the last key when iterating through a hash
Message-Id: <slrnlt7abg.9f5.hjp-usenet3@hrunkner.hjp.at>

On 2014-07-26 12:03, Rainer Weikusat <rweikusat@mobileactivedefense.com> wrote:
> tmcd@panix.com (Tim McDaniel) writes:
>> In article <g73rs9d539rrkiv2drre6jn8jq7m7bpjfv@4ax.com>,
>> J_rgen Exner  <jurgenex@hotmail.com> wrote:
>>>@regular = sort keys %signals;
>>>$last = pop @regular;
>>>foreach $item (@regular){
>>>....}
>>>process_last $last;
>>
>> Or, if there's a lot of overlap in code between the normal case and
>> the last case, it may be more compact to write
>>
>> @regular = sort keys %signals;
>> $last = pop @regular;
>> foreach $item (@regular, $last) {
>>     ...
>>     if ($item eq $last) { # or whatever other test tells you that
>>         ... # special stuff for the last iteration
>>     }
>>     ...
>> }
>
> In this case, you could just store the list of keys in the array,
> execute the loop without the repeated test and do any remaining 'special
> processing' for the last element afterwards.

That depends on whether you can do the 'special processing' after the
normal processing. Maybe the code in the third ellipsis depends on the
code in the second.

Of course you can always move common code into subs:

sub step_1 { ... }
sub step_2 { ... }
sub step_3 { ... }

@regular = sort keys %signals;
$last = pop @regular;
foreach $item (@regular) {
    step_1($item);
    step_3($item);
}
if (defined $last) {
    step_1($item);
    step_2($item); # special stuff for the last iteration
    step_3($item);
}

        hp

-- 
   _  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) |                    | Man feilt solange an seinen Text um, bis
| |   | hjp@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel


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

Date: Sat, 26 Jul 2014 20:17:32 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: Determining the last key when iterating through a hash
Message-Id: <lr12cr$je6$1@reader1.panix.com>

In article <53d3700f$0$2966$e4fe514c@news2.news.xs4all.nl>,
Dr.Ruud <rvtol+usenet@xs4all.nl> wrote:
>On 2014-07-26 02:27, Tim McDaniel wrote:
>
>> @regular = sort keys %signals;
>> $last = pop @regular;
>> foreach $item (@regular, $last) {
>
>If @regular was an empty list, you'll now loop once with undef.

Not true.  An empty list is interpolated as ... an empty list.

$ perl -e '@regular = (); $last = "last";
foreach $item (@regular, $last) { print "item [$item]\n"; }'

outputs only

item [last]

If instead %signals were empty, then $last would be undef ... but I
wrote above that that the coder needs to consider what to do if
%signals being empty (and should also consider what should be done
with one element).

-- 
Tim McDaniel, tmcd@panix.com


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

Date: Sat, 26 Jul 2014 20:23:55 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: Determining the last key when iterating through a hash
Message-Id: <lr12or$ab8$1@reader1.panix.com>

In article <slrnlt7abg.9f5.hjp-usenet3@hrunkner.hjp.at>,
Peter J. Holzer <hjp-usenet3@hjp.at> wrote:
>On 2014-07-26 12:03, Rainer Weikusat
<rweikusat@mobileactivedefense.com> wrote:
>> tmcd@panix.com (Tim McDaniel) writes:
>>> In article <g73rs9d539rrkiv2drre6jn8jq7m7bpjfv@4ax.com>,
>>> J_rgen Exner  <jurgenex@hotmail.com> wrote:
>>>>@regular = sort keys %signals;
>>>>$last = pop @regular;
>>>>foreach $item (@regular){
>>>>....}
>>>>process_last $last;
>>>
>>> Or, if there's a lot of overlap in code between the normal case and
>>> the last case, it may be more compact to write
>>>
>>> @regular = sort keys %signals;
>>> $last = pop @regular;
>>> foreach $item (@regular, $last) {
>>>     ...
>>>     if ($item eq $last) { # or whatever other test tells you that
>>>         ... # special stuff for the last iteration
>>>     }
>>>     ...
>>> }
>>
>> In this case, you could just store the list of keys in the array,
>> execute the loop without the repeated test and do any remaining 'special
>> processing' for the last element afterwards.
>
>That depends on whether you can do the 'special processing' after the
>normal processing. Maybe the code in the third ellipsis depends on the
>code in the second.

Exactly.

>Of course you can always move common code into subs:
>
>sub step_1 { ... }
>sub step_2 { ... }
>sub step_3 { ... }
>
>@regular = sort keys %signals;
>$last = pop @regular;
>foreach $item (@regular) {
>    step_1($item);
>    step_3($item);
>}
>if (defined $last) {
>    step_1($item);
>    step_2($item); # special stuff for the last iteration
>    step_3($item);
>}

Huh.  Looks like you're right -- you can't have a hash index of
undef() -- it is considered to be ''.

That also depends on the code in each of the sections, like if there
are variables set in one that are used in another.  But that can be
handled more or less easily.  That also chops the logic for the loop
into several chunks that are above their actual context, which I think
is a bigger drawback.

-- 
Tim McDaniel, tmcd@panix.com


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

Date: Sat, 26 Jul 2014 17:15:20 -0700
From: Charles DeRykus <derykus@gmail.com>
Subject: Re: Determining the last key when iterating through a hash
Message-Id: <lr1gb5$plb$1@speranza.aioe.org>

On 7/22/2014 1:59 PM, John Black wrote:
> In article <87egxdmbqs.fsf@sable.mobileactivedefense.com>, rweikusat@mobileactivedefense.com
> says...
>>
>> John Black <johnblack@nospam.com> writes:
>>> In article <g73rs9d539rrkiv2drre6jn8jq7m7bpjfv@4ax.com>, jurgenex@hotmail.com says...
>>>> First you question has nothing to do with hashes or keys. It is simply
>>>> about how to process the last element of a list differently from all the
>>>> others, no matter where this list came from.
>>>
>>> Thanks all.  I guess I didn't realize "sort keys %signals" returned a
>>> simple array.
>>
>> It doesn't really return an array. It returns a list which means 'some
>> set of values pushed onto the perl stack'.
>
> Oh come on.  The words array and list are used pretty interchangably in Perl, are they not?
>
>

Many differences.

See:  perldoc -q "What is the difference between a list and an array"

-- 
Charles DeRykus


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

Date: Sat, 26 Jul 2014 22:41:13 -0700
From: Charles DeRykus <derykus@gmail.com>
Subject: Re: Determining the last key when iterating through a hash
Message-Id: <lr23e4$s66$1@speranza.aioe.org>

On 7/21/2014 2:34 PM, John Black wrote:
> Running through the sorted keys of a hash as follows:
>
>    foreach $key (sort keys %signals) {
>    ...
>    }
>
> I want to do something different inside the loop if I am working on the last key of the
> foreach.  How can I determine when the current $key is the last one?  Thanks.
>
>

Another possibility with a fairly modern perl version:

my @keys = sort keys %signals;
while ( my($index,$key) = each(\@keys) ) {
     if ($index == $#keys) { ... }
}

-- 
Charles DeRykus


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

Date: Sun, 27 Jul 2014 19:21:28 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Determining the last key when iterating through a hash
Message-Id: <87k36yvf13.fsf@sable.mobileactivedefense.com>

Charles DeRykus <derykus@gmail.com> writes:
> On 7/21/2014 2:34 PM, John Black wrote:
>> Running through the sorted keys of a hash as follows:
>>
>>    foreach $key (sort keys %signals) {
>>    ...
>>    }
>>
>> I want to do something different inside the loop if I am working on the last key of the
>> foreach.  How can I determine when the current $key is the last one?  Thanks.
>>
>>
>
> Another possibility with a fairly modern perl version:
>
> my @keys = sort keys %signals;
> while ( my($index,$key) = each(\@keys) ) {
>     if ($index == $#keys) { ... }
> }

That's even worse than the idea of a test which is executed on all
iterations but known to be false except on the last one (semantically,
as it add yet more surreptitious noise to the loop and technically,
because it is also going to be rather slow).



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

Date: Sun, 27 Jul 2014 19:29:53 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Determining the last key when iterating through a hash
Message-Id: <87fvhmven2.fsf@sable.mobileactivedefense.com>

"Peter J. Holzer" <hjp-usenet3@hjp.at> writes:
> On 2014-07-26 12:03, Rainer Weikusat <rweikusat@mobileactivedefense.com> wrote:
>> tmcd@panix.com (Tim McDaniel) writes:
>>> In article <g73rs9d539rrkiv2drre6jn8jq7m7bpjfv@4ax.com>,
>>> J_rgen Exner  <jurgenex@hotmail.com> wrote:
>>>>@regular = sort keys %signals;
>>>>$last = pop @regular;
>>>>foreach $item (@regular){
>>>>....}
>>>>process_last $last;
>>>
>>> Or, if there's a lot of overlap in code between the normal case and
>>> the last case, it may be more compact to write
>>>
>>> @regular = sort keys %signals;
>>> $last = pop @regular;
>>> foreach $item (@regular, $last) {
>>>     ...
>>>     if ($item eq $last) { # or whatever other test tells you that
>>>         ... # special stuff for the last iteration
>>>     }
>>>     ...
>>> }
>>
>> In this case, you could just store the list of keys in the array,
>> execute the loop without the repeated test and do any remaining 'special
>> processing' for the last element afterwards.
>
> That depends on whether you can do the 'special processing' after the
> normal processing. Maybe the code in the third ellipsis depends on the
> code in the second.

Maybe, maybe not. Nothing is known about that. But the general idea of a
loop like this:

for (@list) {
	# actions supposed to be performed repeatedly
        
        if (test()) {
        	# actions supposed to be performed
                # once after all but the last item
                # were processed
	}
}

is sort-of a travesty as the while concept of 'a loop' is 'perform the
same operation repeatedly'. Because of this, this should generally rather
be

for (@list[0 .. $#list - 1]) {
	# actions supposed to be performed repeatedly
}
# do something special with $list[-1]


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

Date: Sun, 27 Jul 2014 13:01:52 -0700
From: Charles DeRykus <derykus@gmail.com>
Subject: Re: Determining the last key when iterating through a hash
Message-Id: <lr3lrp$p9o$1@speranza.aioe.org>

On 7/27/2014 11:21 AM, Rainer Weikusat wrote:
> Charles DeRykus <derykus@gmail.com> writes:
>> On 7/21/2014 2:34 PM, John Black wrote:
>>> Running through the sorted keys of a hash as follows:
>>>
>>>     foreach $key (sort keys %signals) {
>>>     ...
>>>     }
>>>
>>> I want to do something different inside the loop if I am working on the last key of the
>>> foreach.  How can I determine when the current $key is the last one?  Thanks.
>>>
>>>
>>
>> Another possibility with a fairly modern perl version:
>>
>> my @keys = sort keys %signals;
>> while ( my($index,$key) = each(\@keys) ) {
>>      if ($index == $#keys) { ... }
>> }
>
> That's even worse than the idea of a test which is executed on all
> iterations but known to be false except on the last one (semantically,
> as it add yet more surreptitious noise to the loop and technically,
> because it is also going to be rather slow).
>

I'm not sure what you're getting at with "surreptitious noise". An index
count provided by perl makes more sense to me and is arguably more 
intuitive and straightforward than providing one on your own.  As for 
the contortions needed to avoid all the "iterations known to be false",
I believe this is micro-optimizing for dubious gain and, unless a 
solution is hideously obtuse or glacially slow, is TIMTOWDI-worthy.

-- 
Charles DeRykus


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

Date: Sun, 27 Jul 2014 22:12:05 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Determining the last key when iterating through a hash
Message-Id: <87vbqitska.fsf@sable.mobileactivedefense.com>

Charles DeRykus <derykus@gmail.com> writes:
> On 7/27/2014 11:21 AM, Rainer Weikusat wrote:
>> Charles DeRykus <derykus@gmail.com> writes:
>>> On 7/21/2014 2:34 PM, John Black wrote:
>>>> Running through the sorted keys of a hash as follows:
>>>>
>>>>     foreach $key (sort keys %signals) {
>>>>     ...
>>>>     }
>>>>
>>>> I want to do something different inside the loop if I am working on the last key of the
>>>> foreach.  How can I determine when the current $key is the last one?  Thanks.
>>>>
>>>>
>>>
>>> Another possibility with a fairly modern perl version:
>>>
>>> my @keys = sort keys %signals;
>>> while ( my($index,$key) = each(\@keys) ) {
>>>      if ($index == $#keys) { ... }
>>> }
>>
>> That's even worse than the idea of a test which is executed on all
>> iterations but known to be false except on the last one (semantically,
>> as it add yet more surreptitious noise to the loop and technically,
>> because it is also going to be rather slow).
>>
>
> I'm not sure what you're getting at with "surreptitious noise". An index
> count provided by perl makes more sense to me and is arguably more
> intuitive and straightforward than providing one on your own.

Except that perl doesn't provide an index count. Sufficiently
'new-fangled' version of Perl overload the meaning of 'each' to apply to
arrays, too, and return an array index and the corresponding value in
this case. That's something each has never done during the past 20 years
so it will be seriously counter-intuitive to a lot of people, especially
considering that the array indices are not stored in an array: Perl is
not PHP and has two different data types here.

Equivalent code without each could look like this:

my (@keys, $index) = sort(keys(%signals));
for (@keys) {
	# perform the loop here

        if (++$index == @keys) {
        	# perform the stuff which doesn't
                # belong ot the loop here
	}
}

That's less code and it uses a 'standard' foreach-loop for traversing
the list. Both perform an operation on each iteration in order to
maintain a running counter and a second operation to check if the
current element is the last one.

The claim that

my ($index, $key) = each(\@keys)

 .
 .
 .
if ($index == $#keys)

is inherently 'more intuitive' than

for (@keys) {

if (++$index == @keys)

just because it need more than twenty years until someone came up with
support for the first variant while the second has existed during all of
this time seems rather bizarre to me.

> As for the contortions needed to avoid all the "iterations known to be
> false", I believe this is micro-optimizing for dubious gain and,

One of my all time favorite quote is:

	A program is a sort of publication.  It's meant to be read by
	the programmer, another programmer (perhaps yourself a few days,
	weeks or years later), and lastly a machine.  The machine
	doesn't care how pretty the program is - if the program
	compiles, the machine's happy - but people do, and they should.
        http://www.lysator.liu.se/c/pikestyle.html

IOW, "But the computer doesn't care!" is not a good excuse for writing
stuff into a loop body which isn't really part of the loop.


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

Date: Sun, 27 Jul 2014 20:45:01 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Determining the last key when iterating through a hash
Message-Id: <871tt6e2gi.fsf@stemsystems.com>

>>>>> "TM" == Tim McDaniel <tmcd@panix.com> writes:

  TM> In article <53d3700f$0$2966$e4fe514c@news2.news.xs4all.nl>,
  TM> Dr.Ruud <rvtol+usenet@xs4all.nl> wrote:
  >> On 2014-07-26 02:27, Tim McDaniel wrote:
  >> 
  >>> @regular = sort keys %signals;
  >>> $last = pop @regular;
  >>> foreach $item (@regular, $last) {
  >> 
  >> If @regular was an empty list, you'll now loop once with undef.

  TM> Not true.  An empty list is interpolated as ... an empty list.

but $last is undef and will cause the loop to iterate one time.

better to do:

	$last = $regular[-1] ;
	foreach my $item( @regular ) {

then you will only loop if you have elements and you can still test for
last.

uri


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

Date: Sat, 26 Jul 2014 01:52:44 -0700
From: Cal Dershowitz <cal@example.invalid>
Subject: Re: knocking rough edges off template project: issue1: encodings.
Message-Id: <c3h8itFlcm6U1@mid.individual.net>

On 07/24/2014 03:43 PM, Cal Dershowitz wrote:

>
> Q1) How do I use perl to represent cyrillic faithfully?

I have better results now.  I solved the ordering issue on the captions, 
leaving only the perl encoding mystery outstanding.

http://merrillpjensen.com/pages/golf1.html

This is one of three modules that support the script.  The first is 
config1, which has my ftp data, which I will not be sharing.  The second 
is renoir1.pm, where I kept the functions that I wasn't working on for 
this revision.  "renoir" was the last stable workspace before I cloned 
it and moved on.  renoir2.pm is where I believe the revision needs to 
take place:

package renoir2;
require Exporter;
use config1;
use renoir1 qw( print_hash );

our @ISA = qw(Exporter);
our @EXPORT = qw(
  get_content print_aoa schmintf  write_bottom);



sub get_content{
use strict;
use 5.010;
use File::Basename;
use Cwd;
use HTML::FromText;
use File::Slurp;
use Path::Class;

my $rvars = shift;
my %vars = %$rvars;

my $refimg = get_images($rvars);
my $refcaps = get_eng_text($rvars);
my $refruscaps = get_rus_text($rvars);
my $aoa = [ $refimg, $refcaps, $refruscaps ];
#print_aoa($aoa);
my $b = invert_aoa($aoa);
return ($b);
}

sub get_images {
use strict;
use 5.010;
use File::Basename;
use Cwd;
use HTML::FromText;
use File::Slurp;
use Path::Class;

my $rvars = shift;
my %vars = %$rvars;
my $current = cwd;
my $rd2 = dir($current);
my @a = $rd2->dir_list();
my @filetypes = qw/jpg gif png jpeg/;
my $pattern = join '|', map "($_)", @filetypes;
my @matching2;
opendir my $hh, $vars{to_images} or warn "warn  $!\n";
while (defined ($_ = readdir($hh))){
if ($_ =~ /($pattern)$/i) {
    push(@matching2, $_);
    }
}
#important to sort
@matching2 = sort @matching2;
return \@matching2;
}

sub get_eng_text {
use strict;
use 5.010;
use File::Basename;
use Cwd;
use HTML::FromText;
use File::Slurp;
use Path::Class;

my $rvars = shift;
my %vars = %$rvars;

my %content;
my $refc = \%content;

opendir my $eh, $vars{"eng_captions"} or die "dead  $!\n";
while (defined ($_ = readdir($eh))){
next if m/~$/;
next if -d;
if (m/txt$/){
    my $file = file($vars{"eng_captions"},$_);
    my $string = read_file($file);
    # surround by divs
    my $oitop = read_file($vars{"oitop"});
    my $oibottom = read_file($vars{"oibottom"});
    my $text = $oitop.$string.$oibottom;
    $content{$_} = $text;
    }
}
closedir $eh;
#important to sort
my @return;
foreach my $key (sort keys %content) {
     print $content{$key} . "\n";
     push @return, $content{$key};
}

#print_hash($refc);
say " return is @return";
return \@return;
}

sub print_aoa{
use strict;
use warnings;
use 5.010;

my $a = shift;
my @AoA = @$a;
     for my $i ( 0 .. $#AoA ) {
         my $aref = $AoA[$i];
         for my $j ( 0 .. $#{$aref} ) {
             print "elt $i $j is $AoA[$i][$j]\n";
         }
     }
return $a;
}

sub invert_aoa{
use strict;
use warnings;
use 5.010;

my $a = shift;
my @AoA = @$a;
my $k = $#AoA;
#say "k is $k";
my @BoB;
     for my $i ( 0 .. $#AoA ) {
         my $aref = $AoA[$i];
         my $x = $#{$aref};
         #say "x is $x";
         for my $j ( 0 .. $#{$aref} ) {
             $BoB[$j][$i]= $AoA[$i][$j];
         }
     }
my $b = \@BoB;
return $b;
}

sub schmintf{
use strict;
use warnings;
use 5.010;
use Text::Template;
my $rvars = shift;
my $reftoAoA = shift;
my %vars = %$rvars;
my @AoA = @$reftoAoA;
say "in schmint ";
my $body = $vars{"body"};
my $template = Text::Template->new(
     ENCODING => 'utf8',
     SOURCE => $body)
     or die "Couldn't construct template: $!";
my $return;
for my $i ( 0 .. $#AoA ){
$vars{"file"} = $AoA[$i][0];
$vars{"english"} = $AoA[$i][1];
$vars{"russian"} = $AoA[$i][2];

my $result = $template->fill_in(HASH => \%vars);

$return = $return.$result;
}
#say "return is $return";
return \$return;
}

sub get_rus_text {
use strict;
use 5.010;
use File::Basename;
use Cwd;
use HTML::FromText;
use File::Slurp;
use Path::Class;

my $rvars = shift;
my %vars = %$rvars;

my %content;
my $refc = \%content;
opendir my $eh, $vars{"rus_captions"} or die "dead  $!\n";
while (defined ($_ = readdir($eh))){
next if m/~$/;
next if -d;
if (m/txt$/){
    my $file = file($vars{"rus_captions"},$_);
    my $string = read_file($file);
    # surround by divs
    my $oitop = read_file($vars{"oitop"});
    my $oibottom = read_file($vars{"oibottom"});
    my $text = $oitop.$string.$oibottom;
    $content{$_} = $text;
    }
}
closedir $eh;
#important to sort
my @return;
foreach my $key (sort keys %content) {
     print $content{$key} . "\n";
     push @return, $content{$key};
}

#print_hash($refc);
say " return is @return";
return \@return;
}

sub write_bottom  {
use strict;
use Text::Template;
my ($rvars) = shift;
my %vars = %$rvars;
my $footer = $vars{"bottom"};
my $template = Text::Template->new(SOURCE => $footer)
           or die "Couldn't construct template: $!";
my $result = $template->fill_in(HASH => $rvars);
return \$result;
}

1;

This is the script that calls it:

#!/usr/bin/perl -w
use strict;
use 5.010;
use lib "template_stuff";
use renoir1 qw(get_ftp_object  get_html_filename
       write_script create_html_file write_header write_footer);
use renoir2 qw( get_content print_aoa schmintf write_bottom);
use Cwd;
use File::Basename;
use Net::FTP;
use Path::Class;
use File::Slurp;
use File::Spec;

# initializations that must precede main data structure
my $ts = "template_stuff";
my $images = "aimages";
my $captions = "captions";
my $ruscaptions = "ruscaptions";
my $current = cwd;
my $rd1 = dir($current);
my @a = $rd1->dir_list();
my $srd1 = $rd1->stringify;
my $title = $rd1->basename;
say "title is $title";
# get headline from stdin
say "Give me a headline:  ";
my $headline = <STDIN>;
my $rd2 = dir(@a,$ts,$images);
my $to_images = $rd2->stringify;
my $rd3 = dir(@a,$ts,$captions);
my $to_captions = $rd3->stringify;
my $rd4 = dir(@a,$ts,$ruscaptions);
my $rus_captions = $rd4->stringify;

# page params
my %vars = (
    title => $title,
    headline => $headline,
    place => 'Oakland',
    css_file => "${title}1.css",
    header => file($ts,"hc_input2.txt"),
    footer => file($ts,"footer_center2.txt"),
    css_local => file($ts,"${title}1.css"),
    body => file($ts,"rebus4.tmpl"),
    print_script => "1",
    code_tmpl=> file($ts,"code1.tmpl"),
    oitop=> file($ts,"oitop.txt"),
    oibottom=> file($ts,"oibottom.txt"),
    to_images => $to_images,
    eng_captions => $to_captions,
    rus_captions =>  $rus_captions,
    bottom => file($ts,"bottom1.txt"),
         );
#main control
my $rvars = \%vars;
my $rftp = get_ftp_object();
my $html_file = get_html_filename($rftp);
my $fh = create_html_file ($html_file);
my $remote_dir = $html_file;
$remote_dir =~ s/\.html$//;
say "remote_dir is $remote_dir";
$vars{remote_dir}= $remote_dir;
# create header
my $rhdr = write_header($rvars);
print $fh $$rhdr;
# print content to file
my $refc = get_content($rvars);
my @AoA = @$refc;
say "in main";
#print_aoa($refc);
my $body = schmintf($rvars, $refc);
print $fh $$body;
my $rftr = write_footer($rvars);
print $fh $$rftr;
if ($vars{"print_script"}) {
    my $specifier2 =  read_file( $vars{"code_tmpl"} ) ;
    print "specifier2 is $specifier2\n";
    my $fspecfile = File::Spec->rel2abs(__FILE__);
    print "File::Spec->rel2abs(__FILE__):       $fspecfile\n";
    my $text1 = read_file($fspecfile);
    printf $fh $specifier2, $text1;
};
my $rhbt = write_bottom($rvars);
print $fh $$rhbt;
close $fh;
#load html file to server
$rftp->cwd("/pages") or warn "cwd failed $!\n";
$rftp->put($html_file) or die "put failed $!\n";
#load css file to server
$rftp->cwd("/css") or warn "cwd failed $@\n";
my $path3 = file(@a, $vars{"css_local"});
my $remote_css = $vars{"css_file"};
$rftp->put("$path3", $remote_css) or warn "put failed $@\n";
# load images
$rftp->binary or warn "binary failed$!\n";
$rftp->cwd("/images") or warn "cwd failed $!\n";
$rftp->mkdir($remote_dir) or warn "cwd failed $!\n";
$rftp->cwd($remote_dir)
       or warn "Cannot change working directory ", $rftp->message;
for my $i ( 0 .. $#AoA ) {
    my $a = file(@a,$ts,$images,$AoA[$i][0]);
    my $sa = $a->stringify;
    my $b = file($AoA[$i][0]);
    my $sb = $b->stringify;
    $rftp->put($sa, $sb) or warn "put failed $@\n";
    }
$rftp->ls;
$rftp->quit();
say "new file is $html_file";

Something here clobbers cyrillic....
-- 
Cal Dershowitz


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

Date: Mon, 28 Jul 2014 09:19:33 +0100
From: Henry Law <news@lawshouse.org>
Subject: Re: Regex to select between first and third occurence of comma
Message-Id: <BICdnfJOvY0KmkvOnZ2dnUVZ8rednZ2d@giganews.com>

On 24/07/14 14:17, IJALAB wrote:
> I have lot of lines like below:
>                                                                             "MONTH", 1, NULL, 0}, //11

>
> I need the following output:
>
>                                                                             "MONTH",  //11

I would use Text::CSV for that; works well, easy to use, and insulates 
you from so many CSV-related things that you don't think of.

-- 

Henry Law            Manchester, England


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

Date: Sun, 27 Jul 2014 20:46:23 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Switch module causes __DATA__ to not be read
Message-Id: <87wqaycnts.fsf@stemsystems.com>

>>>>> "d" == demmith  <demmith@gmail.com> writes:

  d> If your script contains the __DATA__ token AND you are using the
  d> Switch module, your __DATA__ token will not be read.

this is one of the known problems with switch.pm and it has been removed
from the perl core distribution. source filtering is nasty in perl and
shouldn't be done.

uri


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

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 4256
***************************************


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