• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 180
  • Last Modified:

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
0
cucugirl
Asked:
cucugirl
  • 6
  • 3
  • 2
  • +1
1 Solution
 
ghostdog74Commented:

my $str="name-1";
$str =~ s/-/\n/;
print $str;

Open in new window

0
 
Kim RyanIT ConsultantCommented:
@a = qw{name-1 name-2 name-3};

foreach $el ( @a )
{
    ($name,$num)= split(/-/,$el); # seperate contents either side of the dash
    push(@b,$name); # add each to a new array
    push(@b,$num);
}
0
 
cucugirlAuthor Commented:
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ghostdog74Commented:
arrays start from 0.
0
 
cucugirlAuthor Commented:
started it from 0 and same problem :(
0
 
cucugirlAuthor Commented:
any ideas? why it isn't working guys? I've been trying this for a bit and still failing.. :'(
0
 
ozoCommented:
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;
0
 
cucugirlAuthor Commented:
$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?
0
 
ozoCommented:
What is in FILE.txt?
which of the folders is missing?
and what does make_folders do?
0
 
cucugirlAuthor Commented:
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
0
 
cucugirlAuthor Commented:
error message says: File exists when it is going to the subroutine
0
 
ozoCommented:
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?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now