cucugirl
asked on
spliting an array
guys I have one more thing to ask, I have an array that has elements like this:
name-1
name-2
name-3
Each element in the array is each line. I was wondering if you guys know how i can split this array into the following:
name
1
name
2
name
3
each line being an element of the array and just splitting where the "-" is located.
thanks! :D :D
name-1
name-2
name-3
Each element in the array is each line. I was wondering if you guys know how i can split this array into the following:
name
1
name
2
name
3
each line being an element of the array and just splitting where the "-" is located.
thanks! :D :D
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i think this solution is perfect BUT now i tried making folders that have the name of each other element in this new array. I should have 34 folders but there are only 33 showing.
foreach $el ( @array ){
($name,$num)= split(/-/,$el);
push(@list,$name);
push(@list,$num);
}
for($i=1; $i<=68; $i+=2){
&make_folders($list[$i]);
}
where $list is the new array that i created with the previous code... and there were originally 34 lines:
name1-1
name2-2
name3-3
.....
name34-34
do you think the array is omitting one of the names? I'm sending only the odd elements to a subroutine &make_folders;
the odd numbers are the ones that show "name1" which is the left side of each line which is what i need to make the folders.. thanks! :D Also, I don't know how important this maybe but this list of names that i originally provided for the first question is an array that contains lines of .txt file.
foreach $el ( @array ){
($name,$num)= split(/-/,$el);
push(@list,$name);
push(@list,$num);
}
for($i=1; $i<=68; $i+=2){
&make_folders($list[$i]);
}
where $list is the new array that i created with the previous code... and there were originally 34 lines:
name1-1
name2-2
name3-3
.....
name34-34
do you think the array is omitting one of the names? I'm sending only the odd elements to a subroutine &make_folders;
the odd numbers are the ones that show "name1" which is the left side of each line which is what i need to make the folders.. thanks! :D Also, I don't know how important this maybe but this list of names that i originally provided for the first question is an array that contains lines of .txt file.
arrays start from 0.
ASKER
started it from 0 and same problem :(
ASKER
any ideas? why it isn't working guys? I've been trying this for a bit and still failing.. :'(
Wouldn't it be easier to loop
for( @array )
than to loop
for($i=1; $i<=68; $i+=2){
which should be
for($i=0; $i<=$#list; $i+=2){
If you say you changed it, which name is being omitted?
If you know each element has exactly one "-", it may also be easier to create @list with
@list = map{split/-/} @array;
for( @array )
than to loop
for($i=1; $i<=68; $i+=2){
which should be
for($i=0; $i<=$#list; $i+=2){
If you say you changed it, which name is being omitted?
If you know each element has exactly one "-", it may also be easier to create @list with
@list = map{split/-/} @array;
ASKER
$filename = "FILE.txt";
open(my $fh,"<",$filename);
while(my $line = <$fh>){
chomp $line;
my ($num,$name)= split(/-/,$line);
&make_folders($name);
}close($fh);
this is what i have for right now.. and it is still printing 33 folders do you think it is maybe with the subroutine?
open(my $fh,"<",$filename);
while(my $line = <$fh>){
chomp $line;
my ($num,$name)= split(/-/,$line);
&make_folders($name);
}close($fh);
this is what i have for right now.. and it is still printing 33 folders do you think it is maybe with the subroutine?
What is in FILE.txt?
which of the folders is missing?
and what does make_folders do?
which of the folders is missing?
and what does make_folders do?
ASKER
the last folder is missing... FILE.txt is a file that looks like this:
1-name1
2-name2
3-name3
4-name4
make_folders:
this is what it does:
sub make_folders{
while(<@_>){
mkdir("../LIBRARY/".$file. "/".$_, 0777) or die "cannot $!";
}
}
$file is a timestamp createdi n another subroutine
1-name1
2-name2
3-name3
4-name4
make_folders:
this is what it does:
sub make_folders{
while(<@_>){
mkdir("../LIBRARY/".$file.
}
}
$file is a timestamp createdi n another subroutine
ASKER
error message says: File exists when it is going to the subroutine
Does name4 exist in the current directory?
Do you know that <@_> is doing a file glob, and is that what you intended?
if you
mkdir("../LIBRARY/".$file. "/".$_, 0777) or die "'../LIBRARY/$file/$_' $cannot $!";
what file does it die on?
Do you know that <@_> is doing a file glob, and is that what you intended?
if you
mkdir("../LIBRARY/".$file.
what file does it die on?
Open in new window