Perl Split lines into array??

D

Davy

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
 
U

usenet

Davy said:
a SAQ - to TWO newsgroups

Don't multi-post!!!!

You may wish to review the posting guidelines for this group
(comp.lang.perl.misc), which you can read on the web here:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
These guidelines are for YOUR benefit, because they show you how to
post good questions which are very likely to get good (and fast, and
polite) responses. Failure to reasonably follow these guidelines will
more likely get you flamed, ignored, or killfiled by many of the
regular (and expert) participants in this newsgroup.

Cheers!
 
J

Jürgen Exner

Davy said:
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?

Most people would probably use split(), but Perl is famous for there is more
than one way to do it and you can do it whichever way you prefer.

jue
 
S

Scott Bryce

That's a SAQ (self-answering question) - you are hereby nominated for a
bit of fame in perlsaq (http://www.ginini.com/perlsaq.html)


The "How do I split?" portion of his question may be a SAQ, but I think
his confusion is how to split on whitespace when he does not know how
many white space characters there might be.

If the OP reads the docs for regular expressions, he will find his
answer (assuming I understand the question correctly.)
 
U

usenet

Scott said:
The "How do I split?" portion of his question may be a SAQ, but I think
his confusion is how to split on whitespace when he does not know how
many white space characters there might be.

It's still an SAQ (100%), because the OP needs nothing more than the
split() function; no regex or anything else is necessary (per the
docs). By default, split() doesn't give a fig how many whitespaces you
have, as can be seen:

#!/usr/bin/perl
$\ = "\n";
print join ",", map {"'$_'"} split while <DATA>;

__DATA__
sum 15 7
diff 3 44

#### OUTPUT #########
'sum','15','7'
'diff','3','44'
 
S

Scott Bryce

By default, split() doesn't give a fig how many whitespaces you
have, as can be seen:

In other words, had I read the docs before responding...

For some reason I was thinking that split without a pattern would split
$_ on single spaces. I stand corrected.
 
D

DarkStarZ

Davy said:
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

Davy,

here ya go
#======================
while ($line=<>)
{
$myArray[$y]=$line;
$y++;

}
#=====Test================
for(@myArray)
{
print "$_\n";
}
 
P

Paul Lalli

here ya go
#======================
while ($line=<>)
{
$myArray[$y]=$line;
$y++;
}
#=====Test================
for(@myArray)
{
print "$_\n";
}


Is this a joke of some kind? This doesn't do anything the OP asked
for, and is pretty terrible code to begin with.

If it is a joke, I'm afraid I don't get it.

Paul Lali
 
T

Tad McClellan

DarkStarZ said:
while ($line=<>)
{
$myArray[$y]=$line;
$y++;
}


Or, replace those 5 lines with this one:

@myArray = <>;

Do you get paid by the character or something?

#=====Test================
for(@myArray)
{
print "$_\n";
}


Why do you double space the output?
 
J

Joe Smith

DarkStarZ said:
here ya go
#======================
while ($line=<>) {
$myArray[$y]=$line;
$y++;
}

Looks like you've never learned about perl's 'push' operator.

while (<>) {
# do something here that changes $_
push @myArray,$_
}

The other replies to this post shows a better way to do it if
there is no manipulation of $_, by reading directly into @myArray.
-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,378
Latest member
BlakeLig

Latest Threads

Top