Solved

spliting an array

Posted on 2009-04-13
12
169 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
  • 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 250 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now