help w split and array of array plse
Author |
Message |
Dick Penn #1 / 7
|
 help w split and array of array plse
Am trying to convert an array of strings (grep'd from a file) into an array of arrays, ie, a 2 dimensioned matrix of numbers. I can't get the split to work, so I haven't tried the array of arrays part. I have so far: =======code use strict;
'SPH 083000 111200 111300 111100 111280 503 959', 'SPH 084000 111250 111380 111200 111370 354 115', 'SPH 084500 111350 111390 111050 111100 353 747' ); #how to ignore the leading alphas? #should yield a matrix numbrs[dim 3][dim 7] #the ",3" below was just for a test, really want all 7 of above number groups
eventually
print "$#numbrs\n";
====output 2 SPH|083000|111200 111300 111100 111280 503 959 # ^ # where did all this print out from ^ onward come from? #numbrs has only 3 entitites in it THANKS -- {*filter*} Penny
|
Mon, 07 Mar 2005 00:49:06 GMT |
|
 |
Vinny Murph #2 / 7
|
 help w split and array of array plse
Quote:
> Am trying to convert an array of strings (grep'd from a file) into an array > of arrays, ie, a 2 dimensioned matrix of numbers. I can't get the split to > work, so I haven't tried the array of arrays part. I have so far: > =======code > use strict;
> 'SPH 083000 111200 111300 111100 111280 503 959', > 'SPH 084000 111250 111380 111200 111370 354 115', > 'SPH 084500 111350 111390 111050 111100 353 747' ); > #how to ignore the leading alphas? > #should yield a matrix numbrs[dim 3][dim 7] > #the ",3" below was just for a test, really want all 7 of above number > groups
> eventually
> print "$#numbrs\n";
> ====output > 2 > SPH|083000|111200 111300 111100 111280 503 959 > # ^ > # where did all this print out from ^ onward come from? > #numbrs has only 3 entitites in it
You are doing more work than you need to: #!/usr/bin/perl -w use strict; use Data::Dumper;
while (<DATA>) { # skip all but numbers... Note if you had SPH1 it # would grab the 1.
Quote: }
# to get individual entries in the line do the following:
print $ra->[2], "\n"; # print out the 3rd value of each line Quote: }
__DATA__ SPH 083000 111200 111300 111100 111280 503 959 SPH 084000 111250 111380 111200 111370 354 115 SPH 084500 111350 111390 111050 111100 353 747 hth - Vinny
|
Sun, 06 Mar 2005 21:20:20 GMT |
|
 |
Dick Penn #3 / 7
|
 help w split and array of array plse
Thanks for your reply. I've tried your suggested [ m/(\d+)/g ] and I keep getting the "SHP" back in the match answer, why is that, cannot understand, new code follows: =====new code use strict; use Data::Dumper;
'SPH 083000 111200 111300 111100 111280 503 959', 'SPH 084000 111250 111380 111200 111370 354 115', 'SPH 084500 111350 111390 111050 111100 353 747' );
print "$#numbrs\n";
====== new results 0 $VAR1 = [ 'SPH 083000 111200 111300 111100 111280 503 959' ]; #How can the "SPH" being sucked-up by m/\d+/g ?? Any ideas appreciated. {*filter*} Penny
Quote:
> > Am trying to convert an array of strings (grep'd from a file) into an array > > of arrays, ie, a 2 dimensioned matrix of numbers. I can't get the split to > > work, so I haven't tried the array of arrays part. I have so far: > > =======code > > use strict;
> > 'SPH 083000 111200 111300 111100 111280 503 959', > > 'SPH 084000 111250 111380 111200 111370 354 115', > > 'SPH 084500 111350 111390 111050 111100 353 747' ); > > #how to ignore the leading alphas? > > #should yield a matrix numbrs[dim 3][dim 7] > > #the ",3" below was just for a test, really want all 7 of above number > > groups
> > eventually
> > print "$#numbrs\n";
> > ====output > > 2 > > SPH|083000|111200 111300 111100 111280 503 959 > > # ^ > > # where did all this print out from ^ onward come from? > > #numbrs has only 3 entitites in it > You are doing more work than you need to: > #!/usr/bin/perl -w > use strict; > use Data::Dumper;
> while (<DATA>) { > # skip all but numbers... Note if you had SPH1 it > # would grab the 1.
> }
> # to get individual entries in the line do the following:
> print $ra->[2], "\n"; # print out the 3rd value of each line > } > __DATA__ > SPH 083000 111200 111300 111100 111280 503 959 > SPH 084000 111250 111380 111200 111370 354 115 > SPH 084500 111350 111390 111050 111100 353 747 > hth > - > Vinny
|
Mon, 07 Mar 2005 04:32:52 GMT |
|
 |
John W. Krah #4 / 7
|
 help w split and array of array plse
Quote:
> Am trying to convert an array of strings (grep'd from a file) into an array > of arrays, ie, a 2 dimensioned matrix of numbers. I can't get the split to > work, so I haven't tried the array of arrays part. I have so far: > =======code > use strict;
> 'SPH 083000 111200 111300 111100 111280 503 959', > 'SPH 084000 111250 111380 111200 111370 354 115', > 'SPH 084500 111350 111390 111050 111100 353 747' );
John -- use Perl; program fulfillment
|
Mon, 07 Mar 2005 04:46:46 GMT |
|
 |
Anno Sieg #5 / 7
|
 help w split and array of array plse
[Please don't top-post. I have put your reply in context]
[...] Quote: > > #!/usr/bin/perl -w > > use strict; > > use Data::Dumper;
> > while (<DATA>) { > > # skip all but numbers... Note if you had SPH1 it > > # would grab the 1.
> > }
> > # to get individual entries in the line do the following:
> > print $ra->[2], "\n"; # print out the 3rd value of each line > > } > > __DATA__ > > SPH 083000 111200 111300 111100 111280 503 959 > > SPH 084000 111250 111380 111200 111370 354 115 > > SPH 084500 111350 111390 111050 111100 353 747 > Thanks for your reply. I've tried your suggested [ m/(\d+)/g ] and I keep > getting the "SHP" back in the match answer, why is that, cannot understand, > new code follows: > =====new code > use strict;
You haven't switched on warnings. Big mistake. Quote: > use Data::Dumper;
> 'SPH 083000 111200 111300 111100 111280 503 959', > 'SPH 084000 111250 111380 111200 111370 354 115', > 'SPH 084500 111350 111390 111050 111100 353 747' );
How did you derive *that* from Vinny's suggestion? It is wrong in a number of ways. The right-hand side of the assignment is an anonymous list, hence a scalar. You are assigning that to an array. While this is legal
better, to $numbers). What you have inside the [] on the right side will be the content of the anonymous list. To do that, you have to bind the regular expression to $lines[0], using the =~ operator. You have written a comma, which gives the expression a different meaning entirely. The pattern match is applied to the default $_, which is undefined at this point. Warnings would have told you so. The result of that is an empty list, which contributes nothing. So, in effect, you are saying
an anonymous list of (again) a single element, the original $lines[0]. That is what your output shows. Quote: > print "$#numbrs\n";
> ====== new results > 0
Quote: > $VAR1 = [ > 'SPH 083000 111200 111300 111100 111280 503 959' > ];
...which is a listref to a list containing a single string. Quote: > #How can the "SPH" being sucked-up by m/\d+/g ??
my $numbers = [ /\d+/g =~ $lines[0] ]; Anno
|
Mon, 07 Mar 2005 09:24:14 GMT |
|
 |
#6 / 7
|
 help w split and array of array plse
|
Wed, 18 Jun 1902 01:00:00 GMT |
|
 |
Dick Penn #7 / 7
|
 help w split and array of array plse
Vinny, Anno, & John, Your replies appreciated. I understand your points & my mistakes. {*filter*} Penny
|
Mon, 07 Mar 2005 16:17:12 GMT |
|
|
|