Perl array formatting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl array formatting
# 1  
Old 04-09-2016
Perl array formatting

Experts,
I'm having problems with a portion of my perl script.

I have a perl array that contains the following:



Code:
Port1,circuit3,2;4
Port2,circuit1,9;6
Port3,circuit2,12;5
Port4,circuit4,11;10;3
Port5,circuit7,8;7;1

I'm trying to produce the following output:

Code:
	Port1		Port2		Port3		Port4		Port5
1									circuit7
2	circuit3
3							circuit4
4	circuit3
5					circuit2
6			circuit1
7									circuit7
8									circuit7
9			circuit1
10							circuit4
11							circuit4
12					circuit2

I'm really stuck on getting this fixed. I hope it's clear what I'm trying to do here.

Thanks
# 2  
Old 04-09-2016
Hi,
what's array type ?
how to fill this array ?
can you show your code that filled this array ?

Regards.
# 3  
Old 04-09-2016
The array is filled with the input file

Input:
Code:
Port1,circuit3,2;4
Port2,circuit1,9;6
Port3,circuit2,12;5
Port4,circuit4,11;10;3
Port5,circuit7,8;7;1

Each element is a line in the file.

I'm thinking I need to do with a hash of arrays??
# 4  
Old 04-11-2016
Tim,

How about this?

Code:
#! /usr/bin/perl

use strict;
use warnings;

my $file = 'input_file'; ## Change you input file name
my %info = ();
my %ports = ();

if(open(IFILE, "<", $file)) {

        while (<IFILE>) {
                my $line = $_;
                chomp($line);
                my ($port,$c,$l) = split(/,/, $line);
                my @list = split(/;/,$l);

                foreach my $i (@list) {
                        $info{$i}{$port} = $c;
                }
                $ports{$port}++;
        }
        close(IFILE);

        ## Display header
        print "\t", join("\t\t", sort keys %ports), "\n";

        ## Display body
        foreach my $i (sort {$a <=> $b} keys %info) {
                print $i, "\t";
                my $str='';
                foreach my $p (sort keys %ports) {
                        if($str) {
                                $str = join("\t", $str, $info{$i}{$p} || "\t");
                        } else {
                                $str = $info{$i}{$p} || "\t";
                        }
                }
                print $str, "\n";
        }
} else {
        print "Error: Unable to open $file\n";
}


Output that I got:

Code:
        Port1           Port2           Port3           Port4           Port5
1                                                                       circuit7
2       circuit3
3                                                       circuit4
4       circuit3
5                                       circuit2
6                       circuit1
7                                                                       circuit7
8                                                                       circuit7
9                       circuit1
10                                                      circuit4
11                                                      circuit4
12                                      circuit2

Cheers!
Ranga
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Formatting Array Output

Hello All, Q1) I have the below code but when the email was sent out all lines are merged and coming out as a single line though my printf statement has newline "\n", How do i avoid that? Q2) In my second IF statement when i introduced the backslash "\" for continuation of a line or command, i... (10 Replies)
Discussion started by: Ariean
10 Replies

2. Shell Programming and Scripting

Perl formatting.

I have a file which has following data: And I want the following out of it: So basically I want only the first four command separated fields and strip off the rest. How do I do it in perl? I want only perl code and no shell code. I can split it and try to do something, but... (7 Replies)
Discussion started by: som.nitk
7 Replies

3. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

4. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

5. Shell Programming and Scripting

Array in Perl - Detect several file to be in one array

Hi everyone I have one question about using array in perl. let say I have several log file in one folder.. example test1.log test2.log test3.log and the list goes on.. how to make an array for this file? It suppose to detect log file in the current directory and all the log file will... (3 Replies)
Discussion started by: sayachop
3 Replies

6. Shell Programming and Scripting

perl, put one array into many array when field is equal to sth

Hi Everyone, #!/usr/bin/perl use strict; use warnings; my @test=("a;b;qqq;c;d","a;b;ggg;c;d","a;b;qqq;c;d"); would like to split the @test array into two array: @test1=(("a;b;qqq;c;d","a;b;qqq;c;d"); and @test2=("a;b;ggg;c;d"); means search for 3rd filed. Thanks find the... (0 Replies)
Discussion started by: jimmy_y
0 Replies

7. Shell Programming and Scripting

Perl grep array against array

Hi, Is there any way I can grep an array against another array? Basically here's what I need to do. There will be an array containing some fixed texts and I have to check whether some files contain these lines. Reading the same files over and over again for each different pattern doesnt seem... (1 Reply)
Discussion started by: King Nothing
1 Replies

8. Shell Programming and Scripting

perl -write values in a file to @array in perl

Hi can anyone suggest me how to write a file containing values,... say 19 20 21 22 .. 40 to an array @array = (19, 20, ... 40) -- Thanks (27 Replies)
Discussion started by: meghana
27 Replies

9. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies

10. Shell Programming and Scripting

Array Retrieval Formatting

I have a shell script (Solaris v8 ksh) loading an array from a flat file. An exaple of that flat file is below. It contains white-spaces, and carrage returns. When I try to load the first line as a variable, it (the variable) shows up as the first field not the first line. How do you arrange an... (9 Replies)
Discussion started by: gozer13
9 Replies
Login or Register to Ask a Question