Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Perl Split lines into array??

Status
Not open for further replies.

davyzhu

Advanced Member level 1
Joined
May 23, 2004
Messages
494
Helped
5
Reputation
10
Reaction score
2
Trophy points
1,298
Location
oriental
Activity points
4,436
split lines into array perl

Hi all,

I am new to perl.

I use
#------------------
while(<INFILE>) {
$line = $_;
}
#------------------
to get one line from the file,

The line may like: sum 11 32
How to split lines into array like @myarray = {"sum","11","32"}; which knowing how many space between the words?

Any suggestions will be appreciated!
Davy
 

lovelyic

Junior Member level 2
Joined
Apr 28, 2002
Messages
23
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Activity points
200
Code:
chomp $line
@tmp_line = split(/\s+/, $line);
for($i=0, $i <= $#tmp_line, $i++)
{
  push(@myarray, $tmp_line[$i]);
}
 

    davyzhu

    Points: 2
    Helpful Answer Positive Rating

urslen

Member level 2
Joined
Aug 10, 2004
Messages
45
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,288
Activity points
493
The simple array declaration also may be done. Here is another possible way:

Code:
while(<INFILE>) {
$line = $_;
@a=$line;
print @a;
}
 

    davyzhu

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top