[30161] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1404 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Mar 29 14:09:36 2008

Date: Sat, 29 Mar 2008 11:09:06 -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           Sat, 29 Mar 2008     Volume: 11 Number: 1404

Today's topics:
    Re: empty variables - getting rid of "uninitialized val <tch@nospam.syneticon.net>
    Re: FAQ 8.1 How do I find out which operating system I' <ben@morrow.me.uk>
    Re: Memory issues <jm@nospam.fr>
    Re: Memory issues <jurgenex@hotmail.com>
    Re: Memory issues <jm@nospam.fr>
    Re: Memory issues <smallpond@juno.com>
    Re: Readline using foreach and while <szrRE@szromanMO.comVE>
    Re: Readline using foreach and while <uri@stemsystems.com>
    Re: Readline using foreach and while <szrRE@szromanMO.comVE>
    Re: Readline using foreach and while <uri@stemsystems.com>
    Re: Readline using foreach and while <szrRE@szromanMO.comVE>
    Re: Readline using foreach and while <1usa@llenroc.ude.invalid>
    Re: Readline using foreach and while <uri@stemsystems.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 29 Mar 2008 16:41:45 +0100
From: Tomasz Chmielewski <tch@nospam.syneticon.net>
Subject: Re: empty variables - getting rid of "uninitialized value" warnings?
Message-Id: <fslnvp$a2t$1@online.de>

Gunnar Hjalmarsson schrieb:
> Thrill5 wrote:
>> "Tomasz Chmielewski" <tch@nospam.syneticon.net> wrote in message 
>> news:fsie2l$g0f$1@online.de...
>>> How can I get rid of warnings if I make tests with "if" and some 
>>> variables are empty?
>>>
>>> Should I just ignore it? Or use "no warnings" just for that piece of 
>>> code throwing a warning?
>>
>> Try:
>> use strict;
>> use warnings;
>> no warnings 'uninitialized';
>>
>> This will turn off only the uninitialized variable warnings, and keep 
>> all the other warnings.  I find those warnings are more trouble to get 
>> rid of then the value that the warning provides.
> 
> Even if that may be true in some cases, it's not true in this case IMO. 
> If you write code that does not generate such warnings, you increase the 
> chance that there are no bugs. The OP has already received a few 
> suggestions.
> 
> Also, if you want to disable 'uninitialized' warnings, you'd better do 
> so in the block(s) where it's needed, and not disable them for the whole 
> program.

Thanks all in this thread for useful suggestions!


-- 
Tomasz Chmielewski
http://wpkg.org




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

Date: Sat, 29 Mar 2008 16:38:51 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: FAQ 8.1 How do I find out which operating system I'm running under?
Message-Id: <rt72c5-362.ln1@osiris.mauzo.dyndns.org>


Quoth jm <jm@nospam.fr>:
> 
> > 8.1: How do I find out which operating system I'm running under?
> > 
> >     The $^O variable ($OSNAME if you use English) contains an indication of
> >     the name of the operating system (not its release number) that your perl
> >     binary was built for.
> 
> Is cygwin an operating system (according to perlport)?

According to $^O, yes, it is.

> And how can I know what kind of perl I'm running under?
> 
> strawberryperl?

Vanilla/Strawberry Perl are no different from a perl built by hand using
MinGW. That's sort-of the point, actually... You can check for
$Config::Config{cc} eq 'gcc' if you need to know this, but be aware
ActivePerl fakes its %Config if you have gcc installed.

> activeperl?

ActivePerl defines the builtin Win32::BuildNumber, so you can test for
ActivePerl with defined &Win32::BuildNumber.

> cygwinperl?

$^O eq 'cygwin'

Ben



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

Date: Sat, 29 Mar 2008 16:12:18 +0100
From: jm <jm@nospam.fr>
Subject: Re: Memory issues
Message-Id: <47ee5c52$0$21146$7a628cd7@news.club-internet.fr>

smallpond a écrit :
> On Mar 29, 10:10 am, jm <j...@nospam.fr> wrote:
>> smallpond a écrit :
>>
>>> On Mar 29, 8:45 am, jm <j...@nospam.fr> wrote:
>>>> Based on the fact that perl contains many memory leaks,
>>>> A universal way to measure how many memory is malloced is required.
>>>> Is there standard way to measure how many memory a process has
>>>> allacated, which run with cygwin perl, active perl, and strawberry perl?
>>>> This should help to localize which code makes memory leaks.
>>> perldoc perlfaq3
>>> See:
>>>   How can I make my Perl program take less memory?
>>>   How can I free an array or hash so my program shrinks?
>> It is interesting, but it does not seam to solve my substitution issue.
>>
>> However I does not understand this:
>>
>> «                     Memory allocated to lexicals (i.e. my() variables)
>>        cannot be reclaimed or reused even if they go out of scope. It is
>>        reserved in case the variables come back into scope. Memory allocated
>>        to global variables can be reused (within your program) by using
>>        undef()ing and/or delete(). »
>>
>> Aren't my variables local variables?
>> Why aren't they freed when function terminates?
> 
> 
> sub foo {
>   my $v = 5;
>   return \$v;
> }
> 
> In C, once the function terminates $v is gone and a pointer
> to it will fail.  In perl this reference is legal and the
> space will not be reclaimed.
> 
> In your sample of code above, when you pass a string to a sub,
> perl will make a copy.  If you pass a reference it will not.
> This isn't a memory leak in perl, it's a memory leak in your
> program.

As you suggested, I tried to replace scalar by references, but this does
not look like saving memory (might be 10 Mbytes, I mean just the size of
the main variable):

--- results -------------------------
/tmp$ perl essai.pl  && echo ok
10000001
  PID TTY      STAT   TIME  MAJFL   TRS   DRS   RSS %MEM COMMAND
31679 pts/1    R+     0:00      0  1022 22977 20996  4.0 perl essai.pl

10000001
  PID TTY      STAT   TIME  MAJFL   TRS   DRS   RSS %MEM COMMAND
31679 pts/1    R+     0:38      0  1022 150157 148372 28.7 perl essai.pl

10000001
  PID TTY      STAT   TIME  MAJFL   TRS   DRS   RSS %MEM COMMAND
31679 pts/1    R+     0:50      0  1022 150157 148372 28.7 perl essai.pl

10000001
  PID TTY      STAT   TIME  MAJFL   TRS   DRS   RSS %MEM COMMAND
31679 pts/1    R+     3:53      0  1022 198997 197212 38.1 perl essai.pl

10000001
  PID TTY      STAT   TIME  MAJFL   TRS   DRS   RSS %MEM COMMAND
31679 pts/1    R+     6:55      0  1022 198997 197212 38.1 perl essai.pl

ok


--- scipt ---------------------------
sub aa($)
{
  my ($d) = @_;
  $$d =~ s/x(.....)/$1y/g ;
  $$d =~ s/x(.....)/$1z/g ;
  $$d =~ s/x(.....)/$1a/g ;
  $$d =~ s/x(.....)/$1b/g ;
  $$d =~ s/x(.....)/$1c/g ;
  return $d;
}

sub ab($)
{
  my ($d) = @_;
  $$d =~ s/a(.....)/$1y/g ;
  $$d =~ s/b(.....)/$1z/g ;
  $$d =~ s/c(.....)/$1a/g ;
  $$d =~ s/y(.....)/$1b/g ;
  $$d =~ s/z(.....)/$1c/g ;
  return $d;
}


my $s= 'x' x (1000*1000*10) ;
$s .= "\x{1234}" ;
my $c = \$s;
print length($$c) ."\n" ;
my $v = qx( ps v $$ );
print "$v\n" ;
$c = aa($c);
print length($$c) ."\n" ;
my $v = qx( ps v $$ );
print "$v\n" ;
$c = aa($c);
$c = aa($c);
$c = aa($c);
$c = aa($c);
$c = aa($c);
print length($$c) ."\n" ;
my $v = qx( ps v $$ );
print "$v\n" ;
$c = ab($c);
$c = ab($c);
$c = ab($c);
$c = ab($c);
$c = ab($c);
print length($$c) ."\n" ;
my $v = qx( ps v $$ );
print "$v\n" ;
$c = ab($c);
$c = ab($c);
$c = ab($c);
$c = ab($c);
$c = ab($c);
print length($$c) ."\n" ;
my $v = qx( ps v $$ );
print "$v\n" ;


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

Date: Sat, 29 Mar 2008 15:23:58 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Memory issues
Message-Id: <chnsu3152nnfme18mrtfnj2spgd2akcrp9@4ax.com>

jm <jm@nospam.fr> wrote:
>Joost Diepenmaat a écrit :
>> jm <jm@nospam.fr> writes:
>> 
>>> Based on the fact that perl contains many memory leaks,
>> 
>> It doesn't.
>
>I wrote a sample of code to illustrate the issue.
>
>The code create a 10 mega characters string. this is the only big data
>in this sample.

Which subsequently you copy a few times.

>Then, the main part of the code just modify this data; that mean that
>memory usage should (in my humble opinion) stay near of 10 or 20 (or 40)
>mega bytes.
>
>The main program does not manipulate directly the string, but makes
>functions aa and ab to manipulate this string. Those two functions aa
>and ab just make substitutions within the string.
No, they don't modify the string at all, they modify a _copy_ of the
original string.

>----- Script: ------------------------------------------
>
>
>sub aa($)
>{
>  my ($d) = @_;

And here you create a copy of the original string.

>  $d =~ s/x(.....)/$1y/g ;
>  $d =~ s/x(.....)/$1z/g ;
>  $d =~ s/x(.....)/$1a/g ;
>  $d =~ s/x(.....)/$1b/g ;
>  $d =~ s/x(.....)/$1c/g ;
>  return $d;

You return that copy ...

>}
>
>
>my $c= 'x' x (1000*1000*10) ;
>$c .= "\x{1234}" ;
>print length($c) ."\n" ;
>my $v = qx( ps v $$ );
>print "$v\n" ;
>$c = aa($c);

 ...and you save that copy in $c, such that the memory cannot be reused.

The rest of the code seems to duplicate that action several times using
successively updated versions of the string as function argument, such
that successivly new copies of the string are created.

jue


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

Date: Sat, 29 Mar 2008 16:53:08 +0100
From: jm <jm@nospam.fr>
Subject: Re: Memory issues
Message-Id: <47ee65e4$0$21142$7a628cd7@news.club-internet.fr>



Modifying a little bit again the script, and checking execution with
ltrace, I observed malloc is called 1871 times when free is just called
922 times.
Isn't it an issue?

I just replaced
 my $s= 'x' x (1000*1000*10) ;
by
 my $s= 'x' x (10) ;

and did:


/tmp$ ltrace perl essai.pl  2>&1 | sed 's/(.*//g' | sort | uniq -c |
grep 'malloc\|free'
    922 free
   1871 malloc



> --- scipt ---------------------------
> sub aa($)
> {
>   my ($d) = @_;
>   $$d =~ s/x(.....)/$1y/g ;
>   $$d =~ s/x(.....)/$1z/g ;
>   $$d =~ s/x(.....)/$1a/g ;
>   $$d =~ s/x(.....)/$1b/g ;
>   $$d =~ s/x(.....)/$1c/g ;
>   return $d;
> }
> 
> sub ab($)
> {
>   my ($d) = @_;
>   $$d =~ s/a(.....)/$1y/g ;
>   $$d =~ s/b(.....)/$1z/g ;
>   $$d =~ s/c(.....)/$1a/g ;
>   $$d =~ s/y(.....)/$1b/g ;
>   $$d =~ s/z(.....)/$1c/g ;
>   return $d;
> }
> 
> 
> my $s= 'x' x (1000*1000*10) ;
> $s .= "\x{1234}" ;
> my $c = \$s;
> print length($$c) ."\n" ;
> my $v = qx( ps v $$ );
> print "$v\n" ;
> $c = aa($c);
> print length($$c) ."\n" ;
> my $v = qx( ps v $$ );
> print "$v\n" ;
> $c = aa($c);
> $c = aa($c);
> $c = aa($c);
> $c = aa($c);
> $c = aa($c);
> print length($$c) ."\n" ;
> my $v = qx( ps v $$ );
> print "$v\n" ;
> $c = ab($c);
> $c = ab($c);
> $c = ab($c);
> $c = ab($c);
> $c = ab($c);
> print length($$c) ."\n" ;
> my $v = qx( ps v $$ );
> print "$v\n" ;
> $c = ab($c);
> $c = ab($c);
> $c = ab($c);
> $c = ab($c);
> $c = ab($c);
> print length($$c) ."\n" ;
> my $v = qx( ps v $$ );
> print "$v\n" ;


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

Date: Sat, 29 Mar 2008 08:58:51 -0700 (PDT)
From: smallpond <smallpond@juno.com>
Subject: Re: Memory issues
Message-Id: <6958d732-9556-464a-a9f2-69c2eb2cd937@c65g2000hsa.googlegroups.com>

On Mar 29, 11:12 am, jm <j...@nospam.fr> wrote:
> smallpond a =E9crit :
>
>
>
> > On Mar 29, 10:10 am, jm <j...@nospam.fr> wrote:
> >> smallpond a =E9crit :
>
> >>> On Mar 29, 8:45 am, jm <j...@nospam.fr> wrote:
> >>>> Based on the fact that perl contains many memory leaks,
> >>>> A universal way to measure how many memory is malloced is required.
> >>>> Is there standard way to measure how many memory a process has
> >>>> allacated, which run with cygwin perl, active perl, and strawberry pe=
rl?
> >>>> This should help to localize which code makes memory leaks.
> >>> perldoc perlfaq3
> >>> See:
> >>>   How can I make my Perl program take less memory?
> >>>   How can I free an array or hash so my program shrinks?
> >> It is interesting, but it does not seam to solve my substitution issue.=

>
> >> However I does not understand this:
>
> >> =AB                     Memory allocated to lexicals (i.e. my() variabl=
es)
> >>        cannot be reclaimed or reused even if they go out of scope. It i=
s
> >>        reserved in case the variables come back into scope. Memory allo=
cated
> >>        to global variables can be reused (within your program) by using=

> >>        undef()ing and/or delete(). =BB
>
> >> Aren't my variables local variables?
> >> Why aren't they freed when function terminates?
>
> > sub foo {
> >   my $v =3D 5;
> >   return \$v;
> > }
>
> > In C, once the function terminates $v is gone and a pointer
> > to it will fail.  In perl this reference is legal and the
> > space will not be reclaimed.
>
> > In your sample of code above, when you pass a string to a sub,
> > perl will make a copy.  If you pass a reference it will not.
> > This isn't a memory leak in perl, it's a memory leak in your
> > program.
>
> As you suggested, I tried to replace scalar by references, but this does
> not look like saving memory (might be 10 Mbytes, I mean just the size of
> the main variable):
>



The sub call was just answering your question about
locals.

Each of these:
   $$d =3D~ s/x(.....)/$1a/g ;

is making string copies in $1.   $1 is a persistent
variable.  perl 5.10 has new regex syntax for
avoiding use of $1, $2 etc.


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

Date: Sat, 29 Mar 2008 08:47:34 -0700
From: "szr" <szrRE@szromanMO.comVE>
Subject: Re: Readline using foreach and while
Message-Id: <fsloam01dht@news2.newsguy.com>

Ben Morrow wrote:
> Quoth "szr" <szrRE@szromanMO.comVE>:
>> Ben Morrow wrote:
>>> Quoth "szr" <szrRE@szromanMO.comVE>:
>>>>
>>>> Actually the behaviors of "for (@ary)" and "for (@ary, ())" do seem
>>>> consistant if you really think about it. The resulting list is what
>>>> it iterates over (from the first element, to what ever *count*
>>>> is... in the former case *count* come fro mthe array, and since the
>>>> condition is checked at the start of each iteration, if the array
>>>> is added to, the count is incremented.
>>>>
>>>> In the latter case, a new list is created from contents of @ary +
>>>> an empty list, which gives you a new list, which contains the
>>>> values of @ary, but is a new seperate list, and thus is not
>>>> effected by changes to @ary because it has it's own copy of @ary's
>>>> values.
>>>
>>> OK, now explain to me why
>>>
>>>    my @ary = qw/a b c/;
>>>    print map { /c/ and push @ary, 'd'; $_ } @ary;
>>>
>>> *doesn't* work like that :).
>>
>> Actually it does. The difference is, map doesn't recheck the count
>> every time around like for/foreach do. If you print the contents of
>> @ary after the line with the map statement, it does indeed contain
>> 'd' at the end. This behavior seems to correct, as one would likely
>> expect that the list map returns when it is finished to be the same
>> length as the one /passed/ into map at the start. If you pass a 3
>> element list, you should get back a 3 element list, should you not?
>
> Absolutely not. my %h = map { $_ => 1 } qw/a b c/; is quite a common
> idiom.

Well, you're still getting that many *sets* which is probably what I 
should of said, or have been clearer. In that example, you get 3 set of 
hash pairs resulting from the 3 element list. The point is you get 
count_of_passed_list amount of something from the map, in some form or 
another. How exactly it's returned is determined by the template inside 
the map.

> <snip>
>> $_ is aliased to the current array element just like in for. Again,
>> the only difference I see if that map doesn't recheck the count of
>> the passed list for each iteration.
>
> No, you're misunderstanding the difference between a list and an
> array. Evaluating an array in list context returns a list of its
> elements *as they are now*;

I seem to understand it just fine. What we both said above seems to be 
true. Maybe we're just misunderstanding what the other is trying to say? 
:-)

> under most circumstances, it returns a
> list of aliases to those elements, but any changes to the order of
> the elements in @ary are not propagated into the list. Consider
>
>    my @ary = qw/a b c/;
>    sub foo {
>        my @keep = map "$_", @_; # kill the aliasing

Ok @keep now contains (a, b, c)...

>        unshift @ary, 'h';

@ary, which comes from a scope outside this sub, now contains (h, a, b, 
c)

>        $_[$_] .= $keep[$_] for 0..$#_;

Keep in mind $_[0] *still* points to what used to be the first element 
of @ary. Remember, the aliasing isn't to the *array* but to it's 
*elements*. This is because when you normally pass args to a sub (e.g., 
do_something($x, $y); ), the aliasing is with $x and $y to $_[0] and 
$_[1]. Passing an array just like passing that many scalars as are 
elements in the array; each individual one gets aliased to the next 
sequential element of @_ in the sub's scope.

>    }
>    foo @ary;
>    print for @ary;

Running this (with the last line as `print "$_\n" for @ary;` for 
clarity) prints:

h
aa
bb
cc

Seems the changes propgated just fine to me. You pushed 'h' to the 
beginning of @ary, then you effectively iterated from 
$ary[1]..$ary[$#ary].

Map is the same way in that regard; $_ is an alias to an *element*

>> To me, this behavior is part of what separates map from for/foreach.
>
> It separates for (LIST) from everything else that accepts a LIST.
> This is why I called it 'weird'.

I still don't see what you consider weird about it. What does it do that 
you don't expect it to do?

-- 
szr 




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

Date: Sat, 29 Mar 2008 16:02:37 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Readline using foreach and while
Message-Id: <x7sky9ldr7.fsf@mail.sysarch.com>

>>>>> "s" == szr  <szrRE@szromanMO.comVE> writes:

  >> Absolutely not. my %h = map { $_ => 1 } qw/a b c/; is quite a common
  >> idiom.

  s> Well, you're still getting that many *sets* which is probably what I 
  s> should of said, or have been clearer. In that example, you get 3 set of 
  s> hash pairs resulting from the 3 element list. The point is you get 
  s> count_of_passed_list amount of something from the map, in some form or 
  s> another. How exactly it's returned is determined by the template inside 
  s> the map.

you are still missing the picture. map can return ANY number of elements
(not sets). it executes its expression/block once for each input element
but that can generate 0-?? elements which are appended to the return
list. there are no boundaries in those elements so there are no true
sets. if you return a reference which holds stuff you can force your own
boundaries and make sets. that is also a known map idiom. map generates
a new list from input from another list. the transformation can be
anything and isn't tied to how many input elements there are.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Sat, 29 Mar 2008 09:27:57 -0700
From: "szr" <szrRE@szromanMO.comVE>
Subject: Re: Readline using foreach and while
Message-Id: <fslqmd01gvu@news2.newsguy.com>

Uri Guttman wrote:
>>>>>> "s" == szr  <szrRE@szromanMO.comVE> writes:
>
>  >> Absolutely not. my %h = map { $_ => 1 } qw/a b c/; is quite a
>  common >> idiom.
>
>  s> Well, you're still getting that many *sets* which is probably
>  what I s> should of said, or have been clearer. In that example, you
>  get 3 set of s> hash pairs resulting from the 3 element list. The
>  point is you get s> count_of_passed_list amount of something from
>  the map, in some form or s> another. How exactly it's returned is
>  determined by the template inside s> the map.
>
> you are still missing the picture. map can return ANY number of
> elements (not sets). it executes its expression/block once for each
> input element but that can generate 0-?? elements which are appended
> to the return list.

I understand, but still, whatever is being generated to the LHS of map, 
it's done that many times are there are elements in the list on the RHS. 
Is that fair to say?

> there are no boundaries in those elements so there are no true sets.

I meant "set" in a very generic way. Not in the mathematical sense, but 
in the sense that you get back something that can be group of something 
on the LHS by the number of elements on the RHS of map. The `map { $_ => 
1 }` example gives you that many pairs, for example.

-- 
szr 




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

Date: Sat, 29 Mar 2008 16:51:36 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Readline using foreach and while
Message-Id: <x7ej9tlbhj.fsf@mail.sysarch.com>

>>>>> "s" == szr  <szrRE@szromanMO.comVE> writes:

  >> you are still missing the picture. map can return ANY number of
  >> elements (not sets). it executes its expression/block once for each
  >> input element but that can generate 0-?? elements which are appended
  >> to the return list.

  s> I understand, but still, whatever is being generated to the LHS of map, 
  s> it's done that many times are there are elements in the list on the RHS. 
  s> Is that fair to say?

no, i wouldn't say what is being generated. just what i previously said
happens. the CODE in the expression/block is executed ONCE for each
input element. then the collected list of those is returned. use the
correct terminology and you will get it easier. 

  >> there are no boundaries in those elements so there are no true sets.

  s> I meant "set" in a very generic way. Not in the mathematical sense, but 
  s> in the sense that you get back something that can be group of something 
  s> on the LHS by the number of elements on the RHS of map. The `map { $_ => 
  s> 1 }` example gives you that many pairs, for example.

but there are no 'pairs' in the list except by the context of assigning
the list to a hash. there is just a list of elements created by the
map. map doesn't know or care what happens to its generated list. so
calling them pairs is out of context. and as such there are no
boundaries (which pairs implies). map can generate ANY sort of list you
want. no sets or pairs or groups need to be created. a single list is
what you get and all you should care about. how you use that list is up
to the next part of the expression.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Sat, 29 Mar 2008 10:06:44 -0700
From: "szr" <szrRE@szromanMO.comVE>
Subject: Re: Readline using foreach and while
Message-Id: <fslsv401kks@news2.newsguy.com>

(Please use proper capitalization and such, it will make your replies 
easier to read.)

Uri Guttman wrote:
>>>>>> "s" == szr  <szrRE@szromanMO.comVE> writes:
>
>  >> you are still missing the picture. map can return ANY number of
>  >> elements (not sets). it executes its expression/block once for
>  each >> input element but that can generate 0-?? elements which are
>  appended >> to the return list.
>
>  s> I understand, but still, whatever is being generated to the LHS
>  of map, s> it's done that many times are there are elements in the
>  list on the RHS. s> Is that fair to say?
>
> no, i wouldn't say what is being generated. just what i previously
> said happens. the CODE in the expression/block is executed ONCE for
> each input element. then the collected list of those is returned.
> use the correct terminology and you will get it easier.

Well that's basically how I understand it. In fact it seems you 
basically restated the same thing in a different way. 1) the expr/block 
is executed once per cycle. Yep. 2) The end result is something that is 
a grouping, whether singular (say, a simple list) or more complex (like, 
say, a hash key/value pair), of the same amount of elements in the input 
list.

I know using proper terminology is important, but I think understand 
meaning as someone put it into their own words is also an important 
skill. This is something I have to do quite frequently in my line of 
work when dealing with many kinds of people regarding projects I work 
on. Not all of them are as savvy with programming (or a specific 
language like Perl) and often I have to use simpler terms to describe 
things.

>  >> there are no boundaries in those elements so there are no true
> sets.
>
>  s> I meant "set" in a very generic way. Not in the mathematical
>  sense, but s> in the sense that you get back something that can be
>  group of something s> on the LHS by the number of elements on the
>  RHS of map. The `map { $_ => s> 1 }` example gives you that many
> pairs, for example.
>
> but there are no 'pairs' in the list except by the context of
> assigning the list to a hash.

I'm sorry, but I really have to disagree here. A hash is essentially a 
list divided into pairs (keys and values, respectively), and `map { $_ 
=> s> 1 }` is creating such a pair in a statement like that.

-- 
szr 




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

Date: Sat, 29 Mar 2008 17:15:34 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Readline using foreach and while
Message-Id: <Xns9A7086DFAF577asu1cornelledu@127.0.0.1>

"szr" <szrRE@szromanMO.comVE> wrote in
news:fslqmd01gvu@news2.newsguy.com: 

> Uri Guttman wrote:
>>>>>>> "s" == szr  <szrRE@szromanMO.comVE> writes:
>>
>>  >> Absolutely not. my %h = map { $_ => 1 } qw/a b c/; is quite a
>>  common >> idiom.
>>
>>  s> Well, you're still getting that many *sets* which is probably
>>  what I s> should of said, or have been clearer. In that example,
>>  you get 3 set of s> hash pairs resulting from the 3 element
>>  list. The point is you get s> count_of_passed_list amount of
>>  something from the map, in some form or s> another. How exactly
>>  it's returned is determined by the template inside s> the map.
>>
>> you are still missing the picture. map can return ANY number of
>> elements (not sets). it executes its expression/block once for
>> each input element but that can generate 0-?? elements which are
>> appended to the return list.
> 
> I understand, but still, whatever is being generated to the LHS of
> map, it's done that many times are there are elements in the list
> on the RHS. Is that fair to say?

If I understand you correctly, yeah, that's fine to say. 

On the other hand, you might find the following example instructive:

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my @result = map { split // } ( '5' x rand(10) ) ;

print Dumper \@result;

__END__

Note that, the RHS is always a single element list. On the other hand, 
the number of elements in the result of map is variable. So, yes, the 
operation inside the block is only carried out once, what you are 
saying is fair in that sense, but the map operation can change the 
length of the list in non-simplistic ways:

E:\Home\asu1\Src\Test> t1.pl
$VAR1 = [];

E:\Home\asu1\Src\Test> t1.pl
$VAR1 = [
          '5',
          '5',
          '5',
          '5',
          '5',
          '5',
          '5'
        ];

E:\Home\asu1\Src\Test> t1.pl
$VAR1 = [
          '5',
          '5',
          '5',
          '5'
        ];

Sinan


-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/


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

Date: Sat, 29 Mar 2008 17:29:00 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Readline using foreach and while
Message-Id: <x7zlshjv6r.fsf@mail.sysarch.com>

>>>>> "s" == szr  <szrRE@szromanMO.comVE> writes:

  s> Well that's basically how I understand it. In fact it seems you 
  s> basically restated the same thing in a different way. 1) the expr/block 
  s> is executed once per cycle. Yep. 2) The end result is something that is 
  s> a grouping, whether singular (say, a simple list) or more complex (like, 
  s> say, a hash key/value pair), of the same amount of elements in the input 
  s> list.


you keep saying grouping and that is the wrong way to think about
it. drop that word from this concept. map generates ONE list. how it
gets there is irrelevent so there is no grouping.

  >> but there are no 'pairs' in the list except by the context of
  >> assigning the list to a hash.

  s> I'm sorry, but I really have to disagree here. A hash is essentially a 
  s> list divided into pairs (keys and values, respectively), and `map { $_ 
  s> => s> 1 }` is creating such a pair in a statement like that.
no. a hash creates the pairs when it is assigned a list. the map just
generates a list with no real 'pairs'. you may think they are there but
they are not pairs yet. you have to separate what the map does from what
the hash expects.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

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


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