Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Stuck on perl printing same lines.
New Posts  All Forums:Forum Nav:

Stuck on perl printing same lines.

post #1 of 3
Thread Starter 
Code:
while (my $line = <INPUT>) {
        if ($line =~ /$source/) {
                my @source = split / /, $line;
                my $endvalue1 = $source[4];
  #            print OUTPUT $endvalue1;
        }
        elsif ($line =~ /$target/) { 
                my @target = split / /, $line;
                my $endvalue2 =  $target[1];
#              print OUTPUT $endvalue2;
        } 
        elsif ($line =~ /$reason/) {
                my $endvalue3 = $line;
 #             print OUTPUT $endvalue3;
        }
}

This is in perl. Just posting relevant section, is a small script. How can I take $endvalue[123] and print them out on the same line.

Basically this...

print OUTPUT "$endvalue1 $endvalue2 $endvalue3";

That or how can I make those local variables into global ones.
Edited by Tadaen Sylvermane - 7/7/16 at 3:46pm
 
Micromachine
(8 items)
 
 
CPUMotherboardGraphicsRAM
i7 4500u Inspiron 15 7000 Integrated 8gb 
Hard DriveOSMouse
120gb Samsung EVO SSD Debian Logitech m705 
CPUMotherboardRAMHard Drive
AMD 5350 Kabini AsRock AM1H-ITX 8gb Gskill Ripjaws (2x4) Samsung 120 Evo 
Hard DriveHard DriveOSCase
WD 1tb 2.5 WD 2tb USB 3.0 Ubuntu Server 16.04 Antec ISK 
  hide details  
Reply
 
Micromachine
(8 items)
 
 
CPUMotherboardGraphicsRAM
i7 4500u Inspiron 15 7000 Integrated 8gb 
Hard DriveOSMouse
120gb Samsung EVO SSD Debian Logitech m705 
CPUMotherboardRAMHard Drive
AMD 5350 Kabini AsRock AM1H-ITX 8gb Gskill Ripjaws (2x4) Samsung 120 Evo 
Hard DriveHard DriveOSCase
WD 1tb 2.5 WD 2tb USB 3.0 Ubuntu Server 16.04 Antec ISK 
  hide details  
Reply
post #2 of 3
Can you turn that part of the script into a subroutine, return an array and call it from elsewhere?
something like:
Code:
sub controller {
    my $values = getValues();
    $stringify = join(" ", @values);
    print OUTPUT $stringify;
}


sub getValues {
    while (my $line = <INPUT>) {
        #....
    }
    @values = ($endvalue1, $endvalue2, $endvalue3);
    return @values;
}
Ol'Faithful
(11 items)
 
  
CPUMotherboardGraphicsRAM
Intel Xeon x5660 P6X58D-E XFX AMD Radeon R9 280X Corsair Vengeance 24 GB Triple Chanel 6x4GB 
Optical DriveCoolingOSMonitor
Lite-On DVD-RW Noctua DH-14 CPU Cooler Windows 7 Pro x64 Samsung 24" LED 1080p 5ms DVI (S23A300B) 
PowerMouseAudio
Thermaltake Toughpower 750W Gold Corsair M95 Onboard :S 
  hide details  
Reply
Ol'Faithful
(11 items)
 
  
CPUMotherboardGraphicsRAM
Intel Xeon x5660 P6X58D-E XFX AMD Radeon R9 280X Corsair Vengeance 24 GB Triple Chanel 6x4GB 
Optical DriveCoolingOSMonitor
Lite-On DVD-RW Noctua DH-14 CPU Cooler Windows 7 Pro x64 Samsung 24" LED 1080p 5ms DVI (S23A300B) 
PowerMouseAudio
Thermaltake Toughpower 750W Gold Corsair M95 Onboard :S 
  hide details  
Reply
post #3 of 3
Thread Starter 
Is a good idea. I should have marked it solved. Got what I was looking for by writing to a variable then printing the variable as such.
Code:
my $buffer = "";

open my $writeout, ">>", \$buffer;

...

print STDOUT "$buffer\n";
 
Micromachine
(8 items)
 
 
CPUMotherboardGraphicsRAM
i7 4500u Inspiron 15 7000 Integrated 8gb 
Hard DriveOSMouse
120gb Samsung EVO SSD Debian Logitech m705 
CPUMotherboardRAMHard Drive
AMD 5350 Kabini AsRock AM1H-ITX 8gb Gskill Ripjaws (2x4) Samsung 120 Evo 
Hard DriveHard DriveOSCase
WD 1tb 2.5 WD 2tb USB 3.0 Ubuntu Server 16.04 Antec ISK 
  hide details  
Reply
 
Micromachine
(8 items)
 
 
CPUMotherboardGraphicsRAM
i7 4500u Inspiron 15 7000 Integrated 8gb 
Hard DriveOSMouse
120gb Samsung EVO SSD Debian Logitech m705 
CPUMotherboardRAMHard Drive
AMD 5350 Kabini AsRock AM1H-ITX 8gb Gskill Ripjaws (2x4) Samsung 120 Evo 
Hard DriveHard DriveOSCase
WD 1tb 2.5 WD 2tb USB 3.0 Ubuntu Server 16.04 Antec ISK 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Stuck on perl printing same lines.