LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   storing values in an array in perl script :) (https://www.linuxquestions.org/questions/programming-9/storing-values-in-an-array-in-perl-script-755687/)

kdelover 09-16-2009 01:47 PM

storing values in an array in perl script :)
 
hello ppl,
I need a small help in storing the values awk returns in an array so that they can be indexed later on.

Quote:

awk -F" " '{print \$4","\$5","\$6","\$7}'
above awk statement returns the value as

Quote:

100 200 300 400
is there anyway i can place these values in an array in perl? What i mean is something like this

Quote:

my @arr = awk -F" " '{print \$4,\$5,\$6,\$7}'`;
print $arr [2]
;

so it will print me the value indexed at 2nd position that is 300.

To make it simple awk will return values from different columns,which i want to store in different variables,such that i dont have to use the awk statement multiple times.

Hope you ppl got my question :)

estabroo 09-16-2009 02:42 PM

You could probably do something like

$info = `awk -F" " '{print \$4,\$5,\$6,\$7}'`;
my @arr = split(' ', $info);

kdelover 09-16-2009 04:06 PM

Thanks that seems to be working :) Had to read little about to split function since am new to perl.Thanks again.

Sergei Steshenko 09-16-2009 04:39 PM

Quote:

Originally Posted by kdelover (Post 3685591)
hello ppl,
I need a small help in storing the values awk returns in an array so that they can be indexed later on.



above awk statement returns the value as


is there anyway i can place these values in an array in perl? What i mean is something like this

;

so it will print me the value indexed at 2nd position that is 300.

To make it simple awk will return values from different columns,which i want to store in different variables,such that i dont have to use the awk statement multiple times.

Hope you ppl got my question :)

Why do you call 'awk' from Perl in the first place ?

kdelover 09-16-2009 05:42 PM

^^^ That because i dont know the funtion which would perform the task that awk is doing in the program,id be glad if you can tell me something similar to that in perl.

Sergei Steshenko 09-16-2009 06:01 PM

Quote:

Originally Posted by kdelover (Post 3686018)
^^^ That because i dont know the funtion which would perform the task that awk is doing in the program,id be glad if you can tell me something similar to that in perl.

So, you are writing in Perl before even trying to learn it ?

Have you ever read

man perlfunc
man perlre
man perlretut

?

And/or on the web:

http://perldoc.perl.org/perlfunc.html
http://perldoc.perl.org/perlre.html
http://perldoc.perl.org/perlretut.html

?

A lot of 'awk' functionality, especially splitting on whitespaces, is implemented in 'split' function:

http://perldoc.perl.org/functions/split.html
.

Put

Code:

use strict;
use warnings;

into your code first and foremost.


All times are GMT -5. The time now is 11:58 AM.