Need Help Mapping Arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help Mapping Arrays
# 1  
Old 08-23-2011
Need Help Mapping Arrays

I have the following arrays with different lengths that I want to map them with the same key.

Code:
# Week numbers, 8 columns
@headers = ("2011-34", "2011-35", "2011-36", "2011-37", "2011-38", "2011-39", "2011-40", "2011-41");
%data = ("2011-34", BCE,
         "2011-35", YZA,
         "2011-36", MID,
         "2011-37", KAL,
         "2011-39", WUL,
         "2011-41", YOK);

# Need to map output as below:
|    |2011-34|2011-35|2011-36|2011-37|2011-38|2011-39|2011-40|2011-41|
| 1 |  BCE  |  YZA   |   MID  |  KAL   |            |  WUL |            |  YOK  |


Can someone here help?

Thanks in adance !
# 2  
Old 08-23-2011
Hi,
need to map the first array with second?
Also which language you r working?

Cheers,
Ranga:-)
# 3  
Old 08-23-2011
Looks like Perl, but I don't really understand the question myself either.
# 4  
Old 08-23-2011
Sorry for the confusion. Yes, need to map the first array with second.
And it is in Perl.


Thanks!
# 5  
Old 08-23-2011
You said that in the first place, I still don't understand what you mean by that.

If by "map" you mean "print":

Code:
@headers = ("2011-34", "2011-35", "2011-36", "2011-37", "2011-38", "2011-39", "$
%data = ("2011-34", BCE,
         "2011-35", YZA,
         "2011-36", MID,
         "2011-37", KAL,
         "2011-39", WUL,
         "2011-41", YOK);

print "|\t|", join("|", @headers), "\n", "|1\t";
foreach(@headers)       {       print "|", $data{$_}, "\t";     }
print "\n";

# 6  
Old 08-23-2011
Give this perl script a try...
Code:
#!/usr/bin/perl

@headers = ("2011-34", "2011-35", "2011-36", "2011-37", "2011-38", "2011-39", "2011-40", "2011-41");

%data = ("2011-34", BCE,
         "2011-35", YZA,
         "2011-36", MID,
         "2011-37", KAL,
         "2011-39", WUL,
         "2011-41", YOK);

foreach $h (@headers) {
    if (not exists $data{$h}) {
        $data{$h} = "";
    }
}

unshift(@headers, "    ");
$last = $headers[@headers - 1];

foreach $t (@headers) {
    printf("|%s", $t eq $last ? $t . "|\n" : $t);
}

foreach $v (@headers) {
    printf("|%4s", "1") if not exists $data{$v};
    printf("|%7s", $data{$v}) if exists $data{$v};
    print "|\n" if $v eq $last;
}

Save it and run it on the commandline...
Code:
./your_script_name

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Many to many -- mapping

INPUT 13333--TEXT1 14444--TEXT2 13333--TEXT3 12233--TEXT5 14444--TEXT5 12233--TEXT1 12222--TEXT5 13333--TEXT09 what I'm looking for is something using awk arrays with below given output. 14444--TEXT2,TEXT5 13333--TEXT1,TEXT3,TEXT09 12233--TEXT5,TEXT1 12222--TEXT5 (6 Replies)
Discussion started by: busyboy
6 Replies

2. Shell Programming and Scripting

Mapping Servers

Hi All, I need an urgent assistance please . My case below: I have a list of 500 IP addresses. All These ip addresses are mapped/connected to different machine kinds : NT, Linux, Switch, Router ,FW, and so on. My Requirement is to filter from all this ip address only the Linux machines.... (2 Replies)
Discussion started by: James Stone
2 Replies

3. Shell Programming and Scripting

Creating unique mapping from multiple mapping

Hello, I do not know if this is the right title to use. I have a large dictionary database which has the following structure: where a b c d e are in English and p q r s t are in a target language., the two separated by the delimiter =. What I am looking for is a perl script which will take... (5 Replies)
Discussion started by: gimley
5 Replies

4. UNIX for Dummies Questions & Answers

Re-Mapping Printers.

Hi we have a situation where some printers are on a server that sometimes has to be rebooted. If this happens the Unix boxes we have that are referencing the printers in the vfstab file fail to work even when the print server is brought back up. Does anyone know if it would be possible to put... (0 Replies)
Discussion started by: Hadleyshope
0 Replies

5. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

6. UNIX for Dummies Questions & Answers

Mapping PF Keys in Vi

This is my first post and right off the bat, I want to let you know that my experience in UNIX is 2 days only backed up with over 20 years of IT working. So, if this is a dumb question or too stupid, please bear with me. I read somewhere on the web and also on these forums that you can map your... (7 Replies)
Discussion started by: sssccc
7 Replies

7. IP Networking

port mapping

Hello to all! I am new to this interesting forum. My questions is not totally related to unix/linux systems, but I am not finding proper place where to make my question and this forum seems to be visited by experts and unix/linux experts are epxerts in everything My ISP has two kinds of... (1 Reply)
Discussion started by: kallquk
1 Replies

8. Shell Programming and Scripting

searching and mapping

Hi, I just started looking into various aspect of unix shell scripting. I am completely new to the world of UNIX. Could any one help me in solving the following requirement. I have a parameter file with some data e.g. sample.param ------------- Andrew=201 Bob=219 Shelly=239... (4 Replies)
Discussion started by: mamatabbsr
4 Replies

9. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

10. UNIX for Advanced & Expert Users

kernel mapping...

> how the sendmsg and recvmsg calls will know which kernel module to use (SCTP, RTP etc.) internally(kernel mapping: how kernel handle socket call) (1 Reply)
Discussion started by: prangin
1 Replies
Login or Register to Ask a Question