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: Intermediate:
How would I go about..

 



cypher_MS
Novice

Aug 30, 2002, 7:12 PM

Post #1 of 13 (1175 views)
How would I go about.. Can't Post

ok, heres the info;

@ids=("01","02","12","13","541","54","1002")

I need these 7 elements broken up into 2 arrays, split up every 5 elements like so:

@page1=("01","02","12","13","541");
@page2=("54","1002");

Yet, I also need it to function when there are about 150 elements in the "ids" array, simply put, i just need every five put into a new array..
---
C:\Dos
C:\Dos\Run
Run Dos Run
---


davorg
Thaumaturge / Moderator

Aug 31, 2002, 12:51 AM

Post #2 of 13 (1168 views)
Re: [cypher_MS] How would I go about.. [In reply to] Can't Post

If you don't know how many arrays you're going to need then its difficult to pre-declare them, so it's probably better to create a two dimensional array(*)

This code is destructive to your original array, so you might want to take a copy before you start.

Code
my @pages; 
while (my @page = splice @ids, 0, 5) {
push @pages, [@page];
}


If what you're doing is creating pages of data, then you might be better off using the Data::Page module from CPAN.

(*) Ok, I know you can't _actually_ create a 2D array in Perl. What this code really does is to create an array of array references, which is effectively the same thing.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


cypher_MS
Novice

Sep 1, 2002, 8:41 AM

Post #3 of 13 (1163 views)
Re: [davorg] How would I go about.. [In reply to] Can't Post

How would i get the next five values to get put into another array? like @page1 and @page2?
---
C:\Dos
C:\Dos\Run
Run Dos Run
---


thebitch
User

Sep 2, 2002, 10:53 PM

Post #4 of 13 (1152 views)
Re: [cypher_MS] How would I go about.. [In reply to] Can't Post

why would you want to do that?

any time you even remotely think about
doint something like that,
you need to stop yourself,
and decide on a complex data structure,
like an AoA, or a HoA
(A being array(s) and H being hash and o being of )


davorg
Thaumaturge / Moderator

Sep 3, 2002, 1:00 AM

Post #5 of 13 (1151 views)
Re: [cypher_MS] How would I go about.. [In reply to] Can't Post


In Reply To
How would i get the next five values to get put into another array? like @page1 and @page2?


You don't need to do that. All of your data is in @pages.

The first page is in $pages[0], the second one is in $pages[1], etc.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


cypher_MS
Novice

Sep 3, 2002, 11:59 AM

Post #6 of 13 (1145 views)
Re: [davorg] How would I go about.. [In reply to] Can't Post

Basically I want to have 5 items displayed per page, so that way if they request page 1 i can just read which items are to be displayed on that page, and so forth..

That is why Im trying to get it into @page1, @page2, @page3, etc.

Is there any other ::Easier:: way to do this?? Maybe I'm just retarded..
---
C:\Dos
C:\Dos\Run
Run Dos Run
---


davorg
Thaumaturge / Moderator

Sep 4, 2002, 12:54 AM

Post #7 of 13 (1141 views)
Re: [cypher_MS] How would I go about.. [In reply to] Can't Post

I'm sorry, but I don't understand why my solution doesn't answer your question. What doesn't it achieve that you need?

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


fashimpaur
User / Moderator

Sep 4, 2002, 5:02 AM

Post #8 of 13 (1139 views)
Re: [cypher_MS] How would I go about.. [In reply to] Can't Post

Okay, now I will try my hand at the solution.

Instead of using your array as input, I added more "ids" using a __DATA__
label. Take a look:


Code
   


#use
strict;
my @ids = ();

while(<DATA>){
chomp;
push @ids, split(/ /, $_);
}


my $n = 1;

LOAD_LOOP: while (@ids){
my $arrayname = "page".($n);
use vars ( @$arrayname );

for (1..5){
push @$arrayname, shift @ids;
}
++
$n if @ids > 0;
}

print "Total Page arrays Created: $n\n\n";

for (1..$n){
my $arrayname = "page".$_;
print "\@page$_ = (".join(", ", @$arrayname),")\n";
}

print "\n\@page5 = (".join(", ", @page5),")\n";


__DATA__


01 02 12 13 541 54 1002 18 23 25 27 28 29 30 31 33 35 36 38 39 40 41 42 43 44 45
46 47 48 49 505 1821 1312 112 117 189 220 210 221 317 87 892 2230

As you can see, the output can be obtained using the dynamic variable names
(@$arrayname) or , after they are loaded in the LOAD_LOOP, you can use the
variable names you were wanting to use such as @page1, @page2, etc.

!NOTE!
I commented out "use strict;". It will get compile errors if run with strict turned
on.


Hope that helps,
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


(This post was edited by fashimpaur on Sep 4, 2002, 6:32 AM)


davorg
Thaumaturge / Moderator

Sep 4, 2002, 5:55 AM

Post #9 of 13 (1137 views)
Re: [fashimpaur] How would I go about.. [In reply to] Can't Post

Am I missing something obvious? Running that gives me an error:

Global symbol "@page5" requires explicit package name at test.pl line 32.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


fashimpaur
User / Moderator

Sep 4, 2002, 6:34 AM

Post #10 of 13 (1135 views)
Re: [davorg] How would I go about.. [In reply to] Can't Post

Dave,

You caught me. I put in use strict, but did not test it with line 32 in. You cannot
run this with strict. I edited my post to reflect this. Otherwise, it works the way
he described in his original post.

Thanks for the constructive view,
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


Paul
Enthusiast

Sep 4, 2002, 11:16 AM

Post #11 of 13 (1131 views)
Re: [fashimpaur] How would I go about.. [In reply to] Can't Post

Just something I noticed:

split(/ /, $_);

can simply be:

split/ /;


fashimpaur
User / Moderator

Sep 4, 2002, 11:49 AM

Post #12 of 13 (1129 views)
Re: [RedRum] How would I go about.. [In reply to] Can't Post

Yes, but I just like to state the obvious. Wink
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


davorg
Thaumaturge / Moderator

Sep 4, 2002, 10:46 PM

Post #13 of 13 (1124 views)
Re: [RedRum] How would I go about.. [In reply to] Can't Post

In many cases (and this is one of them) this can be reduces further to just

Code
split;

The default regex that split uses is /\s+/.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


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

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