Reputation: 17
I've got a string input, lets say it's:
junk 0 0 :::1000
junk 0 0 :::1001
junk 0 0 :::1002
junk 0 0 :::1009
I want only the last digit of the 100X
number, and I want to place it in a string, and populate an array with these strings.
I have the following code
my @lines = split /^/, $input;
foreach my $line (@lines)
{
my ($garb, $long_ports) = (split /\s*:::\s*/, $line);
my ($garb2, $ports) = (split /10/, $long_ports);
if ($ports < 10)
{
my ($garb3, $ports2) = (split /0/, $ports);
#Add 0 since 0 port is split to empty string
if (length($ports2) == 0)
{
$ports2 = "0$ports2";
}
#Create file name format
@locked_ports = ".X$ports2-lock";
}
}
When I print "@locked_ports"
I only get the value .X9-lock
, when I want all 4.
How do I make it so @locked_ports
contains all 4 of the strings?:
.X0-lock
.X1-lock
.X2-lock
.X9-lock
Upvotes: 1
Views: 816
Reputation: 1068
You simply need to use push
to add each desired result to the end of the array. Here's an example snippet:
foreach my $line (@lines) {
chomp $line;
if ($line =~ /(\d)$/) {
push @ports, ".X$1-lock";
}
}
Upvotes: 2
Reputation: 2071
it runs, when you modify your code as follows
my @lines = split /^/, $input;
foreach my $line (@lines)
{
chomp($line);
my ($garb, $long_ports) = (split /\s*:::\s*/, $line);
my ($garb2, $ports) = (split /10/, $long_ports);
if ($ports < 10)
{
my ($garb3, $ports2) = (split /0/, $ports);
#Add 0 since 0 port is split to empty string
if (length($ports2) == 0)
{
$ports2 = "0$ports2";
}
#Create file name format
push @locked_ports, ".X$ports2-lock";
}
}
you have to push
the locks at the end of @locked_ports
and to chomp
the $line to get rid of newlines
Upvotes: 1
Reputation: 5805
one quick thing to try is change this:
@locked_ports = ".X$ports2-lock";
to:
push @locked_ports,"X${ports2}-lock";
Also make sure you are using use strict'
and use warnings;
and post the snippet where you declare @locked_ports
and print it.
Upvotes: 1