Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Splitting Array with Variable Spaces

by ImpalaSS (Monk)
on Nov 02, 2000 at 23:33 UTC ( [id://39724]=perlquestion: print w/replies, xml ) Need Help??

ImpalaSS has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need help yet again :-). I have a table (ill post a small portion):
21039pa         5005CamdenSouth.3        -          2.4   0.023    50.1    51.9   104.3       -   3.11%  17.43%    2.3%   0.63%   2.53%  30.79%   7.72%       7   0.52%       6   0.94%      16   0.44%      29       0    0.0%       0    0.0%   nj5005CamdenSouth.3(5414)
2178aud         5449Pennington.1         -          8.7   0.392    17.4     8.5    34.5       -   2.84%  17.83%   25.1%   0.15%   2.94%  24.44%  33.24%       3   0.76%      49   4.82%      20   1.03%      72       0    0.0%       0    0.0%   nj5449Pennington.1(12282)
6631pa          5384Chatsworth.1         -         11.0   0.459    12.5    13.2    36.7       -   2.81%  17.67%   30.0%   0.08%   5.28%  26.26%  67.66%       4   1.85%      14   8.81%      38   3.05%      56       3    0.0%       0    0.0%   nj5384Chatsworth.1(5457)
15273pa         0395UpperOxford.3        -          2.3   0.048    10.5    11.6    24.4       -   2.79%   9.60%    9.5%   0.00%   4.00%  15.26%  45.62%       5   1.07%       0   0.00%       8   0.97%      13       0    0.0%       0    0.0%   pa0395UpperOxford.3(12414)
19919pa         0539FisherPark.1         -         22.4   0.110    33.9    27.3    83.6       -   2.42%  16.54%   26.8%   0.14%   1.13%  32.59%  24.54%       3   0.80%      17   2.23%      16   0.70%      36       0    0.0%       0    0.0%   pa0539FisherPark.1(19284)
27850pa         0557Poplar.2             -          0.5   0.170    14.0     6.0    20.5       -   2.34%   6.40%    2.5%   0.00%   1.90%  12.69%  46.68%       1   0.25%       1   1.64%       3   0.29%       5       0    0.0%       0    0.0%   pa0557Poplar.2(14256)
4269aud         0886Longmead.2           -          0.2   0.083     2.8     2.2     5.2       -   2.27%  20.54%    3.6%   0.00%  16.07%   0.00%  10.53%       0   0.00%       0   0.00%       2   1.03%       2       0    0.0%       0    0.0%   pa0886Longmead.2(37040)
15967pa         5458Kenmore.1            -          3.4   0.019    12.7    18.0    34.1       -   2.22%  18.03%   10.0%   0.12%   4.08%  39.05%   3.16%      11   1.10%      18   8.07%      14   0.84%      43       0    0.0%       0    0.0%   md5458Kenmore.1(9399)
1727aud         5029Trenton.2            -          6.5   0.104    63.4    41.2   111.1       -   2.12%  14.48%    5.8%   0.01%   1.52%  38.73%   9.76%      11   0.66%       9   4.09%      39   0.97%      59       0    0.0%       0    0.0%   nj5029Trenton.2(9324)
The thing my boss wants me to do is take the 14th column and compare it to the 11th. And if the 14th is greater than the 11th, print it, if not, dont print it. This is my question, i did, :
foreach $item(@sorted){ @new=split($item); print $new[13]; if($new[13] > $new [10]){ print $item; }
where sorted is the array (each row in the table is one item in the array).
My goal being, taking each line, then splitting it, placing it in a new array, and then comparing and printing.
The problem however, is that when the array is split, i split it at the " " and they have a variable amount of spaces btwn fields to make the columns line up. Any help would be greatly appreciated.
Thanks in advance
Dipul

Replies are listed 'Best First'.
Re: Splitting Array with Variable Spaces
by btrott (Parson) on Nov 02, 2000 at 23:37 UTC
    You want something like
    my @new = split /\s+/, $item;
    This will split on any whitespace (including tabs, etc.). I know that's not explicitly what you're using, but it's useful sometimes to be more flexible.

    By the way, you can save yourself some space by only saving the fields you're interested in after the split:

    my($eleven, $fourteen) = (split /\s+/, $item)[10,13];
    That's an array slice, and it's quite nice.
Re: Splitting Array with Variable Spaces
by mwp (Hermit) on Nov 02, 2000 at 23:39 UTC
    # split $string on a space @ary = split(/ /, $string); # split $string on one or more space @ary = split(/[ ]+/, $string);

    There are also many other useful operators for a situation like this. If you haven't already, I recommend you read perlre.

    HTH!

    Alakaboo

Re: Splitting Array with Variable Spaces
by arturo (Vicar) on Nov 02, 2000 at 23:40 UTC

    If you are *sure* that the entries are separated by spaces and not by tabs AND THAT no entry will contain spaces, you can use the highly effective:

    my @fields = split /\s+/, $data_line;

    To split on "one or more bits of whitespace" (tabs, carriage returns, newlines, etc.). If you want to match spaces specifically, change the \s to a space.

    perlman:perlre for all the goodness of Perl regexen.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Splitting Array with Variable Spaces
by fundflow (Chaplain) on Nov 03, 2000 at 00:11 UTC
    Note that you might have a problem with split() if the fields contain white-spaces.

    It seems like your table is very regular and thus it might be better to use substr(). This will probably be faster.

    if( substr($item, $start_col1, $width1) < substr($item, $start_col2, $width2)) { .... }

awk and tr heresy (Re: Splitting Array with Variable Spaces)
by japhy (Canon) on Nov 03, 2000 at 02:25 UTC
    Why use Perl? Do you have to? I'd do something this trivial in tr and awk if I honestly had a choice: tr -d '%' < INPUT | awk '($11 > $14)' That, however, removes ALL the % signs, which may be less than optimal. Try this, then: awk '0+substr($11,0,length($11)-1) > 0+substr($14,0,length($14)-1)' INPUT If you really want to use Perl, here's a command-one-liner: perl -lane 'print if $F[10] > $F[13]' INPUT or even perl -ape '$_ x= ($F[10] > $F[13])' INPUT If you need to use it in a program, you'd probably want to stick with the unpack() and substr() solutions given you.

    There's more than one way to do it, and dammit, some of them aren't Perl! ;)

    $_="goto+F.print+chop;\n=yhpaj";F1:eval
RE: Splitting Array with Variable Spaces
by dws (Chancellor) on Nov 03, 2000 at 01:25 UTC
    split() isn't the only hammer in the toolbox. Consider unpack(). There's a good example of using it for just this type of problem on pages 37-41 of "The Perl Cookbook".
Re: Splitting Array with Variable Spaces
by ImpalaSS (Monk) on Nov 02, 2000 at 23:35 UTC
    UPDATE the code i posted is wrong, it was not the code i used.
    the code i used is:
    foreach $item(@sorted){ @new=split(/ /,$item); print $new[13]; if($new[13] > $new [10]){ print $item; }
    Sorry for the confusion
    Dipul
      Try splitting on /\s+/. For example:
      foreach $item(@sorted){ @new=split(/\s+/,$item); print $new[13]; if($new[13] > $new [10]){ print $item; }


      Cheers,
      Shendal
Re: Splitting Array with Variable Spaces
by Anonymous Monk on Nov 03, 2000 at 00:12 UTC
    @items = split(" ", $_);

    will work also
Re: Splitting Array with Variable Spaces
by ImpalaSS (Monk) on Nov 02, 2000 at 23:44 UTC
    Wow, unreal. Thanks Guys
    Dipul

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://39724]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-19 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found