Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Re: Re: Producing 2 lists from a grep call

by BrowserUk (Patriarch)
on Jun 16, 2002 at 23:23 UTC ( [id://174991]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Producing 2 lists from a grep call
in thread Producing 2 lists from a grep call

I quite like that one. I came close to that but missed (actually, simply wasn't aware of) the @{\@array) 'trick'.

However (you knew that was coming right?), as I can do:

my (@a1,@a2) = ([1,2,3],[3,2,1]);

Update: the above line doesn't work after all

I had tried:

my (@a1,@a2) = ([1,2,3,4,5,6,7],[7,6,5,4,3,2,1]); print Dumper(@a1), Dumper(@a2);

which gave:

$VAR1 = [ 1, 2, 3, 4, 5, 6, 7 ]; $VAR2 = [ 7, 6, 5, 4, 3, 2, 1 ];
which certainly lookedlike it had worked! It was only after seeing this that I went back and added a couple of labels

print "a1\n", Dumper(@a1), "a2\n", Dumper(@a2);

That I saw what tstock meant below.

Thanks tstock, I'll be more careful in future with my quick tests. (I agree about the loop/if being better too.)

end of update

Which personally I find a very clear yet concise way of declaring and initialising two arrays. It still seems as if something close to:

my ( @dirs, @files)= ( @{-d}, @{-f} ) for (readdir DIR);

could be possible if I could only get the syntactic sugar right!

This is only an exercise in my trying to understand arrays and list contexts etc. ie. Its essentially a purely academic excercise for late (for me) on a Sunday evening, and so not worthy of anyone's time unless they are also in play mode.

Replies are listed 'Best First'.
Re: Re: Re: Re: Producing 2 lists from a grep call
by tstock (Curate) on Jun 16, 2002 at 23:40 UTC
    However (you knew that was coming right?), as I can do:
    my (@a1,@a2) = ([1,2,3],[3,2,1]);

    This doesn't work, sorry. You get @a1 with two array references and @a2 undefined.

    tstock

    Update : This is your one liner: (someone please -- me down for posting this)
    map { ($_ ne '.') && ($_ ne '..') && push @{ -d "$base/$_" ? \@dirs : \@files }, $_ } (readdir DIR);
    I think we can agree that the FOR LOOP/IF STATEMENT is probably a nicer solution ? :)
      I think we can agree that the FOR LOOP/IF STATEMENT is probably a nicer solution ? :)
      Especially since it's almost the for loop I posted - just s/map/for/, pull the (readdir DIR) up top, slip a next into the loop, and there you have it.

      If you actually want something that's not a for loop in disguise, you could pull a really horrible trick like
      my @dir; my @files = map { ($_ ne '.') && ($_ ne '..') && -d "$base/$_" ? do { push @dir, $_; () } : -f "$base/$_" && $_ } readdir DIR;
      But if the Communications Decency Act had gone through I wouldn't have been able to even post this.

      Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://174991]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-19 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found