LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-16-2009, 01:47 PM   #1
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Rep: Reputation: 36
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

Last edited by kdelover; 09-16-2009 at 01:55 PM.
 
Old 09-16-2009, 02:42 PM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,094
Blog Entries: 2

Rep: Reputation: 111Reputation: 111
You could probably do something like

$info = `awk -F" " '{print \$4,\$5,\$6,\$7}'`;
my @arr = split(' ', $info);
 
Old 09-16-2009, 04:06 PM   #3
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Original Poster
Rep: Reputation: 36
Thanks that seems to be working Had to read little about to split function since am new to perl.Thanks again.
 
Old 09-16-2009, 04:39 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by kdelover View Post
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 ?
 
Old 09-16-2009, 05:42 PM   #5
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Original Poster
Rep: Reputation: 36
^^^ 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.
 
Old 09-16-2009, 06:01 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by kdelover View Post
^^^ 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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a CSV text file and storing the values in Oracle Database table shafi2all Linux - Newbie 3 04-17-2008 12:19 PM
storing xset values knobby67 Linux - Software 2 02-07-2008 03:09 AM
another simple unix scripting question! storing values in variables christianunix Linux - Newbie 4 10-30-2007 01:13 PM
help with shell script - fill an array with values from a file delmoras Linux - General 1 07-17-2006 11:19 AM
storing multiple values within one field in mysql antken Programming 8 12-15-2002 10:08 PM


All times are GMT -5. The time now is 08:48 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration