[30860] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2105 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jan 8 16:09:48 2009

Date: Thu, 8 Jan 2009 13:09:13 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 8 Jan 2009     Volume: 11 Number: 2105

Today's topics:
    Re: -1 ** 0 == -1 ??? <g_m@remove-comcast.net>
        I am needing a gentle introduction to accessing a perl  <lvirden@gmail.com>
    Re: loops in perl automated ftp <tzz@lifelogs.com>
    Re: loops in perl automated ftp <banzai@ll.com>
    Re: loops in perl automated ftp <banzai@ll.com>
    Re: loops in perl automated ftp <clauskick@hotmail.com>
    Re: loops in perl automated ftp <glex_no-spam@qwest-spam-no.invalid>
    Re: loops in perl automated ftp <tadmc@seesig.invalid>
    Re: opening a file <cartercc@gmail.com>
    Re: opening a file <jurgenex@hotmail.com>
    Re: opening a file <jurgenex@hotmail.com>
    Re: Parsing a string into a hash <g_m@remove-comcast.net>
        please help me to understand this code? <pilcrow6@gmail.com>
    Re: please help me to understand this code? <glex_no-spam@qwest-spam-no.invalid>
    Re: please help me to understand this code? <tim@burlyhost.com>
        Regular Expression Question <no@spam.com>
    Re: Regular Expression Question <tim@burlyhost.com>
    Re: Regular Expression Question (hymie!)
    Re: Regular Expression Question <cwilbur@chromatico.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 8 Jan 2009 13:34:21 -0500
From: "~greg" <g_m@remove-comcast.net>
Subject: Re: -1 ** 0 == -1 ???
Message-Id: <S4ednel7RYx_2_vUnZ2dnUVZ_sPinZ2d@giganews.com>


"John W. Krahn" <someone@example.com> wrote in message news:h259l.66954$em5.23717@newsfe22.iad...
> neilsolent wrote:
>> Hang on ....
>>
>> perl -le 'print (-1)**0'
>> -1
>>
>> perl -e 'print ((-1)**0)'
>> 1
>>
>> So I don't this is working. Isn't the latter just giving the number of
>> elements in the list ?
>> Mathematically, (-1) to the power 0 should be 1. So the first should
>> return 1 as well.
>>
>> What am I missing ?
>
> Turn on warnings to find out:
>
> $ perl -wle 'print (-1)**0'
> print (...) interpreted as function at -e line 1.
> Useless use of exponentiation (**) in void context at -e line 1.
> -1


probably lucky the (-1) wasn't interpreted as a file handle
to a core OS component










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

Date: Thu, 8 Jan 2009 12:37:59 -0800 (PST)
From: "Larry W. Virden" <lvirden@gmail.com>
Subject: I am needing a gentle introduction to accessing a perl array from a  reference
Message-Id: <5ac3764f-75b2-4a7d-96ed-6e36e22484c9@a12g2000pro.googlegroups.com>

I have some legacy code, the author of whom is no longer available for
consultation.

Here is a piece of the code in question:

:
up here, the DBI connection to the database, the select statement,
etc. occurs
:

# put results of query into single array and return
  while (@next_row= $query->fetchrow_array()){

    push(@array_result,@next_row);
  }

  $query->finish;
  $dbconn->disconnect();
  return \@array_result;

I am a perl novice. I am trying to figure out a readable approach to
dealing with the value that is returned.

One piece of code that I've bubble-gummed and bailing wired together
basically does this:

$super_list = thefunction($argument);
my $tot = scalar(@$super_list);

if ( $tot_supers >= 1 ) {
        for (my $staff=0; $staff < $tot_supers; $staff++ ) {
                if ( defined( $super_list->[$staff]{'User_Name'} ) ) {
                       $super_login = $super_list->[$staff]
{'User_Name'};
                       chomp($super_login);
                       print $super_login . "\n" ;
                } else {
                        print "Unable to identify user $staff \n";
                }
        }
    }

The main concern I have here is that if I want to write a "print all
columns of data" type routine, I would have to hand code a series of
if (defined...) type statements, differing depending on what table is
being dumped.

Is there another approach that would allow me to dynamically generate
the columns from the hash and then loop through them?

I apologize from the vague description - I'm still working on
understanding the data structure , etc.


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

Date: Thu, 08 Jan 2009 08:45:37 -0600
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: loops in perl automated ftp
Message-Id: <86eizdua9a.fsf@lifelogs.com>

On Thu, 8 Jan 2009 10:25:54 -0000 "banzai" <banzai@ll.com> wrote: 

b>     $ftp1->put("$FileBodyName.tar.gz") or LogError("Could not FTP copy $FileBodyName.tar.gz to $FTP1server");
b>     $ftp1->put("$DataTagName.gz") or LogError("Could not FTP copy $DataTagName.gz to $FTP1server");

b> I need to modify the above so that the ftp transfers (put's) are attempted 
b> up to 5 times in case the remote server does not respond. No time delay 
b> needed between retries.

Each one can be done like so (untested code):

for (1..5)
{
 last if $ftp1->put("$FileBodyName.tar.gz");
 LogError("Could not FTP copy $FileBodyName.tar.gz to $FTP1server");
}

In other words, do the loop 5 times unless one of the put() calls
succeeds.  If that happens, exit early with `last'.

Ted


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

Date: Thu, 8 Jan 2009 15:13:51 -0000
From: "banzai" <banzai@ll.com>
Subject: Re: loops in perl automated ftp
Message-Id: <gk557j$b6j$1@usenet01.boi.hp.com>


"Ted Zlatanov" <tzz@lifelogs.com> wrote in message 
news:86eizdua9a.fsf@lifelogs.com...
> On Thu, 8 Jan 2009 10:25:54 -0000 "banzai" <banzai@ll.com> wrote:
>
> b>     $ftp1->put("$FileBodyName.tar.gz") or LogError("Could not FTP copy 
> $FileBodyName.tar.gz to $FTP1server");
> b>     $ftp1->put("$DataTagName.gz") or LogError("Could not FTP copy 
> $DataTagName.gz to $FTP1server");
>
> b> I need to modify the above so that the ftp transfers (put's) are 
> attempted
> b> up to 5 times in case the remote server does not respond. No time delay
> b> needed between retries.
>
> Each one can be done like so (untested code):
>
> for (1..5)
> {
> last if $ftp1->put("$FileBodyName.tar.gz");
> LogError("Could not FTP copy $FileBodyName.tar.gz to $FTP1server");
> }
>
> In other words, do the loop 5 times unless one of the put() calls
> succeeds.  If that happens, exit early with `last'.
>
> Ted


Thanks Ted - looks like a good solution so I will try.
Snorik, this one suits best I think as your solution ignored exit status (if 
thats the right word in perl) of previous attempt(s) . i.e. if first attempt 
succeeds then no need to retry 




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

Date: Thu, 8 Jan 2009 15:18:30 -0000
From: "banzai" <banzai@ll.com>
Subject: Re: loops in perl automated ftp
Message-Id: <gk55g9$dqb$1@usenet01.boi.hp.com>


"Ted Zlatanov" <tzz@lifelogs.com> wrote in message 
news:86eizdua9a.fsf@lifelogs.com...
> On Thu, 8 Jan 2009 10:25:54 -0000 "banzai" <banzai@ll.com> wrote:
>
> b>     $ftp1->put("$FileBodyName.tar.gz") or LogError("Could not FTP copy 
> $FileBodyName.tar.gz to $FTP1server");
> b>     $ftp1->put("$DataTagName.gz") or LogError("Could not FTP copy 
> $DataTagName.gz to $FTP1server");
>
> b> I need to modify the above so that the ftp transfers (put's) are 
> attempted
> b> up to 5 times in case the remote server does not respond. No time delay
> b> needed between retries.
>
> Each one can be done like so (untested code):
>
> for (1..5)
> {
> last if $ftp1->put("$FileBodyName.tar.gz");
> LogError("Could not FTP copy $FileBodyName.tar.gz to $FTP1server");
> }
>
> In other words, do the loop 5 times unless one of the put() calls
> succeeds.  If that happens, exit early with `last'.
>
> Ted


Actually, one more question :-)

Could a smiliar 5x loop be put around the whole ftp sequence i.e. login, cwd 
and put so tha the whole process is repeated 5 times if required (not just 
the put) and exit early if the put succeeds 




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

Date: Thu, 8 Jan 2009 08:15:27 -0800 (PST)
From: Snorik <clauskick@hotmail.com>
Subject: Re: loops in perl automated ftp
Message-Id: <142faf22-cefc-4098-a2a0-0d722f246c69@r37g2000prr.googlegroups.com>

On 8 Jan., 16:13, "banzai" <ban...@ll.com> wrote:
> "Ted Zlatanov" <t...@lifelogs.com> wrote in message
>
> news:86eizdua9a.fsf@lifelogs.com...
>
>
>
> > On Thu, 8 Jan 2009 10:25:54 -0000 "banzai" <ban...@ll.com> wrote:
>
> > b>     $ftp1->put("$FileBodyName.tar.gz") or LogError("Could not FTP copy
> > $FileBodyName.tar.gz to $FTP1server");
> > b>     $ftp1->put("$DataTagName.gz") or LogError("Could not FTP copy
> > $DataTagName.gz to $FTP1server");
>
> > b> I need to modify the above so that the ftp transfers (put's) are
> > attempted
> > b> up to 5 times in case the remote server does not respond. No time delay
> > b> needed between retries.
>
> > Each one can be done like so (untested code):
>
> > for (1..5)
> > {
> > last if $ftp1->put("$FileBodyName.tar.gz");
> > LogError("Could not FTP copy $FileBodyName.tar.gz to $FTP1server");
> > }
>
> > In other words, do the loop 5 times unless one of the put() calls
> > succeeds.  If that happens, exit early with `last'.
>
> > Ted
>
> Thanks Ted - looks like a good solution so I will try.
> Snorik, this one suits best I think as your solution ignored exit status (if
> thats the right word in perl) of previous attempt(s) . i.e. if first attempt
> succeeds then no need to retry

Yes, this is completely right, I forgot about that actually :) Teds
solution is shorter, without being illegible.


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

Date: Thu, 08 Jan 2009 10:58:17 -0600
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: loops in perl automated ftp
Message-Id: <496630a9$0$89869$815e3792@news.qwest.net>

banzai wrote:

> Actually, one more question :-)
> 
> Could a smiliar 5x loop be put around the whole ftp sequence i.e. login, cwd 
> and put so tha the whole process is repeated 5 times if required (not just 
> the put) and exit early if the put succeeds 
> 
> 

Sure.  Why do you think it wouldn't work?  Actually, maybe it'd
be better to figure out why it's failing to work the first time.

You'll have to exit out of the loop correctly, probably easiest to
use a label.

perldoc -f last


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

Date: Thu, 8 Jan 2009 08:22:21 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: loops in perl automated ftp
Message-Id: <slrngmc30t.68i.tadmc@tadmc30.sbcglobal.net>

["Followup-To:" header set to comp.lang.perl.misc.]

Snorik <clauskick@hotmail.com> wrote:
> On 8 Jan., 11:25, "banzai" <ban...@ll.com> wrote:
>> Need help with the following automated ftp script please;
>>
>> if (defined ($ftp1)) {
>>     $ftp1->login($FTP1user,$FTP1password) or LogError("FTP login to
>> $FTP1server failed");
>>     if (defined $FTP1destination){
>>         $ftp1->cwd($FTP1destination) or LogError("Could not change directory
>> to $FTP1destination on $FTP1server");
>>     }
>>     $ftp1->binary;
>>     $ftp1->put("$FileBodyName.tar.gz") or LogError("Could not FTP copy
>> $FileBodyName.tar.gz to $FTP1server");
>>     $ftp1->put("$DataTagName.gz") or LogError("Could not FTP copy
>> $DataTagName.gz to $FTP1server");
>>     $ftp1->quit;} else {
>>
>>         LogError("FTP open failed to $FTP1server, error code: $!");}
>>
>> #
>>
>> I need to modify the above so that the ftp transfers (put's) are attempted
>> up to 5 times in case the remote server does not respond. No time delay
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> needed between retries.
>>
>> TIA
>
> Why not simply use a counter variable in a while loop?


Because if the remote server _does_ respond, it will upload the
same file 5 times when once was enough.

The solution must somewhere include a test of whether the server
responded or not.


> my $count = 0;
> while ($count <= 4)

That is an error-prone way to do it.

    foreach my $count ( 0 .. 4 )
or
    foreach my $count ( 1 .. 5 )

is much easier to read (and therefore, easier to get right).


> {
>      $ftp1->put("$FileBodyName.tar.gz") or LogError("Could not FTP
> copy $FileBodyName.tar.gz to $FTP1server");
>      $ftp1->put("$DataTagName.gz") or LogError("Could not FTP copy
> $DataTagName.gz to $FTP1server");
>      $count++;
> }


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Thu, 8 Jan 2009 06:33:46 -0800 (PST)
From: cartercc <cartercc@gmail.com>
Subject: Re: opening a file
Message-Id: <9c8f3bc2-274e-41ed-b09d-cc0c833a8c63@v5g2000prm.googlegroups.com>

On Jan 7, 9:50=A0pm, George <geo...@example.invalid> wrote:
> I thought I would use perl instead of fortran to parse a text file, what
> with the new m// s/// capabilities at my fingertips.

Easy, easy, easy ...

open INFILE, "<nameoffile.txt";
while (<INFILE>) {print;}
close INFILE;

This creates the handle INFILE for the named file in your current
directory and cycles through it line by line, printing each line. To
write the file to an out handle, do this:

open INFILE, "<nameoffile.txt";
open OUTFILE, ">nameofnewfile.txt";
while (<INFILE>) { print OUTFILE;}
close OUTFILE;
close INFILE;

CC.


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

Date: Thu, 08 Jan 2009 10:52:23 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: opening a file
Message-Id: <rlicm4d4p20i50bgr1tobduf64mp3889qg@4ax.com>

cartercc <cartercc@gmail.com> wrote:
>On Jan 7, 9:50 pm, George <geo...@example.invalid> wrote:
>> I thought I would use perl instead of fortran to parse a text file, what
>> with the new m// s/// capabilities at my fingertips.
>
>Easy, easy, easy ...
>
>open INFILE, "<nameoffile.txt";

Most people would suggest to use the three-argument form of open:
	open INFILE, '<', 'nameoffile.txt';

Almost all people would strongly suggest to test for failure:
	open INFILE, '<', 'nameoffile.txt' or
		die "Cannot open nameoffile.txt because $!\n";

jue


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

Date: Thu, 08 Jan 2009 10:49:24 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: opening a file
Message-Id: <4uhcm4dkain5ncs9jqpr4qopmu5v7scsgc@4ax.com>

George <george@example.invalid> wrote:
>On Wed, 07 Jan 2009 19:05:01 -0800, Jürgen Exner wrote:
>> George <george@example.invalid> wrote:
>>>I thought I would use perl instead of fortran to parse a text file, what
>>>with the new m// s/// capabilities at my fingertips.
>>>
>>>So it is that I need to open a text file 
>> 
>> perldoc -f open
>> 
>>>and send it to STDOUT.  
>> 
>> perldoc -f print
>
>Both of these turn up a lot of information, most of which is overkill for 
>this purpose.  Furthermore, perl's features are simply confusing for me in 
>that there are so many, and I only need a little text file that's already 
>on my machine.

I strongly suggest you get an introductory book about Perl. Learning the
concepts of programming language by looking at individual commands is
doomed to fail.

Even "Programming Perl", although not a tutorial, explains file handles
and open() on page 12 and 13 already and reading from a file is
introduced informally together with "while"on page 25 and used in
exactly the way you are looking for on page 27.

The standard Perl idiom for reading and looping through a file is

open(MyFile, '<', $MyFileName) or 
	die "Cannot open $MyFileName for reading because of $!\n";
while (my $line = <MyFile>) {
	#process $line here
}
close MyFile or die "Cannot close file because of $!\n"; 

>It brings up the notion of filehandles, which is fine, but it is something 
>I only know from C++ and in an MS development environment.  I understand 
>how to open files in C.

Well, it's very similar in Perl. You provide a file handle, the mode,
and the name of the file.

>> There is no relation between Perl and servers.
>
>What language do sysadmins use?

Whatever fits the bill. But sysadmin and servers only related by
coincidence. While it is true that many servers are maintained by
sysadmins, there are just as many servers not maintained by sysadmins
and just as many sysadmins, who couldn't care less about servers.

>Nominally, I want to delimit Larry Wall's sigs in a manner that will work 
>for my next identity. 

What? Are you a nym shifter?

> They look like:
>
>%%
>"And I don't like doing silly things (except on purpose)."
>             -- Larry Wall in <1992Jul3.191825.14435@netlabs.com>
>%%
>:        And it goes against the grain of building small tools.
>Innocent, Your Honor.  Perl users build small tools all day long.
>             -- Larry Wall in <1992Aug26.184221.29627@netlabs.com>
>%%
>/* And you'll never guess what the dog had */
>/*   in its mouth... */
>             -- Larry Wall in stab.c from the perl source code
>%%
>
>, and I want to take the %% and replace it with $divider.
>
>Otherwise I want to condition the set that this fortran program outputs.  I 
>have no idea what to do with some of these characters.
>
>
>module arjen2  
>
>type solar_system
>      type(solar_object), dimension(:), pointer :: object
>  end type
>
>  type solar_object
>      character(len=20) :: name
>      integer :: ascent_hour, ascent_minute, ascent_second
>      integer :: dec_degrees
>      real    :: dec_mins, distance, azimuth, altitude
>      logical :: ER, UP
>     
>  end type
>
>end module arjen2
>
>! default precision
>
>
>integer, parameter:: max = 1023
>
>integer:: ln, istat
>
>character(len=max):: line
>
>open(unit=50,file='eph4.txt')
>
>do
>  Read(50,'(A)',IOSTAT=istat) line
>  ln = Len_Trim(line)
>  If (istat /= 0) Exit
>  call parser(line, ln)
>
>  write(*,*) trim(line)
>end do
>contains 
>subroutine parser(line, ln)
> use arjen2
>integer::ln
>character(len=ln):: line
>
>  print *, line, ln
>
>
>
>end subroutine
>
>endprogram
>! g95 eph7.f03 -o r.exe
>
>C:\MinGW\source>g95 eph7.f03 -o r.exe
>
>C:\MinGW\source>r
> Sun    18h 41m 55s     -23? 5.4'       0.983   10.215  52.155  Up 48
> Sun    18h 41m 55s     -23? 5.4'       0.983   10.215  52.155  Up
> Mercury        20h 2m 16s      -22? 12.5'      1.102   22.537  37.668  Up
>52
> Mercury        20h 2m 16s      -22? 12.5'      1.102   22.537  37.668  Up
> Venus  21h 55m 33s     -14? 16.3'      0.795   39.872  11.703  Up 51
> Venus  21h 55m 33s     -14? 16.3'      0.795   39.872  11.703  Up
> Moon   21h 17m 19s     -15? 2.4'       62.4 ER 36.796  22.871  Up 51
> Moon   21h 17m 19s     -15? 2.4'       62.4 ER 36.796  22.871  Up
> Mars   18h 11m 59s     -24? 6.1'       2.431   4.552   56.184  Up 48
> Mars   18h 11m 59s     -24? 6.1'       2.431   4.552   56.184  Up
> Jupiter        20h 3m 35s      -20? 49.4'      6.034   23.867  38.203  Up
>52
> Jupiter        20h 3m 35s      -20? 49.4'      6.034   23.867  38.203  Up
> Saturn 11h 32m 59s     +5? 8.6'        9.018   -47.333 157.471 Set 53
> Saturn 11h 32m 59s     +5? 8.6'        9.018   -47.333 157.471 Set
> Uranus 23h 21m 30s     -4? 57.9'       20.421  48.328  -18.527 Up 53
> Uranus 23h 21m 30s     -4? 57.9'       20.421  48.328  -18.527 Up
> Neptune        21h 39m 30s     -14? 22.8'      30.748  38.963  16.599  Up
>54
> Neptune        21h 39m 30s     -14? 22.8'      30.748  38.963  16.599  Up
> Pluto  18h 4m 34s      -17? 44.5'      32.543  7.443   62.142  Up 50
> Pluto  18h 4m 34s      -17? 44.5'      32.543  7.443   62.142  Up
>
>This is how the data fits with the declarations:
>
>Pluto  : name
>18       ascent_hour
>4        ascent_minute
>34       ascent_second
>-17     dec_degrees
>44.5    dec_mins
>32.543  distance
>0       ER
>7.443   altitude
>62.142  azimuth
>1       UP
>
>Sorry about the longish post, but you asked.


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

Date: Thu, 8 Jan 2009 13:20:45 -0500
From: "~greg" <g_m@remove-comcast.net>
Subject: Re: Parsing a string into a hash
Message-Id: <iqedna52Ds8m3vvUnZ2dnUVZ_g6dnZ2d@giganews.com>


IanW >
> John W. Krahn
>> my %hash = $str =~ /\[(\w+)\]\D*(\d+)/g;
>
> Thanks, I didn't know you could do that. How about if one has more than one string to parse, like:
>
> my $str_A = "Array
> (
>    [SY] => 4
>    [DN] => 2
>    [YO] => 22
> }
>
> my $str_B = "Array
> (
>    [SY] => 3
>    [DN] => 1
>    [YO] => 11
> }
>
> .. AND you wanted to build a master hash so that the key is still the letters within the square brackets but the values go 
> into sub-hashes.
> E.g: you would get a dump something like:
>
> $VAR1 = {
>         SY => {
>                  A => 4,
>                  B => 3
>                 },
>         DN => {
>                  A => 2,
>                  B => 1
>                 },
>         YO => {
>                  A => 22,
>                  B => 11
>                 }
> };
>
> Can each individual string be parsed into %hash in one line? Perhaps this is where map comes in?
>
> I've got nothing against using a loop, it's just I would like to know if it's possible and I might learn something more about 
> parsing strings in the same way I just learnt that technique you gave for a standard hash.
>
> Regards
> Ian

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

use strict;
$|=1;
use Data::Dump qw(dump);

my $str_A = "Array
(
    [SY] => 4
    [DN] => 2
    [YO] => 22
}";

my $str_B = "Array
(
    [SY] => 3
    [DN] => 1
    [YO] => 11
}";

my $VAR_1;

$VAR_1->{$1}{'A'}=$2 while $str_A =~ /\[([A-Z]+)\]\s*=>\s*(\d+)/g;
$VAR_1->{$1}{'B'}=$2 while $str_B =~ /\[([A-Z]+)\]\s*=>\s*(\d+)/g;

dump($VAR_1);

#{
#  DN => { A => 2, B => 1 },
#  SY => { A => 4, B => 3 },
#  YO => { A => 22, B => 11 },
#}









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

Date: Thu, 08 Jan 2009 05:52:46 -0800
From: Pilcrow <pilcrow6@gmail.com>
Subject: please help me to understand this code?
Message-Id: <591cm45aoiuedan0rcf1m5r7kaftd15cjo@4ax.com>

The following is part of faq 4.51.  I am afraid that a good deal of it
is beyond my understanding, and, I suspect, beyond the understaning of a
good many others who read clpm.  It works, but I don't know how. Perhaps
some genius will explain it, line by line, to me and the other dummies.
Perhaps someone will also tell me the source for "The Fischer-Krause
Algorithm", since TAOCP, vol 4 is still unpublished?

Thank you very much, in advance.

~~~~~~~~~~~~~~~~~~~~~~~~~ quote ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here's a little program that generates all permutations of all the words
on each line of input. The algorithm embodied in the "permute()"
function is discussed in Volume 4 (still unpublished) of Knuth's *The
Art of Computer Programming* and will work on any list:

            #!/usr/bin/perl -n
            # Fischer-Krause ordered permutation generator

            sub permute (&@) {
                    my $code = shift;
                    my @idx = 0..$#_;
                    while ( $code->(@_[@idx]) ) {
                            my $p = $#idx;
                            --$p while $idx[$p-1] > $idx[$p];
                            my $q = $p or return;
                            push @idx, reverse splice @idx, $p;
                            ++$q while $idx[$p-1] > $idx[$q];
                            @idx[$p-1,$q]=@idx[$q,$p-1];
                    }
            }

            permute { print "@_\n" } split;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



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

Date: Thu, 08 Jan 2009 13:59:26 -0600
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: please help me to understand this code?
Message-Id: <49665b1e$0$89390$815e3792@news.qwest.net>

Pilcrow wrote:
> The following is part of faq 4.51.  I am afraid that a good deal of it
> is beyond my understanding, and, I suspect, beyond the understaning of a
> good many others who read clpm.  It works, but I don't know how. Perhaps
> some genius will explain it, line by line, to me and the other dummies.

A lot can be discovered on your own by using print and/or
Data::Dumper.  Add a few calls to print or Dumper, to see
various values, to help you see what's happening. If
you have a specific question about a certain line/syntax,
then ask.

> Perhaps someone will also tell me the source for "The Fischer-Krause
> Algorithm", since TAOCP, vol 4 is still unpublished?

Perhaps you can find that on your own using your favorite Internet 
search engine?

Using on returned: "Results 1 - 10 of about 393 for Fischer-Krause 
algorithm.", so there are a lot of possible pages to read.


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

Date: Thu, 08 Jan 2009 13:08:57 -0800
From: Tim Greer <tim@burlyhost.com>
Subject: Re: please help me to understand this code?
Message-Id: <JXt9l.61590$u17.55594@newsfe20.iad>

Pilcrow wrote:

> The following is part of faq 4.51.  I am afraid that a good deal of it
> is beyond my understanding, and, I suspect, beyond the understaning of
> a
> good many others who read clpm.  It works, but I don't know how.
> Perhaps some genius will explain it, line by line, to me and the other
> dummies. Perhaps someone will also tell me the source for "The
> Fischer-Krause Algorithm", since TAOCP, vol 4 is still unpublished?
> 
> Thank you very much, in advance.
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~ quote
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> Here's a little program that generates all permutations of all the
> words on each line of input. The algorithm embodied in the "permute()"
> function is discussed in Volume 4 (still unpublished) of Knuth's *The
> Art of Computer Programming* and will work on any list:
> 
>             #!/usr/bin/perl -n
>             # Fischer-Krause ordered permutation generator
> 
>             sub permute (&@) {
>                     my $code = shift;
>                     my @idx = 0..$#_;
>                     while ( $code->(@_[@idx]) ) {
>                             my $p = $#idx;
>                             --$p while $idx[$p-1] > $idx[$p];
>                             my $q = $p or return;
>                             push @idx, reverse splice @idx, $p;
>                             ++$q while $idx[$p-1] > $idx[$q];
>                             @idx[$p-1,$q]=@idx[$q,$p-1];
>                     }
>             }
> 
>             permute { print "@_\n" } split;
> 
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It says what it does, so I assume you mean the actual code?  If so, what
parts are you not understanding?  What parts _do_ you understand? 
Knowing this will be helpful to giving you the best answer, without
anyone having to go into great detail or explain every aspect (since if
you didn't know any of it, it probably wouldn't do you any good to have
someone explain it when it comes down to it).  That is, you must have
looked for or saw this code somewhere and wanted to use it, so you must
have some idea of what it does?
-- 
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!


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

Date: Thu, 8 Jan 2009 18:22:50 +0100
From: "Börni" <no@spam.com>
Subject: Regular Expression Question
Message-Id: <gk5crn$k5$1@atlas.ip-plus.net>

Hi

This is probably very easy, but I don't get it.

Example:
#!perl -w
use strict;

my $string = '<meta name="Keywords" content="" lang="fr">';

my ($keywords) =  $string =~ /.*?meta name="Keywords".*?content="(.*?)">/;

print "[$keywords]\n";
exit 0;


In the Example above I'd expect $keywords to be empty. Instead it is [" 
lang="fr].

What is the correct expression to match everything
<meta name="Keywords" content="-->IN HERE<--" lang="fr">
even when it's empty?

Regards Bernard 




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

Date: Thu, 08 Jan 2009 09:45:40 -0800
From: Tim Greer <tim@burlyhost.com>
Subject: Re: Regular Expression Question
Message-Id: <9Zq9l.75714$lX6.7415@newsfe06.iad>

Börni wrote:

> Hi
> 
> This is probably very easy, but I don't get it.
> 
> Example:
> #!perl -w
> use strict;
> 
> my $string = '<meta name="Keywords" content="" lang="fr">';
> 
> my ($keywords) =  $string =~ /.*?meta
> name="Keywords".*?content="(.*?)">/;
> 
> print "[$keywords]\n";
> exit 0;
> 
> 
> In the Example above I'd expect $keywords to be empty. Instead it is
> [" lang="fr].
> 
> What is the correct expression to match everything
> <meta name="Keywords" content="-->IN HERE<--" lang="fr">
> even when it's empty?
> 
> Regards Bernard

In your above code, it is doing exactly what it should.  Using your
current example, make the following change:

my ($keywords) = $string =~ /^.*?meta
name="Keywords".*?content="([^"]*)"/;

That will take zero or more characters in content="" and anything from
the opening double quote to the closing double quote, which is not a
double quote itself, will be what $keywords is.  You could probably
just write that as: my ($keywords) = $string
=~ /^.*?content="([^"]*)"/; if that's what you want to stick with. 
Notice I've added the start of the string with ^ in my examples.  If
it's not going to be the start of the string in real code, just adjust
accordingly.
-- 
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!


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

Date: Thu, 08 Jan 2009 18:07:57 GMT
From: hymie@lactose.homelinux.net (hymie!)
Subject: Re: Regular Expression Question
Message-Id: <1ir9l.33850$Io6.23508@newsfe23.iad>

In our last episode, the evil Dr. Lacto had captured our hero,
  "Börni" <no@spam.com>, who said:

>my $string = '<meta name="Keywords" content="" lang="fr">';
>my ($keywords) =  $string =~ /.*?meta name="Keywords".*?content="(.*?)">/;
>print "[$keywords]\n";

>In the Example above I'd expect $keywords to be empty. Instead it is [" 
>lang="fr].

Your regular expression is forcing the match to include the closing > .

If you just want to match up to the next close-quote , then specificy
your regular expression as such.

--hymie!    http://lactose.homelinux.net/~hymie    hymie@lactose.homelinux.net
------------------------ Without caffeine for 800 days ------------------------


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

Date: Thu, 08 Jan 2009 14:28:54 -0500
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: Regular Expression Question
Message-Id: <86hc494mx5.fsf@mithril.chromatico.net>

>>>>> "B" == Börni  <no@spam.com> writes:

    B> Hi This is probably very easy, but I don't get it.

That's because you're using regular expressions to parse HTML.

You will save yourself considerable pain if you use a parser, such as
HTML::Parser, to parse HTML.

Charlton


-- 
Charlton Wilbur
cwilbur@chromatico.net


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

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:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#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 2105
***************************************


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