We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

How do I save three lines and print them after

ambuli
ambuli asked
on
Medium Priority
248 Views
Last Modified: 2012-05-11
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.

Comment
Watch Question

CERTIFIED EXPERT

Commented:
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 ?
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
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
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
#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

Author

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.

Author

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.



ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
$/="";
@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
CERTIFIED EXPERT

Commented:
Well, struct is available in Perl.  Or, I can make list.  What do you prefer?  Pointers are also there.

Author

Commented:
list would be nice :-)
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Thank you all.  Ozo, I think your code is fine, but I could not understand it as I am a newbie to perl.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
what do you wish to understand?
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.