Visit Our UNIX and Linux User Community


one solution if want fields in a file into each array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting one solution if want fields in a file into each array
# 1  
Old 11-05-2009
one solution if want fields in a file into each array

Hi Everyone,
This is just my method to if want fields in a file into each array.
Code:
[root@]# cat /tmp/3.txt
00:00:01        1
00:00:02        2
00:00:03        33
[root@]# cat text.pl
#!/usr/bin/perl
@field1 = `cat '/tmp/3.txt' | cut -f1 -d'\t'`;
print $field1[0];
[root@]# ./text.pl
00:00:01
[root@]#

As we can see, use this way, then we cut -f2, then can store the 2nd field into an array.

I know this is not a very good efficent way to store each field into each array for a file, but it is just what i can think it out.
# 2  
Old 11-05-2009
Why do you call external programs for things Perl can do by itself?
Code:
#!/usr/bin/perl

use strict;
use warnings;

my @field1 = map { (split /\t/)[0] } <>;

# 3  
Old 11-05-2009
Quote:
Originally Posted by pludi
Why do you call external programs for things Perl can do by itself?
Code:
#!/usr/bin/perl
 
use strict;
use warnings;
 
my @field1 = map { (split /\t/)[0] } <>;

Thanks pludi, but when i run it, the program just hang there, no result. Smilie
# 4  
Old 11-05-2009
It expects input via stdin or a file provided, so call it as either of these:
Code:
perl script.pl /tmp/3.txt
cat /tmp/3.txt | perl script.pl
perl script.pl </tmp/3.txt

Besides, it isn't meant to do anything, but to demonstrate how you can replace your system call to cat and cut with pure Perl, shaving off quite some time and resources off a run.
# 5  
Old 11-05-2009
Thanks pludi Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #941
Difficulty: Medium
A ping of death is a type of attack on a computer system that involves sending a malformed or otherwise malicious ping to a computer.
True or False?

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Psql output into array and read 2 fields into different variables

Hello Just edited the entry to make it easier to understand what i want How can i achieve this: GOAL: read 2 field from a table with PSQL result of this PSQL command is this INSTALLEDLANG=$(su - postgres -c "psql -A -t -q -c -d ${DBNAME} -t -c 'SELECT code, iso_code from res_lang'") ... (0 Replies)
Discussion started by: winston6071
0 Replies

2. Shell Programming and Scripting

awk two fields in one array

I want to compare these files while putting $1 and $2 into an array and getting rid of the punctuation. What am i doing wrong? File1.txt Apple # 223 Peach # 84; Banana # 1605. Banana # 1605; Orange # 6; Peach # 84 Peach # 84; Apple # 229; Banana # 1605. Peach # 84 Apple, # 229;... (3 Replies)
Discussion started by: sdf
3 Replies

3. UNIX for Dummies Questions & Answers

Fill fields with awk from an array?

Hi experts, I have been trying for a while to accomplish the following task using awk, and I just don't seem find find a way. I am not particular about using awk, it just seemed like the logical choice at first. I have a file that contains 5 fields that are delimited by a space character.... (1 Reply)
Discussion started by: GermanicGalore
1 Replies

4. Shell Programming and Scripting

how to spilit a row into fields and store the field content to the array

consider this is a line A#B#C#D#E#F#G#H note the delimeter is # i want to cut or spilt in to fields using the delimeter # and to store in an array. like this array=A array=B array=C array=D array=E and the array content should be displayed. echo "${array}" echo "${array}"... (5 Replies)
Discussion started by: barani75
5 Replies

5. Shell Programming and Scripting

Ksh array solution.

I was wondering if ksh supported arrays. I have a script that may work with several hosts. I'd like a means of knowing how many hosts I'm working with and an easy way to access them (as variables) in a loop. I'm assuming there's some kind of foreach in shell scripting. (1 Reply)
Discussion started by: mrwatkin
1 Replies

6. Shell Programming and Scripting

awk reading many fields to array

I want to read $3,$4,$5,$6,$7 of fileA in array and when fileb $1 = fileA $4 the i want to print array and few fields from fileB. This should work but has some syntax error. nawk -F, 'FNR==NR{a=;next} a{print a}' fileB fileA Appreciate if someone can correct this. (2 Replies)
Discussion started by: pinnacle
2 Replies

7. Shell Programming and Scripting

split varibles and store fields into shell varible array

I need to split a long varible which is a whole line read from a file into fields and store them in an array, the fields are delimited by pipe and a field may contain white spaces. I tried the following concept test and it has problem with field 5 which contain a space, appearently so because... (3 Replies)
Discussion started by: gratus
3 Replies

Featured Tech Videos