Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating array like this

Status
Not open for further replies.

richardko

Programmer
Joined
Jun 20, 2006
Messages
127
Location
US
hi,
can anyone suggest how i go about creating an array like this:
123 asd 1.22 asdd
32 ewq 2.32 asdd
43 rewq 2.12 ewet
51 erwt 34.2 ewet
23 erww 4.21 ewet
91 fdgs 3.43 ewet
123 refs 3.22 asdd
123 refs 4.32 asdd

The problem i had was when i tried to use the Sort::Fields module in the CPAN.
thanks
 
Use Array of Arrays

Code:
@AoA = (
        [ 123,"asd",1.22,"asdd" ],
        [ 32,"ewq",2.32,"asdd" ],
        [ 43,"rewq",2.12,"ewet" ]        
      );

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
If it's for Sort::Fields, then it's a normal array, not an array of arrays that's necessary.
Code:
my @array = (
  '123   asd   1.22   asdd',
  '32    ewq   2.32   asdd',
  '43    rewq  2.12   ewet',
  '51    erwt  34.2   ewet',
  '23    erww  4.21   ewet',
  '91    fdgs  3.43   ewet',
  '123   refs  3.22   asdd',
  '123   refs  4.32   asdd'
);

A nice trick when putting test data in a script like this is to use the special DATA filehandle, i.e.:
Code:
#!/usr/bin/perl
use strict;

my @array = <DATA>;

# rest of your code here

__DATA__
123   asd   1.22   asdd
32    ewq   2.32   asdd
43    rewq  2.12   ewet
51    erwt  34.2   ewet
23    erww  4.21   ewet
91    fdgs  3.43   ewet
123   refs  3.22   asdd
123   refs  4.32   asdd
 
thank you for the valuable post. I have another question as to how i create an array with the example above without using a text file but from other source. For example I am using a DB_File module to read from a Berkeley database file which is like this:
1 -> Cyclops-Remote.html 199.00
2 -> EFG500K.html 10.00
4 -> 9906.html 12.00

1,2,4 are the key. "Cyclops-Remote.html", "199.00" are the value for key "1" and so forth.
After finding the "key" I use the code below to get the "value":
dbmopen(%key, "berkeley.db", undef) or die "dbmopen: $!";
@pages=("1","2","4");
foreach (@pagess){
push (@testarray,$key{$_});
}

My question is how can I create a "testarray" so that I can sort the list afterwards to be able to search by "Price" (10.00,12.00,199.00) or by the name of the file (9906.html,Cyclops-Remote.html,EFG500K.html) .
I am trying to create a search for my website where I should be able to sort the products by name,price,or other factors.

Any hint is appreciated.

Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top