logo
down
shadow

Perl Foreach until loop


Perl Foreach until loop

By : Jennifer Hartley
Date : November 17 2020, 01:00 AM
may help you . It looks like you need to keep just the latest value of each parameter, and save a set of values to a list on each occurrence of ---End Step---.
This program does exactly that. I have used Data::Dump only to demonstrate that the resultant data structure is as requested.
code :
use strict;
use warnings;

open my $fh, '<', 'data.txt' or die $!;

my (@data, %rowdata);

while (<$fh>) {
  if ( /^\s*(\w+) \s* : \s* (.*\S) /x or / \b (stepName) \s* (\w+) : /x ) {
    $rowdata{$1} = $2;
  }
  elsif ( /---End Step---/ ) {
    push @data, { %rowdata };
  }
}

use Data::Dump;
dd \@data;
[
  {
    Id       => 214215,
    jName    => "Bar_Start",
    joutcome => "warning",
    jstatus  => "running",
    outcome  => "success",
    pName    => "Bar",
    status   => "completed",
    stepName => "Start",
    Time     => "5:07",
  },
  {
    Id       => 214215,
    jName    => "Bar_Start",
    joutcome => "warning",
    jstatus  => "running",
    outcome  => "success",
    pName    => "Bar",
    status   => "completed",
    stepName => "A",
    Time     => "5:07",
  },
  {
    Id       => 214215,
    jName    => "Bar_Start",
    joutcome => "warning",
    jstatus  => "running",
    outcome  => "success",
    pName    => "Bar",
    status   => "completed",
    stepName => "B",
    Time     => "5:07",
  },
  {
    Id       => 214215,
    jName    => "Bar_Start",
    joutcome => "warning",
    jstatus  => "running",
    outcome  => "success",
    pName    => "Bar",
    status   => "completed",
    stepName => "C",
    Time     => "5:07",
  },
  {
    Id       => 214215,
    jName    => "Bar_Start",
    joutcome => "warning",
    jstatus  => "running",
    outcome  => "warning",
    pName    => "Bar",
    status   => "completed",
    stepName => "DC",
    Time     => "5:07",
  },
  {
    Id       => 123456,
    jName    => "Foo_Start",
    joutcome => "success",
    jstatus  => "completed",
    outcome  => "success",
    pName    => "Foo",
    status   => "completed",
    stepName => "A",
    Time     => "11:08",
  },
  {
    Id       => 123456,
    jName    => "Foo_Start",
    joutcome => "success",
    jstatus  => "completed",
    outcome  => "success",
    pName    => "Foo",
    status   => "completed",
    stepName => "AD",
    Time     => "11:08",
  },
]
What would be the appropriate call to an array such as this?

If I wanted to do a `foreach` loop of it, would it be something like

    foreach %key (@data) {
      if (key{jName} =~ Foo_start) {
         #then do this
      }
    }
for my $rowdata (@data) {
  next unless $rowdata->{jName} eq 'Foo_start';
  # Do stuff with $rowdata
}


Share : facebook icon twitter icon
Automatically get loop index in foreach loop in Perl

Automatically get loop index in foreach loop in Perl


By : Jim Dunstan
Date : March 29 2020, 07:55 AM
will be helpful for those in need Like codehead said, you'd have to iterate over the array indices instead of its elements. I prefer this variant over the C-style for loop:
code :
for my $i (0 .. $#x) {
    print "$i: $x[$i]\n";
}
Perl reading file a while loop then trying to add data in the month in a foreach loop

Perl reading file a while loop then trying to add data in the month in a foreach loop


By : kevin tolkmitt
Date : March 29 2020, 07:55 AM
Any of those help You might benefit from using a module to parse your timestamp. However, a simple fix might be to do something like this:
code :
my %sum;
while (<>) {  # instead of open on @ARGV, just use diamond operator
    my ($day, $month, $date, $time, $gmt, $year, $volt, $amp, 
        $pf, $watt, $voltamp) = split;
    $sum{"$year$month"}{'voltamp'} += $voltamp;
    $sum{"$year$month"}{'watt'} += $watt;
    ....
}
for my $month (keys %sum) {
    print "voltamp sum ($month): $sum{$month}{'voltamp'}\n";
    ....
}
Get next element of array inside a foreach loop in perl without using next/skipping to next iteration of loop

Get next element of array inside a foreach loop in perl without using next/skipping to next iteration of loop


By : Melee
Date : March 29 2020, 07:55 AM
I wish this helpful for you I am new to perl and am facing some problem in skipping to next element of an array inside a foreach loop without reiterating the loop. Suppose I have the following case where I am going through a array using a foreach loop. , You can use array indexes:
code :
for my $i (0 .. $#lines) {
    # ...
    print $lines[$i];
    if (cond()) {
        $lines[ $i + 1 ] =~ s/pattern/replace/g;
    }
}
for (my $i = 0; $i < $#list ; $i++) {
    # ...
}
#!/usr/bin/perl
use warnings;
use strict;


sub iterator {
    my $list = shift;
    my $i = 0;
    return sub {
        return if $i > $#$list;
        return $list->[$i++];
    }
}


my @list = qw/a b c d e f g h/;
my $get_next = iterator(\@list);

while (my $member = $get_next->()) {
    print "$member\n";
    if ('d' eq $member) {
        my $next = $get_next->();
        print uc $next, "\n";
    }
}
Running a nested while loop inside a foreach loop in Perl

Running a nested while loop inside a foreach loop in Perl


By : user3398709
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Yes, my advice would be:
Turn on strict and warnings. perltidy your code, use 3 argument open: open ( my $inputfile, "<", 'final_expression.txt' ); die if it doesn't open - the rest of your program is irrelevant. chomp $line you are iterating your filehandle, but once you've done this you're at the end of file for the next iteration of the foreach loop so your while loops becomes a null operation. Simplistically, reading the file into an array my @lines = ; would fix this.
code :
#!/usr/bin/perl -T

use strict;
use warnings;

use CGI qw(:cgi-lib :standard);
print "Content-type: text/html\n\n";

my $input       = param('sequence');
my $meanexpfile = "final_expression_complete.txt";
open( my $input_file, "<", $meanexpfile ) or die "unable to open file";
my @meanmatches;

my @regex = ( split /\s/, $input );
my @lines = <$input_file>;
chomp (@lines);
close($input_file) or warn $!;

foreach my $regex (@regex) {
    foreach my $line (@lines) {
        if ( $line =~ m/$regex\s(.+\n)/i ) {
            push( @meanmatches, $1 );
        }
    }

    my $average                  = average(@meanmatches);
    my $std_dev                  = std_dev( $average, @meanmatches );
    my $average_round            = sprintf( "%0.4f", $average );
    my $stdev_round              = sprintf( "%0.4f", $std_dev );
    my $coefficient_of_variation = $stdev_round / $average_round;
    my $cv_round = sprintf( "%0.4f", $coefficient_of_variation );

    print font(
        { color => "blue" }, "<br><B>$regex average: $average_round   
&nbspStandard deviation: $stdev_round&nbspCoefficient of 
variation(Cv): $cv_round</B>"
    );
}

sub average {
    my (@values) = @_;
    my $count    = scalar @values;
    my $total    = 0;
    $total += $_ for @values;

    return $count ? $total / $count : 0;
}

sub std_dev {
    my ( $average, @values ) = @_;

    my $count       = scalar @values;
    my $std_dev_sum = 0;
    $std_dev_sum += ( $_ - $average )**2 for @values;

    return $count ? sqrt( $std_dev_sum / $count ) : 0;
}
perl loop hash using each keyword on foreach loop

perl loop hash using each keyword on foreach loop


By : Jamie Ownsworth
Date : March 29 2020, 07:55 AM
I hope this helps you . foreach takes a list of values, and executes its loop body once per value, assigning some variable ($_ if otherwise unspecified) each time:
Related Posts Related Posts :
  • perl script using XML parser to read values in text file and replace it xml file
  • Using Perl Redis::Client::Hash can't locate object method "TIEHASH"
  • How do I replace a random line in a file without copying it into another file using Perl
  • Example curses/ncurses Perl widget?
  • Perl printing binary to files - cr lf
  • Linux, Backtrack, Perl, Bluesniff
  • perl cgi print header charset not work
  • Any way to set keepalive in DBI connect method in Perl
  • Perl script does not recognize paths which contains environment variables
  • How to stop Runaway Perl Process
  • Scoping variables in a Perl Test::More .t file
  • Perl Sort Array
  • Content-Type setting for .MTS files
  • Seek function not working in perl
  • Perl syntax error printing list index
  • perl matching syntax with mail filter
  • Error Handling Using Perl Tkx
  • Perl : calculation of time in format hh:mm:ss,sss
  • Remove element from array containing empty or whitespace character in Perl
  • Running an external command in Perl / Tkx without blocking the GUI (Windows)
  • Passing multiple values to a Perl program in a single command line argument
  • perl Parallel::ForkManager stuck in this script?
  • Unable to escape @ sign in perl
  • Loop to define buttons in Perl / Tkx
  • Why can't the Opsview_rest Perl script not find the OPSVIEW_URL_PREFIX Environment variable using $ENV{var} while it has
  • Get http-response header with Perl's File::Fetch
  • Perl Get the web content then writing it as a text file
  • perl $CGI::POST_MAX all parameters
  • login failed to remote host - prompt does not match at /usr/lib/perl5/site_perl/5.10.0/Net/Appliance/Session/Transport.p
  • How is a literal list having one element represented unambiguously in perl?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org