Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your second line has more than one space between the number group and the letter, so you probably want <code>\s+</code> both times rather than <code>\s</code> the second time.</p> <p>You won't necessarily know how many items you have in the <code>@value</code> array at the end, so you might want to put the printing into a <code>for</code> loop rather than assume you have a fifth item. (Maybe you know you want the first and fifth, however?) <strong>Follow-up</strong>: based on your edit, you have more than two entries after all. The version that I give below, using <code>split</code> and <code>\s+</code> captures the number and letter for all the lines. I'll adjust the print part of the script to show you what I mean.</p> <p>A few other things:</p> <ul> <li>You should always enable warnings.</li> <li>There's no reason to read the whole file into an array and then process through it line by line. Skip the <code>@body</code> array and just do what you need to in the <code>while</code> loop.</li> <li>Use the more modern form of <code>open</code> with lexical filehandles and three arguments.</li> <li><code>split</code> seems more straightforward here to me, rather than a regular expression with captures. Since you want to capture two specific parts of the line, you can use <code>split</code> with an array slice to grab those two items and feed them to <code>join</code>.</li> <li><code>@value</code> is not an especially helpful variable name, but I think you should at least make it plural. It's a good habit to get into, I think, insofar as the array stores your plural records. (That's not a hard and fast rule, but it bugged me here. This point is pretty minor.)</li> </ul> <p>Here's how all this might look:</p> <pre><code> #!/usr/bin/env perl use warnings; use strict; my @values; # open my $filehandle, '&lt;', '/usr/test' # or die "Can't open /usr/test: $!"; while (my $line = &lt;DATA&gt;) { chomp $line; push @values, join('', (split /\s+/, $line)[1..2]); } for my $record (@values) { print $record, "\n"; } __DATA__ 1389 E not 188 S yes 24 D yes 456 K not 2 Q yes </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload