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:
Data Structure

 



zapzap
User

Nov 3, 2013, 3:43 AM

Post #1 of 6 (1018 views)
Data Structure Can't Post

Greetings, can somebody let me know if there is a better way to create the following requested data structure



Code
   1   2   3   4   5   6 
1 A B C D E F
2 G H I J K L
3 M N O P Q R
4 S T U V W X
5 Y Z 0 1 2 3
6 4 5 6 7 8 9

I have

my $table;
my @chars = split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');
for(my $start = 0; $start < 6; $start++) {
push(@$table,[@chars[(6*$start)..(6*$start+5)]]);
}


Thanx zap


Laurent_R
Enthusiast / Moderator

Nov 3, 2013, 10:55 AM

Post #2 of 6 (1011 views)
Re: [zapzap] Data Structure [In reply to] Can't Post

Try this to populate directly your array:


Code
my @char = (A..Z, 0..9);



Laurent_R
Enthusiast / Moderator

Nov 3, 2013, 11:02 AM

Post #3 of 6 (1010 views)
Re: [zapzap] Data Structure [In reply to] Can't Post

Another starting possible starting point might be regexes, as shown in the following debugger session:


Code
  DB<14> @char = (A..Z, 0..9); 

DB<15> $_ = join '', @char;

DB<16> @lines = m/(.{6})/g

DB<17> x @lines
0 'ABCDEF'
1 'GHIJKL'
2 'MNOPQR'
3 'STUVWX'
4 'YZ0123'
5 456789



BillKSmith
Veteran

Nov 3, 2013, 3:08 PM

Post #4 of 6 (1003 views)
Re: [Laurent_R] Data Structure [In reply to] Can't Post

If your intent is to initialize @table with constant data, direct assignment would be better;


Code
@table = ([qw(A B C D E F)], 
[qw(G H I J K L)],
[qw(M N O P Q R)],
[qw(S T U V W X)],
[qw(Y Z 0 1 2 3)],
[qw(4 5 6 7 8 9)],
);


If you must start from a string, your approach is not bad. I would prefer to use substring and avoid temporary array.


Code
my $string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; 
my @table = map { [map {substr ($string,0,1,'')} 0..5]} 0..5;

Good Luck,
Bill


zapzap
User

Nov 4, 2013, 2:31 AM

Post #5 of 6 (995 views)
Re: [Laurent_R] Data Structure [In reply to] Can't Post

Thanx, I hadn't thought of (A..Z,0..9)
Never crossed my mind.
Nice stuff with the regex, real nice!
thanx, zap


zapzap
User

Nov 4, 2013, 2:34 AM

Post #6 of 6 (994 views)
Re: [BillKSmith] Data Structure [In reply to] Can't Post

Ya, I agree with the hard coded version but I thought of it a little late. But then the thought of creating it myself intrigued me so I gave it a shot.

I have never seen a map within a map, interesting.
Thanx for your help and suggestions
zap

 
 


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

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