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

This code pretends to build a layer progression as per user input but I cannot think of a way to get the last layer into the print out

Simple task (so I thought!)- building a hierarchical progression of layers. Say the user wants to build a metal stack from M1 to M5, ie: M1->v1->M2->v2->M3->v3->M4->v4->M5.

I cannot figure out how to add the last metal (M5) into my logic, any ideas on this?

In fact, I want to add some dimensions to these metals and make sure the via is laid out within those dimensions. IS there a Perl library that I can use for that?? but that's for a later day after I complete the last array member

Anyways, this is what I have so far.

#!/usr/bin/perl use 5.14.2; use strict; use warnings; use Data::Dumper; use diagnostics; sub metal_stack_order { my @unsorted =@_ ; #make sure to arrange to ascending order my @sorted = sort { $a <=>$b} @unsorted; my ($Start,$End)=($sorted[0],$sorted[1]); for (my $i=$Start; $i <= ($End-1) ; $i++){ print "M$i --> V$i-->"; } } say "Enter Lowest metal layer like => \"m1\"\n"; my $input1=<>; say " Enter Highest metal like => \"m5 \"\n"; my $input2=<>; #parsing the metal layer (digits) my ($num_1)=$input1=~ m/(\d)+/; my ($num_2)=$input2=~ m/(\d)+/; #Ony exception, no equal metal layers if ($num_1 == $num_2){ say "You cannot do a layout the same layer\n"; exit; } metal_stack_order($num_1,$num_2);

Thanks for the help!

Replies are listed 'Best First'.
Re: Can't figure out how to include LAST member of the array
by haukex (Archbishop) on Feb 09, 2021 at 19:41 UTC

    How about print "M$End\n"; after the for loop? And note you talk about an array but your code doesn't include one (for output, that is), but I assume you'd probably want to push elements onto a new array instead of printing them.

    BTW, your regexes for getting the numbers from the input aren't quite right, the + quantifier needs to be inside the capture group, and since you're already using regexes, you can use them for validation too. For example, my ($num_1) = $input1 =~ m/^m(\d+)$/i or die "bad input: $input1";

    A few edits shortly after posting.

      You're right, I want to push it into some hash once I knew how to tackle the last element. I will make that change and for my regex as well. Update: I think I am going to create a table for the values of each metal and via then reference those from a table. Not sure how I will do that yet. I will try. thanks!

Re: Can't figure out how to include LAST member of the array
by jdporter (Paladin) on Feb 09, 2021 at 20:27 UTC

    How about adding a

    print "M$End";
    after the for loop?

    That is:

    for (my $i=$Start; $i <= ($End-1) ; $i++){ print "M$i --> V$i-->"; } print "M$End";

    But if I were doing this, I'd probably do it like so:

    for (my $i=$Start; $i <= $End; $i++) { print "M$i"; print " --> V$i-->" unless $i == $End; }

      Thanks for the help. I did not know I could write the unless condition this way. I will try that! Thanks

Re: Can't figure out how to include LAST member of the array
by kcott (Archbishop) on Feb 10, 2021 at 05:10 UTC

    G'day perlynewby,

    "IS there a Perl library that I can use for that??"

    List::MoreUtils has a zip (aka mesh) function which you could use something like this:

    $ perl -e ' use strict; use warnings; use List::MoreUtils "zip"; my @sorted = 1..5; my @M = map "M$_", @sorted; my @v = map "v$_", @sorted[0..$#sorted-1]; print join "->", grep defined, zip @M, @v; ' M1->v1->M2->v2->M3->v3->M4->v4->M5

    — Ken