Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

How do I save three lines and print them after

Hi Experts,

I want to save three lines along with an ID.

For example:

KeyOne
line one
line two
line three

KeyTwo
another line one
another line two
another line three

In the above 8 lines, I want to save the line "one, line two line three" along with the keyOne.  Similarly for
keyTwo, I want to save "another line one, another line two, another line three".  

given the key, I should be able to print the three lines associated with that key.

0
ambuli
Asked:
ambuli
  • 5
  • 4
  • 3
2 Solutions
 
farzanjCommented:
Do you mean you want to make associate array like

('keyone'=> 'line one line two line three' , 'keytwo' => 'another line one another line two another line three')

Something like this ?
0
 
ozoCommented:
while( <DATA> ){
   print if $n=(/Key(One|Two)/..$n==3)
}
__DATA__
KeyOne
line one
line two
line three

KeyTwo
another line one
another line two
another line three
0
 
ozoCommented:
#or did you mean you want
$/="";
while( <DATA> ){
    @F=split/\n/;
    $save{shift @F}=join", ",@F;
}
for( keys %save ){
    print "$_: $save{$_}\n";
}
__DATA__
KeyOne
line one
line two
line three

KeyTwo
another line one
another line two
another line three
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ambuliAuthor Commented:
Hi farzanj, yes that is correct.  But, I would prefer to separate 'one line' 'two line' 'three line' somehow.  Later on I have to print them back as if they were present before.
0
 
ambuliAuthor Commented:
ozo, sorry I could not understand the code well.  I am new to perl (but not to programming).  If I talk in C Language,

I want something like

typedef struct{
   char *line1;
   char *line2;
   char *line3;
} my_struct;

my_struct myArray[100];

Here the index would be integer, but in my perl case, I want to use a string as the index as well.  Sorry, if I am confusing.



0
 
ozoCommented:
$/="";
@myArray = map[(split/\n/)[1..3]],<DATA>;
$\="\n";
print join", ",@$_ for @myArray;
__DATA__
KeyOne
line one
line two
line three

KeyTwo
another line one
another line two
another line three
0
 
farzanjCommented:
Well, struct is available in Perl.  Or, I can make list.  What do you prefer?  Pointers are also there.
0
 
ambuliAuthor Commented:
list would be nice :-)
0
 
farzanjCommented:
Here is some C-like Perl code, I wrote for you.  Perl programmers would hate it as it is very C-programming like.


#! /usr/bin/perl

use strict;
use warnings;

#Open the text file, read into array and close it.
my $file = 'data.txt';
open IN, $file or die "Could not open $!";
my @lines = <IN>;
close IN;

#Define hash
my %arr;
#Define variables
my ($key, $val1, $val2, $val3); 

while  (@lines)
{
        $key = shift @lines;
        #Read three lines
        ($val1, $val2, $val3) = (shift @lines, shift @lines, shift @lines);
        shift @lines;
        #Remove new line characters
        chomp ($key, $val1, $val2, $val3);

        #Make a hash of pointers to the list of lines
        $arr{$key} = [$val1, $val2, $val3];
}
#Print first line
print $arr{KeyOne}[0];

Open in new window

0
 
ozoCommented:
#If you want a hash rather than an array of my_struct
$/="";
while( <DATA> ){
    my($key,@F)=split/\n/;
    $arr{$key}=[@F];
}
for( keys %arr ){
    print "$_: ",join(", ",$arr{$_}),"\n";
}
__DATA__
KeyOne
line one
line two
line three

KeyTwo
another line one
another line two
another line three
0
 
ambuliAuthor Commented:
Thank you all.  Ozo, I think your code is fine, but I could not understand it as I am a newbie to perl.
0
 
ozoCommented:
what do you wish to understand?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now