Click here to get back home

Problem expanding filenames in loop

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Problem expanding filenames in loop Hemant 06-23-2008
Get Chitika Premium
Posted by Ben Morrow on June 24, 2008, 12:53 pm
Please log in for more thread options

Quoth xhoster@gmail.com:
> >
> > or, if you need a 'scalar',
> >
> > print scalar( () = glob(...) );
> >
> > I have no idea where this is documented, but if you assign to the empty
> > list in scalar context it returns a count of the items in the assigned
> > list.
>
> Not just an empty list.
>
> perl -le 'print scalar(my ($x,$z) = glob q(/*));'
> 24

Huh. I had it in my head that that returned a count of the LHS, not the
RHS. Don't know where I got that from...

> > This is weird and not at all the way any other list assignment
> > works, but surprisingly useful.

...so, in fact, exactly like every other list assignment :).

Thanks,
Ben

--
Many users now operate their own computers day in and day out on various
applications without ever writing a program. Indeed, many of these users
cannot write new programs for their machines...
-- F.P. Brooks, 'No Silver Bullet', 1987 [ben@morrow.me.uk]

Posted by comp.llang.perl.moderated on June 24, 2008, 3:46 pm
Please log in for more thread options
> Quoth xhos...@gmail.com:
>
>
> > > or, if you need a 'scalar',
>
> > > print scalar( () = glob(...) );
>
> > > I have no idea where this is documented, but if you assign to the empty
> > > list in scalar context it returns a count of the items in the assigned
> > > list.
>
> > Not just an empty list.
>
> > perl -le 'print scalar(my ($x,$z) = glob q(/*));'
> > 24
>
> Huh. I had it in my head that that returned a count of the LHS, not the
> RHS. Don't know where I got that from...
>
> > > This is weird and not at all the way any other list assignment
> > > works, but surprisingly useful.
>
> ...so, in fact, exactly like every other list assignment :).
>

Even with the list, it almost looks like Perl actually creates some
temp AV just to later grab its size perhaps...?

perl -MO=Concise -le 'print scalar( ($x,$z)=(1,2,3))'
...
aassign[t1] sKS ->c
...

--
Charles DeRykus

Posted by Eric Pozharski on June 24, 2008, 3:42 pm
Please log in for more thread options

*SKIP*
>> As of Perl 5.8.8 (for Debian Etch) it makes no difference:

> You're testing the wrong thing... which is not surprising, since where
> Perl keeps its implicit iterators is never very clear.

That's the second time I see you suppose some magic behind interpreter.
I like it. I suppose too.

> Scalar glob keeps its iterator *in the op*: that is, each instance of

[ I'm sorry about that thread. It was based on my sole misunderstanding
what are (differences among) lists, arrays, assignments in scalar and
list context; and incomplete process of familarizing myself with
precedence and associativity. Now I feel much beter. ]
*SKIP*

>> 10:24:07 34 [0:0]$ perl -e 'print scalar(@{[ glob q(/*) ]}) > 1; print
>> "\n"'

> This works correctly, but again is deeply ugly.

> The idiom you are looking for is

> my $x = () = glob(...);

> or, if you need a 'scalar',

> print scalar( () = glob(...) );

That's what I missed (yes, I admit missing I<-l> and B<I<-w>>). To put
it clear:

22:16:07 143 [0:0]$ perl -le '$x = (1,2,4); print $x'
4
22:16:14 144 [0:0]$ perl -le '($x) = (1,2,4); print $x'
1
22:16:18 145 [0:0]$ perl -le '$x = () = (1,2,4); print $x'
3

The 1st is a B<comma operator>, I know. The 3rd slowly moves down to
backbone where it will stay forever. What surprises me most is the 2nd.
A long time ago, when I've just started the addiction to Perl, I was
told (OK, I've read), that intentional way to do that is
C<($x, undef) = (1, 2, 3)>; either way (C<($x) = (1, 2, 3)>) will set
I<$x> to the number of items in list. It's not so. Any more?

I've just checked, replacing the list with explicit array merges 1st and
3rd cases. And has no influence on the 2nd. Obviously, I've missed
some reading. What?

> I have no idea where this is documented, but if you assign to the empty
> list in scalar context it returns a count of the items in the assigned
> list. This is weird and not at all the way any other list assignment
> works, but surprisingly useful.

L<perlop/"Assignment operators">. In the last paragraph it's mentioned;
no explicit example though.

--
Torvalds' goal for Linux is very simple: World Domination

Similar ThreadsPosted
Problem with glob and filenames containing '[' and ']' September 27, 2006, 7:33 am
Help on expanding variable from sh May 2, 2007, 3:36 pm
Expanding tree paths July 19, 2005, 10:48 am
Problem with loop control LAST exiting prematurely March 18, 2008, 6:22 am
How do you match 25 filenames? May 11, 2005, 7:51 am
using -e with array of filenames May 24, 2005, 3:19 pm
How to use a textfile for filenames?? February 12, 2006, 11:16 pm
utf8 filenames April 9, 2006, 8:39 pm
Can't change filenames..why? April 3, 2007, 8:39 pm
how to read Chinese filenames? June 5, 2006, 5:49 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap