Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

split function

1 view
Skip to first unread message

Chris Stinemetz

unread,
Dec 13, 2011, 11:43:47 PM12/13/11
to begi...@perl.org
I am trying to split the first element of an array by white space then
continue reading the rest of the file.
Thus far I am having trouble figuring out how to split the first line.

I would like the first line to be split so it looks like the following
with the "=" sign added.

Thank you in advance!

Chris

csno=
rfpi=
header_1=
header_2=
header_3=
header_4=
header_5=
header_6=
header_7=
header_8=
header_9=

I am getting the error:

Use of implicit split to @_ is deprecated at ./xxxxx.pl line 6.

#!/usr/bin/perl
use warnings;
use strict;

while (my @line = <DATA>) {
  my $header = split " ",$line[0];
  print $header;
}

__DATA__
csno    rfpi    header_1        header_2        header_3
header_4        header_5        header_6        header_7   header_8
    header_9
1       1       5.5     5.5     5.5     5.5     5.5     5.5     5.5
5.5     5.5
1       2       5.5     5.5     5.5     5.5     5.5     5.5     5.5
5.5     5.5
1       3       5.5     5.5     5.5     5.5     5.5     5.5     5.5
5.5     5.5
2       1       5.5     5.5     5.5     5.5     5.5     5.5     5.5
5.5     5.5
2       2       5.5     5.5     5.5     5.5     5.5     5.5     5.5
5.5     5.5
2       3       5.5     5.5     5.5     5.5     5.5     5.5     5.5
5.5     5.5
3       1       5.5     5.5     5.5     5.5     5.5     5.5     5.5
5.5     5.5
3       2       5.5     5.5     5.5     5.5     5.5     5.5     5.5
5.5     5.5
3       3       5.5     5.5     5.5     5.5     5.5     5.5     5.5
5.5     5.5
4       1       5.5     5.5     5.5     5.5     5.5     5.5     5.5
5.5     5.5
4       2       5.5     5.5     5.5     5.5     5.5     5.5     5.5
5.5     5.5
4       3       5.5     5.5     5.5     5.5     5.5     5.5     5.5
5.5     5.5

John W. Krahn

unread,
Dec 14, 2011, 12:22:52 AM12/14/11
to Perl Beginners
Chris Stinemetz wrote:
> I am trying to split the first element of an array by white space then
> continue reading the rest of the file.
> Thus far I am having trouble figuring out how to split the first line.
>
> I would like the first line to be split so it looks like the following
> with the "=" sign added.
>
> Thank you in advance!
>
> Chris
>
> csno=
> rfpi=
> header_1=
> header_2=
> header_3=
> header_4=
> header_5=
> header_6=
> header_7=
> header_8=
> header_9=
>
> I am getting the error:
>
> Use of implicit split to @_ is deprecated at ./xxxxx.pl line 6.

perldoc -f split
split /PATTERN/,EXPR,LIMIT
split /PATTERN/,EXPR
split /PATTERN/
split Splits the string EXPR into a list of strings and returns
that list. By default, empty leading fields are preserved,
and empty trailing ones are deleted. (If all fields are
empty, they are considered to be trailing.)

In scalar context, returns the number of fields found. In
scalar and void context it splits into the @_ array. Use
of split in scalar and void context is deprecated, however,
because it clobbers your subroutine arguments.


> #!/usr/bin/perl
> use warnings;
> use strict;
>
> while (my @line =<DATA>) {

You don't need a while loop if you are just going to read the whole file
into an array.


> my $header = split " ",$line[0];

You are using split in scalar context which means that $header will now
contain the number 11 because there are 11 fields in $line[0].


> print $header;
> }
>
> __DATA__
> csno rfpi header_1 header_2 header_3
> header_4 header_5 header_6 header_7 header_8
> header_9
> 1 1 5.5 5.5 5.5 5.5 5.5 5.5 5.5
> 5.5 5.5
> 1 2 5.5 5.5 5.5 5.5 5.5 5.5 5.5
> 5.5 5.5



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

timothy adigun

unread,
Dec 14, 2011, 8:55:13 AM12/14/11
to Chris Stinemetz, Perl Beginners, John W. Krahn
Hi Chris,
Please check added code to yours, in addition to what John wrote;

I am trying to split the first element of an array by white space then
continue reading the rest of the file.
Thus far I am having trouble figuring out how to split the first line.

I would like the first line to be split so it looks like the following
with the "=" sign added.

Thank you in advance!

Chris

csno=
rfpi=
header_1=
header_2=
header_3=
header_4=
header_5=
header_6=
header_7=
header_8=
header_9=

I am getting the error:

Use of implicit split to @_ is deprecated at ./xxxxx.pl line 6.

#!/usr/bin/perl
use warnings;
use strict;

#while (my @line = <DATA>) {
while (my $line = <DATA>) {
chomp $line;
# my $header = split " ",$line[0];
if($. == 1){ #$. => Current line number for the last filehandle
accessed
print $_,"=\n" for split/\s+/=>$line;
}
else{print $line,"\n";}
#print $header;
}

__DATA__
csno rfpi header_1 header_2 header_3
header_4 header_5 header_6 header_7 header_8
header_9
1 1 5.5 5.5 5.5 5.5 5.5 5.5 5.5
5.5 5.5
1 2 5.5 5.5 5.5 5.5 5.5 5.5 5.5
5.5 5.5
1 3 5.5 5.5 5.5 5.5 5.5 5.5 5.5
5.5 5.5
2 1 5.5 5.5 5.5 5.5 5.5 5.5 5.5
5.5 5.5
2 2 5.5 5.5 5.5 5.5 5.5 5.5 5.5
5.5 5.5
2 3 5.5 5.5 5.5 5.5 5.5 5.5 5.5
5.5 5.5
3 1 5.5 5.5 5.5 5.5 5.5 5.5 5.5
5.5 5.5
3 2 5.5 5.5 5.5 5.5 5.5 5.5 5.5
5.5 5.5
3 3 5.5 5.5 5.5 5.5 5.5 5.5 5.5
5.5 5.5
4 1 5.5 5.5 5.5 5.5 5.5 5.5 5.5
5.5 5.5
4 2 5.5 5.5 5.5 5.5 5.5 5.5 5.5
5.5 5.5
4 3 5.5 5.5 5.5 5.5 5.5 5.5 5.5
5.5 5.5

Please check perldoc -f split,
also check perldoc perlvar, for $. => Current line number for the last
filehandle accessed

Regards,
Tim

John W. Krahn

unread,
Dec 14, 2011, 7:18:31 PM12/14/11
to Perl Beginners
Don't change split " " to split/\s+/, it does something different. And
you don't need to use chomp as both split " " and split/\s+/ remove ALL
whitespace, including the newline.


> }
> else{print $line,"\n";}
> #print $header;
> }
>
> __DATA__
> csno rfpi header_1 header_2 header_3
> header_4 header_5 header_6 header_7 header_8
> header_9
> 1 1 5.5 5.5 5.5 5.5 5.5 5.5 5.5
> 5.5 5.5
> 1 2 5.5 5.5 5.5 5.5 5.5 5.5 5.5
> 5.5 5.5




Chris Stinemetz

unread,
Dec 15, 2011, 10:47:00 AM12/15/11
to John W. Krahn, Perl Beginners
I'm getting a bit closer. There a couple roadblocks I am up against.

I am able to split the lines by white space, but for some reason the
program isn't capturing the first lines to the @fieldValue array after
the @headerNames array.
Once I get all the lines to go into the array correctly I would like
to combine the @headerNames and @fieldValue arrays. The way I am doing
it now only appends the later.
I would like the combination to be the below for each elements in the
two arrays.

any help is greatly appreciated,

Chris

csno=1
rfpi=1
header_1=5.5
header_2=5.5
header_3=5.5
header_4=5.5
header_5=5.5
header_6=5.5
header_7=5.5
header_8=5.5
header_9=5.5



#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

my $header;
my @headerNames;
my $field;
my @fieldValue;
my @apxScript;

while (my $line = <DATA>) {
if($line =~ m|(.*_.*\n)|){
$header = $1;
@headerNames = split(" ",$header);
}

if($line !~ m|.*_.*\n|){
@fieldValue = split(" ",$line);
print "$fieldValue[0]\n";
}
}

my @apxScript=(@headerNames, @fieldValue);
print Dumper \@headerNames;
print Dumper \@fieldValue;
print Dumper \@apxScript;


__DATA__
csno rfpi header_1 header_2 header_3 header_4 header_5 header_6
header_7 header_8 header_9
1 1 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
2 2 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
3 3 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
4 1 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
5 2 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
6 3 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
7 1 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
8 2 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
9 3 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
10 1 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
11 2 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
12 3 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5

Dr.Ruud

unread,
Dec 15, 2011, 11:42:23 AM12/15/11
to begi...@perl.org
On 2011-12-14 05:43, Chris Stinemetz wrote:

> I am trying to split the first element of an array by white space then
> continue reading the rest of the file.
> Thus far I am having trouble figuring out how to split the first line.

You have an XY problem, you are probably looking for
http://search.cpan.org/perldoc?Text::CSV_XS.

--
Ruud

Chris Stinemetz

unread,
Dec 15, 2011, 12:58:00 PM12/15/11
to Dr.Ruud, begi...@perl.org
Text::CSV_XS is not an option for me. The unix system I am developing
Perl scripts on doesn't allow me to install local libraries from CPAN.

Thank you,

Chris

Rob Dixon

unread,
Dec 15, 2011, 1:20:31 PM12/15/11
to Perl Beginners, Chris Stinemetz
Hey Chris

Your program reads lines from DATA and splits every line that doesn't
contain an underscore into @fieldValue. Every line of data overwrites
the contents of the array, so the end result is that @fieldValue holds
the data from the last line of data.

What are you hoping for? If you want to retain the /first/ line of data
instead of the last then you need only to add a 'last' statement after
the '@fieldValue = split(" ",$line)' on line 19.

However I think it's more likely that you need /all/ of the data to be
output, so I suggest something like my program below.

HTH,

Rob


use strict;
use warnings;

my @headers;

while (<DATA>) {
if (@headers) {
my @data = split;
for my $i (0 .. $#headers) {
printf "%s=%s\n", $headers[$i], $data[$i];
}
}
else {
@headers = split;
}
}


__DATA__
csno rfpi header_1 header_2 header_3 header_4 header_5 header_6 header_7 header_8 header_9
1 1 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
2 2 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
3 3 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
4 1 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
5 2 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
6 3 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
7 1 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
8 2 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
9 3 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
10 1 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
11 2 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5
12 3 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5

**OUTPUT**

csno=1
rfpi=1
header_1=5.5
header_2=5.5
header_3=5.5
header_4=5.5
header_5=5.5
header_6=5.5
header_7=5.5
header_8=5.5
header_9=5.5
csno=2
rfpi=2
header_1=5.5
header_2=5.5
header_3=5.5
header_4=5.5
header_5=5.5
header_6=5.5
header_7=5.5
header_8=5.5
header_9=5.5
csno=3
rfpi=3
header_1=5.5
header_2=5.5
header_3=5.5
header_4=5.5
header_5=5.5
header_6=5.5
header_7=5.5
header_8=5.5
header_9=5.5
csno=4
rfpi=1
header_1=5.5
header_2=5.5
header_3=5.5
header_4=5.5
header_5=5.5
header_6=5.5
header_7=5.5
header_8=5.5
header_9=5.5
csno=5
rfpi=2
header_1=5.5
header_2=5.5
header_3=5.5
header_4=5.5
header_5=5.5
header_6=5.5
header_7=5.5
header_8=5.5
header_9=5.5
csno=6
rfpi=3
header_1=5.5
header_2=5.5
header_3=5.5
header_4=5.5
header_5=5.5
header_6=5.5
header_7=5.5
header_8=5.5
header_9=5.5
csno=7
rfpi=1
header_1=5.5
header_2=5.5
header_3=5.5
header_4=5.5
header_5=5.5
header_6=5.5
header_7=5.5
header_8=5.5
header_9=5.5
csno=8
rfpi=2
header_1=5.5
header_2=5.5
header_3=5.5
header_4=5.5
header_5=5.5
header_6=5.5
header_7=5.5
header_8=5.5
header_9=5.5
csno=9
rfpi=3
header_1=5.5
header_2=5.5
header_3=5.5
header_4=5.5
header_5=5.5
header_6=5.5
header_7=5.5
header_8=5.5
header_9=5.5
csno=10
rfpi=1
header_1=5.5
header_2=5.5
header_3=5.5
header_4=5.5
header_5=5.5
header_6=5.5
header_7=5.5
header_8=5.5
header_9=5.5
csno=11
rfpi=2
header_1=5.5
header_2=5.5
header_3=5.5
header_4=5.5
header_5=5.5
header_6=5.5
header_7=5.5
header_8=5.5
header_9=5.5
csno=12
rfpi=3
header_1=5.5
header_2=5.5
header_3=5.5
header_4=5.5
header_5=5.5
header_6=5.5
header_7=5.5
header_8=5.5
header_9=5.5

Tool completed successfully




Rob Dixon

unread,
Dec 15, 2011, 2:19:01 PM12/15/11
to Perl Beginners, Ken Slater, Chris Stinemetz
On 15/12/2011 16:09, Ken Slater wrote:
>
> I have not been following this too closely, but I don't understand the
> algorithm used to get the above output.

What is that Ken? If you don't understand the question then ask some
questions of your own!

> I would have named it @fieldValues since arrays usually hold multiple values

Renaming variables will never fix a problem as long as 'use strict
"vars"' is in effect. It is a gesture towards better code and no more.

>> my @apxScript=(@headerNames, @fieldValue); print Dumper \@headerNames;
>> print Dumper \@fieldValue; print Dumper \@apxScript;
>
> HTH, Ken

This is the source of the OP's misunderstanding, yet you make no comment
at all.

Rob

Chris Stinemetz

unread,
Dec 15, 2011, 2:22:04 PM12/15/11
to Rob Dixon, Perl Beginners
>
> Tool completed successfully
>

Thank you Rob! This is what I was trying to accomplish. I'm going to
have to research to find out exactly what you did.

Thanks agian,

Chris

Shlomi Fish

unread,
Dec 15, 2011, 2:54:41 PM12/15/11
to Chris Stinemetz, Dr.Ruud, begi...@perl.org
Hi Chris,
Is that your company's policy, or do you just lack root access? If it's the
latter, then see the various resources at http://perl-begin.org/topics/cpan/ ,
so you can see how to install Perl modules from CPAN under your home directory.

Regards,

Shlomi Fish

--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
First stop for Perl beginners - http://perl-begin.org/

Knuth is not God! It took him two days to build the Roman Empire.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

Chris Stinemetz

unread,
Dec 15, 2011, 4:29:08 PM12/15/11
to Shlomi Fish, Dr.Ruud, begi...@perl.org
>
> Is that your company's policy, or do you just lack root access? If it's the
> latter, then see the various resources at http://perl-begin.org/topics/cpan/ ,
> so you can see how to install Perl modules from CPAN under your home directory.
>

It isn't a company policy just circumstance. The unix box I'm using
doesn't support DNS nameserver lookup or a C compiler.

I'm currently using Perl 5.6.1 which doesnt' support local::lib and I
can't install perlbrew to upgrade my Perl version due to the fact I
can't figure out to get wget to work correctly without having DNS
nameserver lookup capabilities.

Any suggestions?

Thank you,

Chris

Shlomi Fish

unread,
Dec 16, 2011, 3:38:04 AM12/16/11
to Chris Stinemetz, Dr.Ruud, begi...@perl.org
Hi Chris,
It sounds like a really mal-functioning UNIX system. Why isn't it getting fixed
to support DNS and a C compiler?

Regards,

Shlomi Fish

>
> Thank you,
>
> Chris
>



--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
"Humanity" - Parody of Modern Life - http://shlom.in/humanity

Mastering ‘cat’ is almost as difficult as herding cats.
http://www.shlomifish.org/humour/bits/Mastering-Cat/

shawn wilson

unread,
Dec 16, 2011, 7:25:43 AM12/16/11
to Chris Stinemetz, Shlomi Fish, Dr.Ruud, begi...@perl.org
On Thursday, December 15, 2011, Chris Stinemetz <chrisst...@gmail.com>
wrote:

>
> It isn't a company policy just circumstance. The unix box I'm using
> doesn't support DNS nameserver lookup or a C compiler.
>
> I'm currently using Perl 5.6.1 which doesnt' support local::lib and I
> can't install perlbrew to upgrade my Perl version due to the fact I
> can't figure out to get wget to work correctly without having DNS
> nameserver lookup capabilities.
>

You could download perlbrew on another box and scp / rsync / rcp / netcat
it over. This, however won't help much without a compiler.

> Any suggestions?
>

Tried doing what you need under windows? If you tied both of my hands, I
probably wouldn't be able to operate a computer either.

I'd consider starting by making my system usable by finding and installing
the c compiler.

theba...@gmail.com

unread,
Dec 14, 2011, 6:51:48 PM12/14/11
to begi...@perl.org
split() splits on whitespace by default. so the "\s+/" is
optional.

$_ = "3 element array";
@words = split;

Chris Stinemetz

unread,
Dec 16, 2011, 10:36:53 PM12/16/11
to Rob Dixon, Perl Beginners
This program does all I need it to do. I am having some difficulty
wrapping my head around it though. Mainly the for loop. Did Rob use
special varible?

If any one can explain it to me so I can have a better understanding
that would be great!

Thanks,

Chris

Brandon McCaig

unread,
Dec 17, 2011, 2:04:36 PM12/17/11
to Chris Stinemetz, Rob Dixon, Perl Beginners
On Fri, Dec 16, 2011 at 09:36:53PM -0600, Chris Stinemetz wrote:
> This program does all I need it to do. I am having some difficulty
> wrapping my head around it though. Mainly the for loop. Did Rob use
> special varible?
>
> If any one can explain it to me so I can have a better understanding
> that would be great!

Is this the part that you don't understand?

> >    for my $i (0 .. $#headers) {
> >      printf "%s=%s\n", $headers[$i], $data[$i];
> >    }

There are no special variables here. $#headers is the upper-bound
(i.e., last) index of the @headers array. It uses the $#
"operator"[1] to do it. It's equivalent to @headers - 1 (AKA the
length of the @headers array minus one, because arrays are
0-indexed in Perl). It seems to be mentioned in 'perldoc
perldata', and you can find examples of its usage in a few other
documents with something like /\$# (i.e., search forward in the
pager).

The rest is pretty self explanitory. He's using the printf
function with a format string to create the name and value line
output.

HTH,


--
Brandon McCaig <bamc...@gmail.com> <bamc...@castopulence.org>
Castopulence Software <https://www.castopulence.org/>
Blog <http://www.bamccaig.com/>
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'


[1] I don't know if it's a sigil, operator, or something else;
but it behaves a bit like both, I guess.

signature.asc
0 new messages