i want to create a variable in perl of different length from 1 to 255 - perl

Like how we use a regex to match a variable, instead i want to use regex to assign variable-length to my variable.
eg: lets take a counter $i, which $i run in a for loop
for($i=0; $i < 256; $i++)
{
$myVariable = a{$i};
}
i want $myVariable to be of different length, based on the counter variable $i
for instance, if $i is 5, then $myVariabe should be "aaaaa"

for my $i (1..255) {
my $myVariable = 'a' x $i;
...
}
or
my $myVariable;
for my $i (1..255) {
$myVariable .= 'a';
...
}

Related

Concatenate int variable from for loop with string

I'm passing in possibly four file names: FileName1, FileName2, FileName3, FileName4.
Some may be empty, some not. Because of this I need to check if they are empty before using them. So instead of using four if statements I thought I would just loop through them. How I wanted to do that was like this:
for ($i = 1; $i -lt 5; $i++) {
$FileName = $FileName + $i
Write-Host $FileName
}
So I can get Filename1, FileName2, FileName3, FileName4. Instead I get 1, 1 + 2, etc.
I've also tried $FileName$i, $FileName"$($i)", $FileName + "$($i)".
Any ideas?
EDIT
FileName1, FileName2, FileName3, FileName4 are variables that are passed to the script. They could be FileName1="Budget2018.xlsx", FileName2="MonthlyExpenses.xlx", FileName3="", FileName4="". Or all four variables can contain values ... or just FileName1 can contain a value, etc.
I need to check if they are empty before I continue on processing them. So rather than use 4 if statements to check if they are empty I thought I could loop through them referencing the variables as $Filename$i where $i would be the value 1 to 4. I'm trying to concatenate the two values together to represent the variables that are the parameters.
This produces FileName1, FileName2 etc, but I'm not sure how that squares with "I'm passing in four possible filenames", as there's no list in your script.
For($i = 1; $i -lt 5; $i++){
$FileName = "FileName$i"
Write-Host $FileName
}
If I understand what you want then this should do it:
For($i = 1; $i -lt 5; $i++){
$FileName = 'FileName{0}' -f $i
Write-Host $FileName
}
If you want to define the root portion of the name as a variable then:
$root = 'FileName'
For($i = 1; $i -lt 5; $i++){
$FileName = '{0}{1}' -f $root,$i
Write-Host $FileName
}
Ok, so based on the most recent edit, here is a way to do what I now think you want:
1..4 | %{(ls variable:\$("FileName$_")).value}
If you have a single filename, and you want a list created based off that, you can do something like this:
$FileName = 'test'
$FileList = for ($i = 1; $i -le 4; $i++) { "$FileName$i" }
$FileList
# => test1
# => test2
# => test3
# => test4
But I suspect you have a list of files given your question had "check if null/empty" mentioned in it.

How is a for loop written in Perl?

How do you write a for loop in Perl? I've tried the following to no avail:
my $i = 0;
for my $foo; do
$i=$[i+1];
done < bar;
There are two types of for loop in perl, in addition to the for statement modifier. They look like this:
# c-style for loop
for ( my $i = 0; $i < 12; ++$i ) {
...
}
# regular for loop
for my $i (0..11) {
...
}
# statement modifier
... for 0..11;
To write a for loop in Perl the syntax would look like this:
for my $foo (#array) {
print "$foo\n";
}
The syntax of a for loop in Perl is:
for ( init; condition; increment )
{
statement(s);
}
Example:
for (my $i=0; $i <= 9; $i++)
{
print "$i\n";
}
for
for(my $i = 1; $i < 5; $i++) {
something...
}
or foreach
my #names = qw(Steve Bill Connor Bradley);
foreach my $name (#names) {
something...
}

Perl - Trouble With Loop Variables

I'm fairly new to coding in Perl and am used to using C-style for loops. I'm not sure why the following program never prints a value of 4 for $l:
use strict;
use warnings;
my $minL = 4;
for (my $l = $minL; $l > 0; $l--) {
for (my $i = 0; $i + $l < $minL; $i++) {
print "$i $l\n";
}
}
Many thanks in advance.
Your inner for loop has the condition $i + $l < $minL. If $l == $minL, then $i + $l < $minL will be false even if $i is 0, so the loop runs 0 times and never prints anything.
Maybe you wanted to use <= for the condition?
By the way, here is how you could write the same thing (assuming the <= condition) using Perl-style foreach loops:
my $minL = 4;
for my $l (reverse 1 .. $minL) {
for my $i (0 .. $minL - $l) {
print "$i $l\n";
}
}
In the first iteration:
for (my $l = $minL; $l > 0; $l--) {
$l == $minL
for (my $i = 0; $i + $l < $minL; $i++) {
So this block won't run until $l is decremented:
print "$i $l\n";
}
Change your inner loop to:
for (my $i = 0; $i + $l <= $minL; $i++) {

perl: bignum not working with STDIN

#!/usr/bin/perl
use bignum;
$line = <STDIN>;
( $arr[0], $arr[1], $n ) = split( / /, $line );
$i = 2;
sub func {
while ( $i < $n ) {
$t = $arr[ $i - 1 ];
$arr[$i] = $arr[ $i - 1 ] * $arr[ $i - 1 ] + $arr[ $i - 2 ];
$i = $i + 1;
}
return $arr[ $i - 1 ];
}
print func;
when i am setting manual value for $arr[1] then bignum is working fine but when the value is taken from STDIN then it is being printed as integer no BIGINt. can anyone explain why this is happening.
When you use strings in a numeric context, perl converts them using your C library's atof(). This is not changed by bignum. If you'd like your strings converted in a different manner, I'd recommend Math::BigFloat->new or Math::BigInt->new.

Perl, using variable from within While loop outside of the loop?

This seems really simple but it is giving me a hard time figuring it out as I'm new to perl.. I've been looking through a lot of documentation now about loops and I am still stumped by this... I have a sub that contains a while loop and I want to use a variable value from within the loop outside of the loop (after the loop has run), however when I try to print out the variable, or return it out of the sub, it doesn't work, only when I print the variable from within the loop does it work.. I would appreciate any advice as to what I'm doing wrong.
Doesn't work (doesn't print $test ):
sub testthis {
$i = 1;
while ($i <= 2) {
my $test = 'its working' ;
$i++ ;
}
print $test ;
}
&testthis ;
Works, prints $test:
sub testthis {
$i = 1;
while ($i <= 2) {
my $test = 'its working' ;
$i++ ;
print $test ;
}
}
&testthis ;
You declare variable test inside the loop, so it scope is the loop, as soon as you leave the loop the variable is not longer declared.
Add my $test; just between $i=1 and while(..) and it will work. The scope will now be the entire sub instead of only the loop
Place my $test before the while loop. Note that it will only contain the last value that is assigned in the while loop. Is that what you are after?
// will print "it's working" when 'the loop is hit at least once,
// otherwise it'll print "it's not working"
sub testthis {
$i = 1;
my $test = "it's not working";
while ($i <= 2) {
$test = "it's working";
$i++ ;
}
print $test ;
}
you can try this one:
sub testthis {
my $test
$i = 1;
while ($i <= 2) {
$test = 'its working' ;
$i++ ;
print $test ;
}
}
&testthis ;
Note: whenever write perl code, its better to add use strict; and use warning in the beginning of the code.