CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
A smarter way of doing...

 



mwaf
Deleted

Jul 9, 2000, 11:29 PM

Post #1 of 7 (1132 views)
A smarter way of doing... Can't Post

...this:


@asdf = ($asdf[0],$asdf[1],$asdf[2]);


Jasmine
Administrator / Moderator

Jul 10, 2000, 5:15 AM

Post #2 of 7 (1132 views)
Re: A smarter way of doing... [In reply to] Can't Post

Basically, you can extract one or more elements of a particular array using an array slice.

Let's say you have an array like this:

@array = qw(a b c d e f g h i j);

And you want to use array elements 1, 3, 5 and 7. Here's how you could extract just those elements without using $array[1], $array[3], etc:

@array2 = @array[1,3,5,7]; # @array2 contains b d f h

You can also use the range (..) operator to define a start and end point for the slice.

@array2 = @array[0..3]; # @array2 contains a b c d

You can also mix and match...

@array2 = @array[0, 4..6]; # @array2 contains a e f g

Hope this helps!


mwaf
Deleted

Jul 10, 2000, 6:57 AM

Post #3 of 7 (1132 views)
Re: A smarter way of doing... [In reply to] Can't Post

thanks!


dws
Deleted

Jul 12, 2000, 10:43 PM

Post #4 of 7 (1132 views)
Re: A smarter way of doing... [In reply to] Can't Post

@S{map {"d$_"} 1..22} = (1) x 22;


Kanji
User / Moderator

Jul 12, 2000, 10:46 PM

Post #5 of 7 (1132 views)
Re: A smarter way of doing... [In reply to] Can't Post

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

for $n ( 0..22 ) {
$S{ "d" . $n } = 1;
}</pre><HR></BLOCKQUOTE>

Also, if don't have a specific need for the 'd' prefix, why not use $S{ $n }? Be a lot more simple to read. : )


mwaf
Deleted

Jul 13, 2000, 9:31 AM

Post #6 of 7 (1132 views)
Re: A smarter way of doing... [In reply to] Can't Post

...this:


$S{d1} = 1;
$S{d2} = 1;
$S{d3} = 1;
$S{d4} = 1;
$S{d5} = 1;
$S{d6} = 1;
$S{d7} = 1;
$S{d8} = 1;
$S{d9} = 1;
(until 22)

I tried;
for $n (0..22) { $S{d$n} = 1; }
but it (as you might have guessed) didn't work


mwaf
Deleted

Jul 14, 2000, 10:22 AM

Post #7 of 7 (1132 views)
Re: A smarter way of doing... [In reply to] Can't Post

thanks, I'll give it a try

and I do have a specific need for the 'd'

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives