[Perl] Split lines into array - variable line items - variable no of lines.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Perl] Split lines into array - variable line items - variable no of lines.
# 8  
Old 09-28-2011
Quote:
Originally Posted by ejdv
...Do not know how to access the array items when I have the desired $company.
You'll need to know the concept of "references" for this.

As for the custom ordering of keys, you could reorder the array @all_comp_products, but that was not its intention in the first place. The array was used so that the companies (and hence their products) could be displayed exactly in the order in which they appear in the data file.

A hash should be a simpler alternative. The algorithm is as follows:

(1) We create a hash %all_comp_products that has the company names as keys.
(2) The values are the array references that point to arrays of products.
(3) A hash called %keyorder has our custom key order for keys of %all_comp_products
(4) We iterate through keys of %all_comp_products sorted as per %keyorder and print company and product values.

Something like this -

Code:
$
$
$ cat lines.txt
Microsoft,IBM,Oracle,Apple
Windows 95,DB2,Oracle,MacBook
MS Excel,Fortran,Siebel,iPod
XBox,ATM,MySQL,iPad
Zune,Deep Blue,PeopleSoft,Pixar
$
$
$ cat -n lines2.pl
     1  #perl -w
     2  use strict;
     3  # ##################################################################################################
     4  #
     5  #  For the data file that looks like this:
     6  #
     7  #  Microsoft,IBM,Oracle,Apple
     8  #  Windows 95,DB2,Oracle,MacBook
     9  #  MS Excel,Fortran,Siebel,iPod
    10  #  XBox,ATM,MySQL,iPad
    11  #  Zune,Deep Blue,PeopleSoft,Pixar
    12  #
    13  #  this Perl program creates a hash %all_comp_products that looks like this:
    14  #
    15  #  $all_comp_products{Microsoft} = [ "Windows 95", "MS Excel", "XBox",  "Zune"       ]
    16  #  $all_comp_products{IBM}       = [ "DB2",        "Fortran",  "ATM",   "Deep Blue"  ]
    17  #  $all_comp_products{Oracle}    = [ "Oracle",     "Siebel",   "MySQL", "PeopleSoft" ]
    18  #  $all_comp_products{Apple}     = [ "MacBook",    "iPod",     "iPad",  "Pixar"      ]
    19  #
    20  # ##################################################################################################
    21
    22  my $file = "lines.txt";
    23  my @company;
    24  my @values;
    25  my %all_comp_products;
    26  my %keyorder;
    27
    28  open (FH, "<", $file) or die "Can't open $file for reading: $!";
    29  while (<FH>) {
    30    chomp;
    31    if (/^Microsoft/) {
    32      @company = split /,/;
    33    } else {
    34      @values = split /,/;
    35      for (my $i = 0; $i <= $#company; $i++) {
    36        push @{$all_comp_products{$company[$i]}}, $values[$i];
    37      }
    38    }
    39  }
    40  close (FH) or die "Can't close $file: $!";
    41
    42  # Our custom key order is as follows -
    43  %keyorder = qw (Oracle 1 Apple 2 IBM 3 Microsoft 4);
    44
    45  # Now, we'll iterate through the hash with custom sorted key order and display the data
    46  foreach my $comp (sort {$keyorder{$a} <=> $keyorder{$b}} keys %all_comp_products ) {
    47    print "Company  : $comp\n";
    48    print "Products :\n";
    49    foreach my $prod (@{$all_comp_products{$comp}}) {
    50      print "           $prod\n";
    51    }
    52    print "=" x 40,"\n";
    53  }
$
$
$ perl lines2.pl
Company  : Oracle
Products :
           Oracle
           Siebel
           MySQL
           PeopleSoft
========================================
Company  : Apple
Products :
           MacBook
           iPod
           iPad
           Pixar
========================================
Company  : IBM
Products :
           DB2
           Fortran
           ATM
           Deep Blue
========================================
Company  : Microsoft
Products :
           Windows 95
           MS Excel
           XBox
           Zune
========================================
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a pattern in a file and split the line into two lines

Hi All, Greetings everyone !!! I have a file which has many lines, out of which one line is as below. I need to search for pattern "varchar(30) Select" and if exists, then split the line as below. I am trying to achieve this in ksh. Can anyone help me on this. (8 Replies)
Discussion started by: Pradhikshan
8 Replies

2. Shell Programming and Scripting

Split a line into multiple lines based on delimeters

Hi, I need help to split any lines that contain ; or , input.txtAc020 Not a good chemical process AC030 many has failed, 3 still maintained AC040 Putative; epithelial cells AC050 Predicted binding activity AC060 rodC Putative; upregulated in 48;h biofilm vs planktonic The output... (8 Replies)
Discussion started by: redse171
8 Replies

3. Shell Programming and Scripting

How do I split a single-line input into five lines?

Example input: John:Shepherd:770-767-4040:U.S.A:New York Mo Jo:Jo Jo: 666-666-6666:U.S.A:Townsville Expected Output: First Name: John Last Name: Shepherd Phone Number: 770-767-4040 Country: U.S.A State: New York First Name: Mo Jo Last Name: Jo Jo Phone Number: 666-666-6666... (10 Replies)
Discussion started by: Camrikron
10 Replies

4. Shell Programming and Scripting

How to get lines having a variable in perl?

Hi, This might be simple question. But i couldn't find the answer. #$ra is having a value which i have got from some other file I want the first occurrence of the line having the value that is in variable "$ra" This is what i have tried. while ( $iop = <TST>) { if($iop =~ /$ra/) { ... (6 Replies)
Discussion started by: twistedpair
6 Replies

5. Shell Programming and Scripting

Need to remove first 6 lines and last line in a array ---- perl scripting

Hi I have stored a command output in an array like below @a = `xyz`; actually xyz comnad will give the output like this tracker date xxxxxxx xxxxxxx --------------------- 1 a 2 b ---------------------- i have stored the "xyz" output to an... (3 Replies)
Discussion started by: siva kumar
3 Replies

6. Shell Programming and Scripting

Delete lines in an array Using perl

im having an array @check which contains text ..i want to open the array and i have to delete lines starting from a word called "check1" till "check2" for eg:- check1 Use descriptive titles when posting. For example, do not post questions with subjects like "Help Me!", "Urgent!!" or "Doubt".... (0 Replies)
Discussion started by: rajkrishna89
0 Replies

7. Shell Programming and Scripting

Split line in to 3 lines

Hi, I have a file which contains 1000's of lines. Each line is a log which is pretty long. So i want to split the each line based on 3 category. 1> Date 2><REQUEST> 3><RESPONSE> So below is the example of a line. 2010-11-16 00:45:12,314<REQUEST><VALIDATION-ERROR><soapenv:Envelope... (16 Replies)
Discussion started by: raghunsi
16 Replies

8. Shell Programming and Scripting

split single line into two line or three lines

Dear All, I want to split single line into two line or three lines wherever “|” separated values comes using Input line test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087, output shoule be test,DEMTEMPUT20100404010012,,,,,,,,0070086, test,DEMTEMPUT20100404010012,,,,,,,,0070087, (14 Replies)
Discussion started by: arvindng
14 Replies

9. Shell Programming and Scripting

Join in a single line variable number of lines

Hi all, I have a file with little blocks beginning with a number 761XXXXXX, and 0, 1, 2 or 3 lines below of it beginning with STUS as follow: 761625820 STUS ACTIVE 16778294 STUS NOT ACTIVE 761157389 STUS ACTIVE 16778294 761554921 STUS ACTIVE 16778294 STUS NOT ACTIVE STUS ACTIVE OP... (4 Replies)
Discussion started by: cgkmal
4 Replies

10. Shell Programming and Scripting

split variable values into array

i have these values inside variable $blah BUNGA TERATAI 3 5055 ITH 1 0 0 0 1 1 JADE TRADER 143W ITH 4 0 0 0 4 4 MOL SPLENDOR 0307A ITH 3 0 0 0 3 3 so how do I split them into array with the... (4 Replies)
Discussion started by: finalight
4 Replies
Login or Register to Ask a Question