Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

spliting an array

Posted on 2009-04-13
12
Medium Priority
?
179 Views
Last Modified: 2012-05-06
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
Comment
Question by:cucugirl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 9

Expert Comment

by:ghostdog74
ID: 24129570

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

Open in new window

0
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 1000 total points
ID: 24129572
@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
 

Author Comment

by:cucugirl
ID: 24129715
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
Technology Partners: 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!

 
LVL 9

Expert Comment

by:ghostdog74
ID: 24129859
arrays start from 0.
0
 

Author Comment

by:cucugirl
ID: 24129899
started it from 0 and same problem :(
0
 

Author Comment

by:cucugirl
ID: 24129964
any ideas? why it isn't working guys? I've been trying this for a bit and still failing.. :'(
0
 
LVL 84

Expert Comment

by:ozo
ID: 24130514
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
 

Author Comment

by:cucugirl
ID: 24130558
$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
 
LVL 84

Expert Comment

by:ozo
ID: 24130606
What is in FILE.txt?
which of the folders is missing?
and what does make_folders do?
0
 

Author Comment

by:cucugirl
ID: 24130685
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
 

Author Comment

by:cucugirl
ID: 24130737
error message says: File exists when it is going to the subroutine
0
 
LVL 84

Expert Comment

by:ozo
ID: 24131480
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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question