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

Hi Monks,

I have these 2 lines in separate sections in a report:

"QR No N/A 1 1 1 0 0 0 0 0 0" "QR 1 1 1 0 157M 23:09:15.190448 00:42:28.205336 00:40:04.086054"

I find the strings using regex, then split them by \/+\ into 2 arrays. Then the arrays are joined after dropping the first 4 values in the second string.

My problem is the 157M value. I need to find the best way to detect and then multiply the 157 by 1000000 and then have that value in the array.

Should I do that in the string? Or after the split to array? (how would I loop the array, change the value?)

Thank you. (NOTE: I'm a mainframe sysprog,HL Assembler is my first language.)

Replies are listed 'Best First'.
Re: Find and change value.
by ikegami (Patriarch) on May 29, 2009 at 20:47 UTC
    After the split to array.
    BEGIN { my %muls = ( K => 1_000, M => 1_000_000, ); sub canonize_units { my ($v) = @_; $v =~ s/^(.*)(.)/ ( exists($muls{$2}) ? $1 * $muls{$2} : "$1$2" ) /e; } } my @line1_fields = split, ' ', ...; my @line2_fields = split, ' ', ...; $line2_fields[5] = canonize_units($line2_fields[5]); my @joined_fields = ( @line1_fields, @line2_fields[ 4 .. $#line2_fields ], );
Re: Find and change value.
by baxy77bax (Deacon) on May 29, 2009 at 21:09 UTC
    hi,

    i suppose you have some file for which you wish to iterate over. so one possible way to go would be

    use strict; #use warnings; my @array; my $count = 0; my $count_big = 0; while(<DATA>){ chomp($_); my @arraytmp = split(' ',$_); if ($count == 1){for(1..4){shift(@arraytmp)}} $arraytmp[1]=~ s/M/000000/g if ($count == 1); push(@{$array[$count_big]},@arraytmp); $count++; $count = 0 if ($count == 2); $count_big++ unless ($count); } __DATA__ QR No N/A 1 1 1 0 0 0 0 0 0 QR 1 1 1 0 157M 23:09:15.190448 00:42:28.205336 00:40:04.086054
    i think the code is straightforward and should work on large files.

    also i would always import the changes after the split to array.

    update:

    i totally missed the point!

    if ($arraytmp[1]=~ s/M//g && $count == 1){$arraytmp[1] *= 1000000}
    but i admit that ikegami's solution is more elegant :)
Re: Find and change value.
by DStaal (Chaplain) on May 29, 2009 at 20:59 UTC

    Edit: Deleted. It only appeared to work: wasn't converted into numeric properly.