Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, this one is actually how you might do it in <em>perl</em>:</p> <pre><code># two must-have pragmas for perl development use strict; use warnings; </code></pre> <p>Perl allows for variables to be created as they are used, <code>$feldman = some_function()</code> means that you now have the variable <code>$feldman</code> in your local namespace. But the bad part about this is that you can type <code>$fldman</code> and take a long time finding out why what you thought was <code>$feldman</code> has no value. Turning on <a href="http://perldoc.perl.org/strict.html" rel="nofollow noreferrer"><em>strictures</em></a> means that your code fails to compile if it encounters an undeclared variable. You declare a variable with a <code>my</code> or <code>our</code> statement (or in older Perl code a <code>use vars</code> statement. </p> <p>Turning on <a href="http://perldoc.perl.org/warnings.html" rel="nofollow noreferrer"><code>warnings</code></a> just warns you when you're not getting values you expect. Often warnings tends to be too touchy, but they are <em>generally</em> a good thing to <em>develop</em> code with.</p> <pre><code>my %hash; # the base object for the data </code></pre> <p>Here, I've declared a hash variable that I creatively called <code>%hash</code>. The sigil (pronounced "sijil") "%" tells that it is a map of name-value pairs. This <a href="http://perldoc.perl.org/functions/my.html" rel="nofollow noreferrer"><code>my</code></a> statement <em>declared</em> the variable and makes it legal for the compiler. The compiler will warn me about any use of <code>%hsh</code>.</p> <p>The next item is a <a href="http://perldoc.perl.org/perlsyn.html#For-Loops" rel="nofollow noreferrer"><code>foreach</code></a> loop (which can be abbreviated "for"). The loop will process the list of lines in <code>@tmp_cycledef</code> assigning each one in turn to <code>$row</code>. ( <em>my</em> <code>$row</code>).</p> <ol> <li>We <a href="http://perldoc.perl.org/functions/chomp.html" rel="nofollow noreferrer"><code>chomp</code></a> the line first, removing the end-of-line character for that platform. </li> <li>We <a href="http://perldoc.perl.org/functions/split.html" rel="nofollow noreferrer"><code>split</code></a> the line on the '|' character, creating a list of strings that had been separated by a pipe. </li> <li>And then we store it in a two-layered hash. Since we want to group them by at least the first number. We could do this by array, and create an array at the location in the hash like so: <code>push @{$hash{$key}}, $val</code>, but I typically want to collapse duplicates (not that there were any duplicates in your sample.)</li> </ol> <p>Here:</p> <pre><code>foreach my $row ( @tmp_cycledef ) { chomp $row; # removes the end-of-line character when present. my ( $key, $val ) = split /\|/, $row; # One of the best ways to merge lists is a presence-of idea # with the hash holding whether the value is present $hash{$key}{$val} = 1; } </code></pre> <p>Once we have the data in the structure, we need to iterate both level of hash <a href="http://perldoc.perl.org/functions/keys.html" rel="nofollow noreferrer"><code>keys</code></a>. You wanted to separate the "top level" numbers by lines, but you wanted the second numbers concatenated on the same line. So we print a line for each of the first numbers and <a href="http://perldoc.perl.org/functions/join.html" rel="nofollow noreferrer"><code>join</code></a> the list of strings stored for each number on the same line, delimited by commas. We also <em>sort</em> the list: <code>{ $a &lt;=&gt; $b }</code> just takes to keys and <em>numerically</em> compares them. So you get a numeric order. </p> <pre><code># If they were alpha keys our sort routine, we would just likely say sort keys %hash foreach my $num ( sort { $a &lt;=&gt; $b } keys %hash ) { my $h = $hash{$num}; print "$num ", join( ',', sort { $a &lt;=&gt; $b } keys %$h ), "\n"; } </code></pre> <p>As I said in the comments, <a href="http://perldoc.perl.org/functions/sort.html" rel="nofollow noreferrer"><code>sort</code></a>, by default, sorts in character order so you can just say <code>sort keys %hash</code>. </p> <p>To help you out, you really need to read some of these: </p> <ul> <li><a href="http://perldoc.perl.org/strict.html" rel="nofollow noreferrer">strictures</a></li> <li><a href="http://perldoc.perl.org/warnings.html" rel="nofollow noreferrer">warnings</a></li> <li><a href="http://perldoc.perl.org/perldata.html#List-value-constructors" rel="nofollow noreferrer">perldata</a></li> <li><a href="http://perldoc.perl.org/perlfunc.html" rel="nofollow noreferrer">perlfunc</a> -- especially <a href="http://perldoc.perl.org/functions/my.html" rel="nofollow noreferrer"><code>my</code></a>, <a href="http://perldoc.perl.org/perlsyn.html#For-Loops" rel="nofollow noreferrer"><code>foreach</code></a>, <a href="http://perldoc.perl.org/functions/chomp.html" rel="nofollow noreferrer"><code>chomp</code></a>, <a href="http://perldoc.perl.org/functions/split.html" rel="nofollow noreferrer"><code>split</code></a>, <a href="http://perldoc.perl.org/functions/keys.html" rel="nofollow noreferrer"><code>keys</code></a>, <a href="http://perldoc.perl.org/functions/sort.html" rel="nofollow noreferrer"><code>sort</code></a> and <a href="http://perldoc.perl.org/functions/join.html" rel="nofollow noreferrer"><code>join</code></a></li> <li>And the <a href="http://perldoc.perl.org/perldsc.html" rel="nofollow noreferrer">data structure tutorial</a></li> </ul>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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