Solved

spliting an array

Posted on 2009-04-13
12
170 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
pipe to sed or perl, please help 5 127
unable to remove invisible characters with perl 2 77
Writing a parser for java language 4 71
html form to write data to csv 24 96
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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

911 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

22 Experts available now in Live!

Get 1:1 Help Now