[28065] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 9429 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jul 6 14:06:00 2006

Date: Thu, 6 Jul 2006 11:05:08 -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           Thu, 6 Jul 2006     Volume: 10 Number: 9429

Today's topics:
    Re: date format <bart@nijlen.com>
    Re: date format <someone@example.com>
        Get the reference to an array from a function... <howachen@gmail.com>
    Re: Get the reference to an array from a function... <aukjan@gmail.com>
    Re: Get the reference to an array from a function... <aukjan@gmail.com>
    Re: Get the reference to an array from a function... <howachen@gmail.com>
    Re: Get the reference to an array from a function... <someone@example.com>
    Re: Get the reference to an array from a function... <howachen@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 6 Jul 2006 01:01:12 -0700
From: "Bart Van der Donck" <bart@nijlen.com>
Subject: Re: date format
Message-Id: <1152172868.858828.53950@b68g2000cwa.googlegroups.com>

John W. Krahn wrote:

> [...]
> A lot of date formats use a leading space for single digit days so splitting
> on a single space character will give you an off-by-one error on the day and
> year fields.

Yes, I think I remember there was something tricky with it, that's why
I did  sprintf('%02g',$ar[4]);  in stead of just print  $ar[4]  in my
program. Thanks for pointing that out, the solution is simple:

  my @ar = split / +/, (split /\n/, $file)[0];

>> $ar[3] =~ s/$m[$_]/@{[sprintf('%02g',$_+1)]}/ for (0..$#m);
>
> Is there a good reason to use a floating-point format ('g') instead of an
> integer format ('i', 'd' or 'u')?

Well, it's there to guarantee the two-digit MM (month) format. I don't
see any drawbacks for that here, but maybe I'm not fully aware of the
exact finetunings of this construction.

>> die "Can't find month '$ar[3]'\n" unless $ar[3]=~/^(0|1)[0-9]$/;
>
> Why use an alternation with capturing parentheses instead of a character
> class?  Where do you use the value stored in $1?  So you are saying that 19 is
> a valid month?

You're right, something like

  die "..." unless $ar[3]=~ /^0[1-9]$|^1[0-2]$/;

should do.

Thanks for your comment. I learnt from it.

-- 
 Bart



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

Date: Thu, 06 Jul 2006 11:46:54 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: date format
Message-Id: <OK6rg.104745$A8.91205@clgrps12>

Bart Van der Donck wrote:
> John W. Krahn wrote:
> 
>>>$ar[3] =~ s/$m[$_]/@{[sprintf('%02g',$_+1)]}/ for (0..$#m);
>>Is there a good reason to use a floating-point format ('g') instead of an
>>integer format ('i', 'd' or 'u')?
> 
> Well, it's there to guarantee the two-digit MM (month) format. I don't
> see any drawbacks for that here, but maybe I'm not fully aware of the
> exact finetunings of this construction.

The array index (0..$#m) will always be an integer.  The underlying C library
uses type promotion so the integer will be converted to a double
floating-point number in order to convert it to a string.  If the integer is
large enough it will be converted using scientific notation:

$ perl -le'print for $^T, sprintf( q[%02g], $^T ), sprintf q[%02d], $^T'
1152186353
1.15219e+09
1152186353


John
-- 
use Perl;
program
fulfillment


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

Date: 6 Jul 2006 01:45:51 -0700
From: "howa" <howachen@gmail.com>
Subject: Get the reference to an array from a function...
Message-Id: <1152175551.395194.49440@m73g2000cwd.googlegroups.com>

hi,

consider the program, why reference to an array returning from a
function did not work?


#------------------------------

sub test {

	my @arr = ("1", "2", "3");
	return @arr;
}

my $size = test();
my @arr = test();

my $arr_ref = \test();

use Data::Dumper;
#print Dumper ( $size );
#print Dumper ( @arr );
print Dumper ( $arr_ref ); # DIDN'T WORK!



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

Date: Thu, 06 Jul 2006 11:02:21 +0200
From: Aukjan van Belkum <aukjan@gmail.com>
Subject: Re: Get the reference to an array from a function...
Message-Id: <b5fc9$44acd17a$c2abfc64$28381@news2.tudelft.nl>

howa wrote:
> hi,
> 
> consider the program, why reference to an array returning from a
> function did not work?
>
> 
> my $arr_ref = \test();

You should change this line to:

my $arr_ref = [ test() ];

This will force a list of values returned by test into an array 
reference.  Using \test() will actually do nothing, and if you would 
have written it as:

my $arr_ref = \&test();

You would have created a reference to the subroutine 'test';

Aukjan


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

Date: Thu, 06 Jul 2006 11:06:16 +0200
From: Aukjan van Belkum <aukjan@gmail.com>
Subject: Re: Get the reference to an array from a function...
Message-Id: <5fd39$44acd265$c2abfc64$28381@news2.tudelft.nl>

Aukjan van Belkum wrote:
> Using \test() will actually do nothing, 

To correct myself... "my $ref = \test()" will actually return a 
reference to the last returned value in the list.

check: "perldoc perlref" for more info

Aukjan


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

Date: 6 Jul 2006 03:09:01 -0700
From: "howa" <howachen@gmail.com>
Subject: Re: Get the reference to an array from a function...
Message-Id: <1152180541.658751.77920@75g2000cwc.googlegroups.com>


Aukjan van Belkum =E5=AF=AB=E9=81=93=EF=BC=9A

> howa wrote:
> > hi,
> >
> > consider the program, why reference to an array returning from a
> > function did not work?
> >
> >
> > my $arr_ref =3D \test();
>
> You should change this line to:
>
> my $arr_ref =3D [ test() ];
>
> This will force a list of values returned by test into an array
> reference.  Using \test() will actually do nothing, and if you would
> have written it as:
>
> my $arr_ref =3D \&test();
>
> You would have created a reference to the subroutine 'test';
>=20
> Aukjan

Thanks!



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

Date: Thu, 06 Jul 2006 11:16:25 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Get the reference to an array from a function...
Message-Id: <di6rg.104740$A8.5373@clgrps12>

Aukjan van Belkum wrote:
> Aukjan van Belkum wrote:
>> Using \test() will actually do nothing, 
> 
> To correct myself... "my $ref = \test()" will actually return a
> reference to the last returned value in the list.

test() returns a list and \test() will return a list of references to the
items in the original list.  Only the last item will be stored in the scalar
because that is the way that scalar assignment works with a list.


John
-- 
use Perl;
program
fulfillment


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

Date: 6 Jul 2006 09:41:42 -0700
From: "howa" <howachen@gmail.com>
Subject: Re: Get the reference to an array from a function...
Message-Id: <1152204100.609651.295610@j8g2000cwa.googlegroups.com>


John W. Krahn =E5=AF=AB=E9=81=93=EF=BC=9A

> Aukjan van Belkum wrote:
> > Aukjan van Belkum wrote:
> >> Using \test() will actually do nothing,
> >
> > To correct myself... "my $ref =3D \test()" will actually return a
> > reference to the last returned value in the list.
>
> test() returns a list and \test() will return a list of references to the
> items in the original list.  Only the last item will be stored in the sca=
lar
> because that is the way that scalar assignment works with a list.
>

in fact, what does "\test()" means as it is not the reference to the
list returning from the function?

>>\test() will return a list of references to the items in the original lis=
t=2E



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

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 V10 Issue 9429
***************************************


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